Highscore on multiple pages...

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
alexander19
Posts: 180
Joined: Fri Apr 02, 2010 1:05 pm

Highscore on multiple pages...

Post by alexander19 »

Hello everyone!

I got a problem with the highscore system,I can create a standard score system,but the problem is when the game will have a serious amount of players.
All the online game I've seen uses a system that will divide the players amount by 50 on each page.
So if you got 170 players..the highscore page would be divided in 4 pages...3 pages with 50 players and the last one with 20.
Been googling on some examples or tutorials on this thing,but found nothing.
Anyone got some ideeas?

Alexander,
Last edited by alexander19 on Fri Aug 13, 2010 7:40 am, edited 1 time in total.
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: Highscore on multiple pages...

Post by Chris »

Should this not be in the help and support section?

And the easiest way of doing this would be to use a limit your SQL.

I'll write something up real quick:

Code: Select all

$rowsPerPage = 50;
$_GET['pageNum'] = empty($_GET['pageNum']) === false ? (int)($_GET['pageNum']-1) : 0;

// list of the stuff
$query = mysql_query("SELECT * FROM `table` ORDER BY `field` ASC LIMIT ". ($rowsPerPage*$_GET['pageNum']) .",$rowsPerPage");
$i = 1;
while( $array = mysql_fetch_assoc($query) )
{
    echo (($rowsPerPage*$_GET['pageNum'])+$i) ." {$array['field']}<br />\n";
    $i++;
}


// page links
$query = mysql_query("SELECT * FROM `table`");
for( $i=1; $i<=(mysql_num_rows($query)/$rowsPerPage); $i++ )
{
    echo "<a href=\"{$_SERVER['PHP_SELF']}?pageNum=$i\">". ( ($_GET['pageNum']+1) == $i ? "<strong>$i</strong>" : $i )."</a> \n";
}
 
Make sure to read the code, should be self explanatory.
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Highscore on multiple pages...

Post by Callan S. »

I'm not sure what the problem is?
alexander19
Posts: 180
Joined: Fri Apr 02, 2010 1:05 pm

Re: Highscore on multiple pages...

Post by alexander19 »

Thanks Chris,I think I got the point,gonna try it and let you know how it worked.

Callan,
I'm trying to do a score system which will show only 50 members per page,so if I got 120 players..that would mean 3 pages,2 pages with 50 players and the third page will have 20 players.

Edit:Man that was awesome,it worked like a charm,thanks again ;)
There were 2 small bugs...first:if I had 120 members,there were 3 pages,2 with 50 members and another with 20 pages,but there were only 2 link,for the first and for the second page,to reach the 3rd page I had to reach it manualy.
I solved this by changing :

Code: Select all

 for( $i=1; $i<=(mysql_num_rows($query)/$rowsPerPage); $i++ )

to

Code: Select all

 for( $i=1; $i<=ceil(mysql_num_rows($query)/$rowsPerPage); $i++ )
..I added only the ceil function to round up the number.
Now for the second bug...the links are always behind by 1,if you click on the second link,it will show you all the members from that page,but the previous link will be selected(in this case,link 1).
I guess the problem is from here:

Code: Select all

 $_GET['pageNum'] = empty($_GET['pageNum']) === false ? (int)($_GET['pageNum']-1) : 0;
On that -1...but if I dont add it,the link would jump ahead with +1,and if I add +1,it will jump with + 2 everytime I click on a link
Is there anyway to move it regulary ?
Post Reply

Return to “Advanced Help and Support”