I have a multi-column table that holds scores for games. The format is that each score has a gameid number attached to it. I then find the highest score for each gameid and display it. The problem is that if a game is deleted and the the gameid numbers are no longer consecutive, it will display 2 of the same high score for the game. An example would be a score of 5000 for gameid 3, 6000 for gameid 4, and 3200 for gameid 5. If I delete gameid 4, the output would be 5000-3, 5000-3, 3200-5.I obviously don’t want it to output 2 of the same score. Below is the PHP code I use to do this:
<?php
echo '<table cellpadding="0" class="content" cellspacing="0" width="100%" >';
echo '<tr><th>Game</th><th>Initials</th><th>Score</th><th>Date</th></tr>';
include("includes/sqlconnect.inc");
include("includes/functions.php");
$idnum=1;
$allscores = mysql_query("SELECT * FROM scores");
$highid = array(
);
while($row = mysql_fetch_array( $allscores ))
{
$idnums=$row['gameid'];
$highid[]=$idnums;
}
while($idnum<=max($highid))
{
$allscores = mysql_query("SELECT * FROM scores WHERE gameid ='$idnum'");
$array = array(
);
while($row = mysql_fetch_array( $allscores ))
{
$score=$row['score'];
$scoreid=$row['scoreid'];
$array[$scoreid]=$score;
}
$high=doublemax($array);
$nameid = mysql_query("SELECT scoreid,playerid,score FROM scores WHERE scoreid='$high[i]'");
while($row = mysql_fetch_array($nameid))
{
$player=$row['playerid'];
}
$dateid = mysql_query("SELECT scoreid,date FROM scores WHERE scoreid='$high[i]'");
while($row = mysql_fetch_array($dateid))
{
if($row['date']=="")
{
$date="Unknown";
}
else
{
$date=$row['date'];
}
}
$name = mysql_query("SELECT playerid,player_initials FROM players WHERE playerid='$player'");
while($row = mysql_fetch_array($name))
{
$initials=$row['player_initials'];
}
$gamename = mysql_query("SELECT gameid,gamename FROM games WHERE gameid='$idnum'");
while($row = mysql_fetch_array($gamename))
{
$game=$row['gamename'];
}
$idnum++;
echo "<tr>";
echo '<td>'.$game.'</td>';
echo '<td>'.$initials.'</td>';
echo '<td>'.number_format($score).'</td>';
echo '<td>'.$date.'</td>';
echo "</tr>";
}
echo "</table>";
?>
The problem is you’re basing the number of outputs on the id rather than the number of rows of data returned.
Rather than getting the max score later get that during your initial query
Then base your main loop on a foreach rather than a while.