Jump to content

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?

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 />';
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.