query("select quantity as qty,id,price,product_code from ecmstockoperations where deleted='0' and type='0' and in_id IS NULL and stock_id='c7afd71a-4c3a-bde4-138d-4acaee1644e4' and product_id='".$pid."' and (used=0 or used is null)"); while($rrr=$GLOBALS['db']->fetchByAssoc($w)){ $ww=$GLOBALS['db']->query("select quantity from ecmstockoperations where in_id='".$rrr['id']."' and type='1' and in_id IS NOT NULL and stock_id='c7afd71a-4c3a-bde4-138d-4acaee1644e4' and product_id='".$pid."' and deleted='0'"); $rqty=0; while($rrp=$GLOBALS['db']->fetchByAssoc($ww)){ $rqty+=$rrp['quantity']; } $qty=$rrr['qty']-$rqty; if($qty>0){ $arr[]=array("qty"=>$qty,"id"=>$rrr['id'],"price"=>$rrr['price']); } } if(count($arr)>0){ foreach($arr as $v){ $q1+=$v['qty']; $pp1+=$v['qty']*$v['price']; } } print $rrr['product_code']." ".$q1." ".(microtime()-$start)."\n"; mysql_close($sql); ?>