Jump to content

SUM function help


lukep11a

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/244538-sum-function-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.