Page 1 of 1

[SOLVED] Help Grouping Results of Array

Posted: Wed Jun 23, 2004 11:12 am
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);
?>

Posted: Wed Jun 23, 2004 11:26 am
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

Posted: Wed Jun 23, 2004 11:28 am
by Canadian Pickle
How do you use Php tags?

Posted: Wed Jun 23, 2004 11:29 am
by markl999
Wrap your code in
[syntax=php]....[/syntax]

Posted: Wed Jun 23, 2004 11:34 am
by Canadian Pickle
Sorry!

Posted: Wed Jun 23, 2004 2:36 pm
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.

Posted: Thu Jun 24, 2004 7:14 am
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.

Posted: Thu Jun 24, 2004 10:04 am
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";
}

Posted: Thu Jun 24, 2004 2:46 pm
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.

Posted: Thu Jun 24, 2004 3:11 pm
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.

[Solved!]

Posted: Fri Jun 25, 2004 11:42 am
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

Posted: Fri Jun 25, 2004 11:53 am
by pickle
Hey no problem, happy to help out a fellow pickle. :)