I'm trying to loop through a list of results from MySQL and display all the unique game names and the count for each in an HTML table format. It works fine up until the last row which doesn't display the name or count. If the last name has multiple rows then it displays the name but not the count.
My code:
$propertyGameQuery = "SELECT G.GAME_NAME FROM GAMES AS G, PROPERTY_GAMES AS PG WHERE PG.PROPERTY_ID=$cid AND G.ID=PG.GAME_ID ORDER BY G.GAME_NAME";
$propertyGameResult = mysql_query($propertyGameQuery);
$gameCount = 0;
$rowCounter = 0;
$number = mysql_num_rows($propertyGameResult);
$rws = mysql_fetch_assoc($propertyGameResult);
$starterName = $rws['GAME_NAME'];
while($propertyGames = mysql_fetch_assoc($propertyGameResult)) {
$currentGameName = $propertyGames['GAME_NAME'];
if(($gameCount == 0) or ($rowCounter == $number)){
?>
<tr class="game-table"><td width="80%" title="<? echo $currentGameName; ?>" align="left" style="padding-left:5px;"><? echo $rowCounter ?> <? echo $currentGameName; ?></td>
<?
}
if(($currentGameName == $starterName) or ($lastGameName == $currentGameName)){
$gameCount = $gameCount + 1;
}else{
?>
<td width="20%" align="center"><? echo $gameCount + 1; ?></td></tr>
<?
$gameCount = 0;
} ?>
<?
$lastGameName = $currentGameName;
$rowCounter++;
}
mysql_*
functions for new code. They are no longer maintained and the community has begun the deprecation process. See the red box? Instead you should learn about prepared statements and use either PDO or MySQLi. If you can't decide, this article will help to choose. If you care to learn, here is good PDO tutorial. – Second Rikudo Jun 22 '12 at 20:34