[SOLVED] Help Grouping Results of Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Canadian Pickle
Forum Newbie
Posts: 12
Joined: Wed Jun 23, 2004 11:12 am
Location: It a Canadian Fridge

[SOLVED] Help Grouping Results of Array

Post by Canadian Pickle »

Hi All,

I have some Php code that grabs information from a MySQL database.

Once I have the information I take 2 fields and minus them. Depending on the answer, I use a Switch() function to assign a new value ...ie 8.

What I need help with now is, grouping all these new values under a key.

For example, user scobu may have 8, 4, 3 ,2 in the array. I need to sum those values up.

Here is the code I have so far... Any help would be appreciated. :D

--------------------------------------------------
--------------------------------------------------

Code: Select all

<?php require_once('Connections/waxyourboard.php'); ?>
<?php
mysql_select_db($database_waxyourboard, $waxyourboard);
$query_rsgetinfo = "SELECT FirstName, LastName, GolferID, Enter_Score.GolfDate, (Hole1 + Hole2 + Hole3 + Hole4 + Hole5 + Hole6 + Hole7 + Hole8 + Hole9) as Grandtotal, magicnum FROM Enter_Score, Golfer, MagicNumber WHERE Golfer.GolferID = Enter_Score.Golfer AND MagicNumber.golfdate = Enter_Score.GolfDate";
$rsgetinfo = mysql_query($query_rsgetinfo, $waxyourboard) or die(mysql_error());
$row_rsgetinfo = mysql_fetch_assoc($rsgetinfo);
$totalRows_rsgetinfo = mysql_num_rows($rsgetinfo);
?>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

<p>&nbsp;</p>
<p>&nbsp;</p>
<p><font color="#FF0000" size="4" face="Arial, Helvetica, sans-serif"><?php echo $row_rsgetinfo['FirstName']; ?> <?php echo $row_rsgetinfo['LastName']; ?></font></p>
<p>&nbsp;</p>

<table border="1" align="center">
  <?php do { 
  
  $a = $row_rsgetinfo['Grandtotal'];
  $b = $row_rsgetinfo['magicnum'];
  $c = $row_rsgetinfo['GolferID'];
  
  switch($a - $b)
  {
  // Look for people who got 10 points
  case "0":
  case "8":
  case "16":
  case "24":
  case "-8":
  case "-16":
  case "-24":
  $test = "10";
  $name[] = $c;
  break;
  // Look for people who got 9 points
  case "1":
  case "7":
  case "9":
  case "15":
  case "17":
  case "23":
  case "25":
  case "-1":
  case "-7":
  case "-9":
  case "-15":
  case "-17":
  case "-23":
  case "-25":
  $test = "9";
  $name[] = $c;
  break;
  // Look for people who got 8 points
  case "2":
  case "6":
  case "10":
  case "14":
  case "18":
  case "22":
  case "-2":
  case "-6":
  case "-10":
  case "-14":
  case "-18":
  case "-22":
  $test = "8";
  $name[] = $c;
  break;
  // Look for people who got 7 points
  case "3":
  case "5":
  case "11":
  case "13":
  case "19":
  case "21":
  case "-3":
  case "-5":
  case "-11":
  case "-13":
  case "-19":
  case "-21":
  $test = "7";
  $name[] = $c;
  break;
  // Look for people who got 6 points
  case "4":
  case "12":
  case "20":
  case "-4":
  case "-12":
  case "-20":
  $test = "6";
  $name[] = $c;
  break;
  default:
  $test = "0";
  $name[] = $c;
  break;
  }
  ?>
  <tr>
    <td><div align="center">Date of Golf</div>
    </td>
    <td> <div align="center">Total Score</div>
    </td>
    <td><div align="center">Magic Number Rolled</div>
    </td>
    <td><div align="center">Points Given</div>
    </td>
  </tr>
  <tr>
    <td><div align="center"><?php echo $row_rsgetinfo['GolfDate']; ?></div>
    </td>
    <td><div align="center"><?php echo $row_rsgetinfo['Grandtotal']; ?></div>
    </td>
    <td><div align="center"><?php echo $row_rsgetinfo['magicnum']; ?></div>
    </td>
    <td><?php print_r($test); ?>
    <div align="center"></div></td>
  </tr>
  <?php } while ($row_rsgetinfo = mysql_fetch_assoc($rsgetinfo));?>
</table>
</body>
</html>
<?php
mysql_free_result($rsgetinfo);
?>
Last edited by Canadian Pickle on Fri Jun 25, 2004 11:43 am, edited 2 times in total.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

<?php
<?php require_once('Connections/waxyourboard.php'); ?> 
<?php 
mysql_select_db($database_waxyourboard, $waxyourboard); 
$query_rsgetinfo = "SELECT FirstName, LastName, GolferID, Enter_Score.GolfDate, (Hole1 + Hole2 + Hole3 + Hole4 + Hole5 + Hole6 + Hole7 + Hole8 + Hole9) as Grandtotal, magicnum FROM Enter_Score, Golfer, MagicNumber WHERE Golfer.GolferID = Enter_Score.Golfer AND MagicNumber.golfdate = Enter_Score.GolfDate"; 
$rsgetinfo = mysql_query($query_rsgetinfo, $waxyourboard) or die(mysql_error()); 
$row_rsgetinfo = mysql_fetch_assoc($rsgetinfo); 
$totalRows_rsgetinfo = mysql_num_rows($rsgetinfo); 
?> 
<html> 
<head> 
<title>Untitled Document</title> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
</head> 
<body> 

<p> </p> 
<p> </p> 
<p><font color="#FF0000" size="4" face="Arial, Helvetica, sans-serif"><?php echo $row_rsgetinfo['FirstName']; ?> <?php echo $row_rsgetinfo['LastName']; ?></font></p> 
<p> </p> 

<table border="1" align="center"> 
<?php do { 

$a = $row_rsgetinfo['Grandtotal']; 
$b = $row_rsgetinfo['magicnum']; 
$c = $row_rsgetinfo['GolferID']; 

switch($a - $b) 
{ 
// Look for people who got 10 points 
case "0": 
case "8": 
case "16": 
case "24": 
case "-8": 
case "-16": 
case "-24": 
$test = "10"; 
$name[] = $c; 
break; 
// Look for people who got 9 points 
case "1": 
case "7": 
case "9": 
case "15": 
case "17": 
case "23": 
case "25": 
case "-1": 
case "-7": 
case "-9": 
case "-15": 
case "-17": 
case "-23": 
case "-25": 
$test = "9"; 
$name[] = $c; 
break; 
// Look for people who got 8 points 
case "2": 
case "6": 
case "10": 
case "14": 
case "18": 
case "22": 
case "-2": 
case "-6": 
case "-10": 
case "-14": 
case "-18": 
case "-22": 
$test = "8"; 
$name[] = $c; 
break; 
// Look for people who got 7 points 
case "3": 
case "5": 
case "11": 
case "13": 
case "19": 
case "21": 
case "-3": 
case "-5": 
case "-11": 
case "-13": 
case "-19": 
case "-21": 
$test = "7"; 
$name[] = $c; 
break; 
// Look for people who got 6 points 
case "4": 
case "12": 
case "20": 
case "-4": 
case "-12": 
case "-20": 
$test = "6"; 
$name[] = $c; 
break; 
default: 
$test = "0"; 
$name[] = $c; 
break; 
} 
?> 
<tr> 
<td><div align="center">Date of Golf</div> 
</td> 
<td> <div align="center">Total Score</div> 
</td> 
<td><div align="center">Magic Number Rolled</div> 
</td> 
<td><div align="center">Points Given</div> 
</td> 
</tr> 
<tr> 
<td><div align="center"><?php echo $row_rsgetinfo['GolfDate']; ?></div> 
</td> 
<td><div align="center"><?php echo $row_rsgetinfo['Grandtotal']; ?></div> 
</td> 
<td><div align="center"><?php echo $row_rsgetinfo['magicnum']; ?></div> 
</td> 
<td><?php print_r($test); ?> 
<div align="center"></div></td> 
</tr> 
<?php } while ($row_rsgetinfo = mysql_fetch_assoc($rsgetinfo));?> 
</table> 
</body> 
</html> 
<?php 
mysql_free_result($rsgetinfo); 
?>
?>
USE PHP TAGS
User avatar
Canadian Pickle
Forum Newbie
Posts: 12
Joined: Wed Jun 23, 2004 11:12 am
Location: It a Canadian Fridge

Post by Canadian Pickle »

How do you use Php tags?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Wrap your code in
[syntax=php]....[/syntax]
User avatar
Canadian Pickle
Forum Newbie
Posts: 12
Joined: Wed Jun 23, 2004 11:12 am
Location: It a Canadian Fridge

Post by Canadian Pickle »

Sorry!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Do you want to sum up right in the DB and pull that result out, or do you want to sum up all the values for a given username and store them in an array?

[php_man]array_sum[/php_man] might help.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Canadian Pickle
Forum Newbie
Posts: 12
Joined: Wed Jun 23, 2004 11:12 am
Location: It a Canadian Fridge

Post by Canadian Pickle »

I was hoping to sum up all values for a username, and then diplay the values within the same page.

Nothing fancy, just wanted to show who has the most points, etc.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

What about something like this:

Code: Select all

//group and sum the elements by username
foreach($un_summed_array as $username=>$associated_value)
{
  $summed_array[$username] += $associated_value;
}

//display the summed elements
foreach($summed_array as $username=>$sum)
{
  echo "$username has a summed value of: $sum";
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Canadian Pickle
Forum Newbie
Posts: 12
Joined: Wed Jun 23, 2004 11:12 am
Location: It a Canadian Fridge

Post by Canadian Pickle »

Hello,

Still can't seem to get it to work.

The code works fine, but it is only giving me the first value for a username, instead of adding them all up for a user name.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

So, just to help me get my head wrapped around your problem: You're pulling results out of a table, but there may be multiple entries for a given GolferID. You want to sum up all the results of GrandTotal - magicnum for each GolferID and display that? If you're storing the scores and such in the name() array, then I think you want to put

Code: Select all

$name[$c] += 10; //or 9, or 8, or whatever $test is assigned to ($test isn't really necessary I don't think).
at the end of each case, before the break statement.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Canadian Pickle
Forum Newbie
Posts: 12
Joined: Wed Jun 23, 2004 11:12 am
Location: It a Canadian Fridge

[Solved!]

Post by Canadian Pickle »

Thank you!

That worked great. I just have to figure out now how to output each total for each person.

thanks again :D
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Hey no problem, happy to help out a fellow pickle. :)
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply