Skipjackrick Posted February 15, 2008 Share Posted February 15, 2008 hello again fellas. So, I am trying to make a standings page which will rank every team by the amount of points they have acquired. I figured out how to sum up the total points but I want to sort the points from Highest to Lowest and keep the (team/points) relationship together. Could you guys help out a Noobie!?! If you have any ideas on how I can populate the Rank column with First, Second, and third etc. that would help out too. But one step at a time. Thanks AGAIN!!! You guys always help out!! $query_sum = "SELECT team_id, SUM(points) FROM submit GROUP BY team_id"; $result = mysql_query($query_sum) or die(mysql_error()); $kayakwars_header=<<<EOD <h2><Center>Kayak Wars 2008 Standings</center></h2> <table width='70%' border='1' cellpadding='2' cellspacing='2' align='center'> <tr> <th>Rank</th> <th>Team</th> <th>Points</th> </tr> EOD; function get_team_name() { global $team_id; global $teamname; $query_d = "SELECT team_name FROM team WHERE team_id='$team_id'"; $results_d = mysql_query($query_d) or die(mysql_error()); $row_d = mysql_fetch_array($results_d); extract ($row_d); $teamname = $team_name; } while($row = mysql_fetch_array($result)) { $team_id = $row['team_id']; $points = $row['SUM(points)']; //get team's name from team table get_team_name($team_id); $kayakwars_details .=<<<EOD <tr> <td>First</td> <td>$teamname</td> <td>$points</td> </tr> EOD; } $kayakwars_footer ="</table>"; $kayak_wars =<<<KAYAKWARS $kayakwars_header $kayakwars_footer KAYAKWARS; print "There are $num_entries Kayak Wars submissions in our database"; print $kayak_wars; Quote Link to comment Share on other sites More sharing options...
marcus Posted February 15, 2008 Share Posted February 15, 2008 $x=1; while($row = mysql_fetch_array($result)) { <td>$x</td> EOD; $x++; } would produce something like <tr><td>1</td><td>team</td><td>points</td></tr> <tr><td>2</td><td>team</td><td>points</td></tr> <tr><td>3</td><td>team</td><td>points</td></tr> etcetera Quote Link to comment Share on other sites More sharing options...
Skipjackrick Posted February 15, 2008 Author Share Posted February 15, 2008 $x=1; while($row = mysql_fetch_array($result)) { <td>$x</td> EOD; $x++; } would produce something like <tr><td>1</td><td>team</td><td>points</td></tr> <tr><td>2</td><td>team</td><td>points</td></tr> <tr><td>3</td><td>team</td><td>points</td></tr> etcetera Cool (one down), but how do I sort the Points from Highest to Lowest? Quote Link to comment Share on other sites More sharing options...
marcus Posted February 15, 2008 Share Posted February 15, 2008 you have SUM(points) make it SUM(points) as total_points then after FROM submit add ORDER BY total_points DESC Quote Link to comment Share on other sites More sharing options...
Skipjackrick Posted February 15, 2008 Author Share Posted February 15, 2008 you have SUM(points) make it SUM(points) as total_points then after FROM submit add ORDER BY total_points DESC I Tried that and for some reason it doesn't return any data from the database. I think its trying to GROUP BY team and then we are telling it to ORDER by total_points which contradicts the GROUP BY statement. I am clueless. Quote Link to comment Share on other sites More sharing options...
Skipjackrick Posted February 15, 2008 Author Share Posted February 15, 2008 YAY!!! I got it!! THANKS MGALLFOREVER!! I had an error on the rest of my table. You had it correct. i didn't realize you could use order by and group by together. THANKS AGAIN!! Quote Link to comment Share on other sites More sharing options...
Barand Posted February 15, 2008 Share Posted February 15, 2008 ORDER BY comes last <?php $query_sum = "SELECT team_id, SUM(points) as total_points FROM submit GROUP BY team_id ORDER BY total_points DESC"; Quote Link to comment 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.