Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP - Retrieve top 5 from database

Featured Replies

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.

post-19991-0-46595800-1318699500_thumb.jpg

on your mysql statement you add to it ORDER BY `Field Name` ASC LIMIT 0 , 5

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

Yes, but he wants those with the top points, ie the highest 5, where as you would have the bottom 5

Edited by Jay Gilford

  • 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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.