October 15, 201114 yr Hi all, Let me attempt to explain what i'm trying to do. I'm in the process of building a Fantasy Football game, and i'd like to be able to display the top 5 players. By top 5 I mean those with the highest points. I have a query which displays all players, but I would like to only display 5, and only the top 5 players with the most points. Hope i'm making sense! This is the code I have: <?php $query = "SELECT playername, club_id, value, points, picture FROM players"; $result = mysql_query($query); while($row = mysql_fetch_array($result,MYSQL_ASSOC)) { echo "{$row['playername']}<br>", "{$row['club_id']}<br>", "{$row['value']}<br>", "{$row['points']}<br>", "{$row['picture']}"; } ?> How would I amend this to display only 5, and then sort them by value? Ideally, i'd like to create something like the attached, but my PHP are limited and this entire project is a learning curve for me.
October 15, 201114 yr SELECT playername, club_id, value, points, picture FROM players ORDER BY points DESC LIMIT 5
October 15, 201114 yr on your mysql statement you add to it ORDER BY `Field Name` ASC LIMIT 0 , 5 You don't need the 0, unless you are offsetting the result set, and since you want the top 5 not the bottom 5, you'd need to use DESC not ASC
October 15, 201114 yr Yes, but he wants those with the top points, ie the highest 5, where as you would have the bottom 5 Edited October 15, 201114 yr by Jay Gilford
October 15, 201114 yr Author SELECT playername, club_id, value, points, picture FROM players ORDER BY points DESC LIMIT 5 Worked a treat. Cheers Jay!
Create an account or sign in to comment