Jump to content

Trying to think of the best way to create a top ten list from a database.


tvance929

Recommended Posts

I'm new AND rusty with making arrays but I want to loop through my dbase table and grab the top ten scores, then json that array back to my page.

 

I have all the stuff I need to loop through my table rows.  I guess I can take the first one and then start comparing from there.

 

I was planning on having an associative array with userName and avgScore... I have gotten to this point :

 

//First check and see if this row has more than 9 games played.
if ($result->fields['gamesPlayed'] > 9) {

	//Get count of array
	$theCount = count($scoresArray);

	//If this is the first row then just add the first score.
	if ($theCount < 1) {	
			$scoresArray[$result->fields['userName']] = $result->fields['avgScore'];
	}
                
	else if  (BLAH BLAH BLHAAHAHAHA)

	//How to check this -- see if it's above the other.

 

So once I add the first score to the array, I am not sure how to compare the next and then only take the top 10....

 

Can someone give me some advice or point me to a some?

 

Thanks a bunch!

t

 

----------------

Now playing: The David Crowder Band - O Praise Him (All This For A King) (Oceanic Mix)

via FoxyTunes

I am a bit rusty well alot rusty myself but I would use SQL when pulling it from the database depending on how your databse is set up

 

something like off the top of my head I am very rusty so probably needs cleaning up

 

$sql = "SELECT username FROM db.table WHERE gamesplayed > 9 ORDER by avgScore DESC LIMIT 10";

 

like I siad I am very rusty but you could look int using sql to get the 10 rows you want from your db?

PHP has a few sort functions for arrays.

 

sort()

usort()

 

So you can just populate your array, then sort it and take the first 10.

 

If you are pulling the records from a database, you can use the database functions to return and sort your data.

I love both of these options!!! Thanks guys!!!  I will probably do the sql idea, I should have thought of it and it will be extremely easy. 

 

Thanks!

 

yay I feel usefull - and let it be noted that this is my 100th post on these forums wooo - party!!

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.