Jump to content

[SOLVED] Sum a slice of an array


woody86

Recommended Posts

   $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

   $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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.