funbinod Posted May 13, 2014 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 Link to comment Share on other sites More sharing options...
justsomeguy Posted May 13, 2014 Share Posted May 13, 2014 Remove the function definition from inside the loop. You can only define a function once. Link to comment Share on other sites More sharing options...
funbinod Posted May 14, 2014 Author 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'. Link to comment Share on other sites More sharing options...
justsomeguy Posted May 14, 2014 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. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now