tvance929 Posted June 19, 2009 Share Posted June 19, 2009 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 Link to comment https://forums.phpfreaks.com/topic/162959-trying-to-think-of-the-best-way-to-create-a-top-ten-list-from-a-database/ Share on other sites More sharing options...
SetToLoki Posted June 19, 2009 Share Posted June 19, 2009 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? Link to comment https://forums.phpfreaks.com/topic/162959-trying-to-think-of-the-best-way-to-create-a-top-ten-list-from-a-database/#findComment-859833 Share on other sites More sharing options...
J.Daniels Posted June 19, 2009 Share Posted June 19, 2009 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. Link to comment https://forums.phpfreaks.com/topic/162959-trying-to-think-of-the-best-way-to-create-a-top-ten-list-from-a-database/#findComment-859835 Share on other sites More sharing options...
tvance929 Posted June 20, 2009 Author Share Posted June 20, 2009 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! Link to comment https://forums.phpfreaks.com/topic/162959-trying-to-think-of-the-best-way-to-create-a-top-ten-list-from-a-database/#findComment-859934 Share on other sites More sharing options...
SetToLoki Posted June 20, 2009 Share Posted June 20, 2009 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!! Link to comment https://forums.phpfreaks.com/topic/162959-trying-to-think-of-the-best-way-to-create-a-top-ten-list-from-a-database/#findComment-859935 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.