funbinod 3 Posted May 13, 2014 Report Share Posted May 13, 2014 (edited) how can i loop a result from a php function to loop through each 'id'? like for all 'sid's, i want to use following function to get data... /* above this i calculate value for each $cqty */$cqty = $oqty + $inqty - $outqty;$camt = "select date, pqty, prate, pamt from stock stjoin purchase p on st.sid = p.sid where st.sid = $sidunion allselect date, -sqty, srate, samt from stock stjoin sales s on st.sid = s.sid where st.sid = $sidunion allselect null, oqty, oprice, oamt from stock where sid = $sidorder by date asc";$dataSet = array();$camtresult = mysqli_query($connect, $camt);while ($row = mysqli_fetch_array($camtresult)){ $dataSet[] = $row;}function camt($dataSet, $cqty){ $totalPurchaseValue = 0; $totalPurchaseQty = 0; $done = false; foreach($dataSet as $row) { // skip sales rows if ($row['pqty'] < 0) { continue; } for ($i=0; $i<$row['pqty']; $i++ ) { $totalPurchaseQty++; $totalPurchaseValue += $row['prate']; if ($totalPurchaseQty == $cqty) { return $totalPurchaseValue; } } } return $totalPurchaseValue;}// Determine the $countOfGoodsForSale$crate = camt($dataSet, $cqty) / $cqty;$camt = $cqty*$crate; i tried while loop but it said--- Fatal error: Cannot redeclare camt() (previously declared in E:xampphtdocsacstock.php:73) in E:xampphtdocsacstock.php on line 73 please guide through Edited May 13, 2014 by funbinod Quote Link to post Share on other sites
justsomeguy 1,135 Posted May 13, 2014 Report Share Posted May 13, 2014 Remove the function definition from inside the loop. You can only define a function once. Quote Link to post Share on other sites
funbinod 3 Posted May 14, 2014 Author Report Share Posted May 14, 2014 this function is only for 'sid=1'. i mean for single 'sid'. and is working properly. but i asked for help to derive values for all 'sid's. i mean to ask u 'how to use this function more than once'. Quote Link to post Share on other sites
justsomeguy 1,135 Posted May 14, 2014 Report Share Posted May 14, 2014 You can call the function as many times as you want. But you can only define the function once. So, don't define a function inside of a loop. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.