lukep11a Posted August 11, 2011 Share Posted August 11, 2011 Hi, I have a table currently called 'test' with 3 columns, 'teamname', 'points' and 'date'. Every time a team plays they earn points based on their performances, the points they have earned are then inserted into the table so each team wil have many entries in the table, for example: Man Utd 50 2011-08-14 15:00:00 Arsenal 80 2011-08-14 15:00:00 Liverpool 100 2011-08-14 15:00:00 Man Utd 80 2011-08-21 15:00:00 Arsenal 20 2011-08-21 15:00:00 Liverpool 50 2011-08-21 15:00:00 What I am trying to do is display each team name once with the total points they have scored. This is the code I currently have that displays every entry in the table, does anybody know how I can ammend this to display each team once?? <table width="390" border="0" cellpadding="0" cellspacing="0"> <tr class="title"> <td width="65">Group</td> <td width="260">Team Name</td> <td width="65">Points</td> </tr> <?php $query = "SELECT * FROM test"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { ?> <tr class="teams"> <td width="65"></td> <td width="260"><?php echo $row['teamname']; ?></td> <td width="65"><?php echo $row['points']; ?></td> </tr> <?php } ?> <?php $query = "SELECT SUM(test.points) FROM test"; $result = mysql_query($query) or die(mysql_error()); $row = mysql_fetch_assoc($result); ?> <tr class="total"> <td width="65"> </td> <td width="260">Total Points</td> <td width="65"><?php echo $row['SUM(test.points)']; ?></td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/244538-sum-function-help/ Share on other sites More sharing options...
PFMaBiSmAd Posted August 11, 2011 Share Posted August 11, 2011 This query will get all the information using just one query - SELECT teamname, SUM(points) as total FROM test GROUP BY teamname You would reference the total (alias name for the SUM(points) value) as $row['total'] Quote Link to comment https://forums.phpfreaks.com/topic/244538-sum-function-help/#findComment-1256080 Share on other sites More sharing options...
lukep11a Posted August 11, 2011 Author Share Posted August 11, 2011 Thankyou for your help, it is now working Quote Link to comment https://forums.phpfreaks.com/topic/244538-sum-function-help/#findComment-1256084 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.