woody86 Posted September 28, 2007 Share Posted September 28, 2007 $result = mysql_query("SELECT Owner, TotalPoints FROM data ORDER BY Owner",$db); $myrow = mysql_fetch_array($result); //Slice array into each owner's team $b_result = array_slice($myrow, 0, 2); $w_result = array_slice($myrow, 2, 2); //Sum total points of each owner's team while($b_array = mysql_fetch_array($b_result)) { $b_points[]=$b_array['TotalPoints']; } $b_total=array_sum($b_points); while($w_array = mysql_fetch_array($w_result)) { $w_points[]=$w_array['TotalPoints']; } $w_total=array_sum($w_points); $b_result comes out with the data I expect. The problem is that $b_points and $b_total end up with no value. Any insight would be appreciated Link to comment https://forums.phpfreaks.com/topic/71096-solved-sum-a-slice-of-an-array/ Share on other sites More sharing options...
BlueSkyIS Posted September 28, 2007 Share Posted September 28, 2007 do you realize that you are only working on one row of data, not all records returned from that query? $myrow = mysql_fetch_array($result); is only called once. Link to comment https://forums.phpfreaks.com/topic/71096-solved-sum-a-slice-of-an-array/#findComment-357504 Share on other sites More sharing options...
woody86 Posted September 28, 2007 Author Share Posted September 28, 2007 $result = mysql_query("SELECT Owner, TotalPoints FROM data ORDER BY Owner",$db); while ($myrow = mysql_fetch_array($result)) { $myrow2[] = $myrow['TotalPoints']; } //Slice array into each owner's team $b_result = array_slice($myrow2, 0, 2); $w_result = array_slice($myrow2, 2, 2); //Sum total points of each owner's team while($b_array = mysql_fetch_array($b_result)) { $b_points[]=$b_array['TotalPoints']; } $b_total=array_sum($b_points); while($w_array = mysql_fetch_array($w_result)) { $w_points[]=$w_array['TotalPoints']; } $w_total=array_sum($w_points); Ok, so I'm a noob Anyways, so now $myrow2 should come out with all 4 rows in the table, I'm still left without any data output though. I am right to not include the slice in the loop right? Since the slice would act on the data already stored in $myrow2? Link to comment https://forums.phpfreaks.com/topic/71096-solved-sum-a-slice-of-an-array/#findComment-357520 Share on other sites More sharing options...
sasa Posted September 29, 2007 Share Posted September 29, 2007 try <?php $result = mysql_query("SELECT Owner, SUM(TotalPoints) as points FROM data GROUP BY Owner ORDER BY Owner",$db); while ($row = mysql_fetch_array($result)){ echo 'Owner: ',$row['Owner'],' - ',$row['points'],' point(s).<br />'; } ?> Link to comment https://forums.phpfreaks.com/topic/71096-solved-sum-a-slice-of-an-array/#findComment-357769 Share on other sites More sharing options...
woody86 Posted September 29, 2007 Author Share Posted September 29, 2007 Wow, I didn't realize that query had that much power! That works perfectly! Thank you very much Link to comment https://forums.phpfreaks.com/topic/71096-solved-sum-a-slice-of-an-array/#findComment-357773 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.