A feature of this highscore chart is that it will only calculate itself once per half hour (this timeframe can easily be adjusted). That way it wont go through your hundreds of players on every single page call.
First of all you need a new entry in your database 'highscorces'. In it you need to add to the structure
id: small int, size: 2
highscorestring: variable characters, size: around 500 character (you may need to adjust depending on the size of you highscore table
averagescore: medium int, size: 4
timestamp: big int, size: 10
Also make one entry with it, with an ID of one, everything else blank. It'll only ever have one entry.
The code calls on the database with this line you can find in the code
Code: Select all
$playerinfo = "SELECT name, score FROM players";
$playerinfo2 = mysql_query($playerinfo);
while($playerinfo3 = mysql_fetch_array($playerinfo2))
{ // --------------------------------------------------------------------------------------
$playername=$playerinfo3['name'];
$scoretocheck=$playerinfo3['score'];
Good luck! You can see how I've implemented it in my game
Code: Select all
<?php
include_once 'connect.php';
session_start();
$highscoreinfo="SELECT * from highscores where id='1'";
$highscoreinfo2=mysql_query($highscoreinfo) or die("could not get player stats!");
$highscoreinfo3=mysql_fetch_array($highscoreinfo2);
$highscoretimestamp = $highscoreinfo3['timestamp'];
// Change this value to the number of top scores you want to show
$numberoftopscores=10;
// initialising the temporary top score list
for($count=0;$count<=$numberoftopscores-1;$count+=1)
{
//$topscores[$count]=110-(($count+1)*10);
$topscores[$count]=0;
$topscoresname[$count]="";
//echo $topscores[$count] . "<br>";
}
// How to change the half hour thing
// the 1800 is the time in seconds it will wait for the next score update
// Change the value to the number of seconds you want
if (time() >= ($highscoretimestamp+1800))
{ // &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
// !!!CHANGE!!! 'score' to the name of the interger in the database you want to check
// Also of course change 'name' or 'players' if needed
$playerinfo = "SELECT name, score FROM players";
$playerinfo2 = mysql_query($playerinfo);
while($playerinfo3 = mysql_fetch_array($playerinfo2))
{ // --------------------------------------------------------------------------------------------------
$playername=$playerinfo3['name'];
$playerscreenname=$playerinfo3['screenname'];
$scoretocheck=$playerinfo3['score'];
//echo $scoretocheck . ", <br>";
/////////////////////////////////////////////////////////////////////////
$scorename = $playername;
// $scoreinsertposition shows either where to insert the new score, or
// if the score being checked doesn't beat any of the scores on the
// high score list, it defaults to $numberoftopscores+1, which is an indicator
// that the high score list isn't to be changed
$scoreinsertposition=$numberoftopscores+1;
for($count2=0;$count2<=$numberoftopscores-1;$count2+=1)
{
//echo $scoretocheck . ", " . $topscores[$count2] . "<br>";
if ($scoretocheck > $topscores[$count2])
{
//echo "Wuz more<br>";
$scoreinsertposition=$count2;
//echo $count2 . "<br>";
$count2=$numberoftopscores; // exits out of the for loop
}
}
//echo $dummyposition . "<br><br>";
//echo $scoreinsertposition . "<br>";
if ($scoreinsertposition <> $numberoftopscores+1) // ie only do so if it's found a score entry it's higher than
{
//echo $scoreinsertposition . ", " . $player . ", " . $scoretocheck ."<br>";
for($count2=$numberoftopscores-1;$count2>=$scoreinsertposition;$count2-=1)
{
$topscores[$count2+1]=$topscores[$count2];
$topscoresname[$count2+1]=$topscoresname[$count2];
}
$topscores[$scoreinsertposition]=$scoretocheck;
$topscoresname[$scoreinsertposition]=$scorename;
}
/////////////////////////////////////////////////////////////////////////
} // -------------------------------------------------------------------------------------------------
// finally render the top scores to a single string
$topscorestring = "";
for($count=0;$count<=$numberoftopscores-1;$count+=1)
{
if ($topscores[$count] > 0)
{
$topscorestring .="<b>#" . ($count+1) . "</b> . Power Factor: <b>" . $topscores[$count] . "</b> , <b>" . $topscoresname[$count] . "</b><br>";
}
else {$topscorestring .= "<b>#" . ($count+1) . " .</b><br>";}
}
// updating the string in the database
$updatehighscores="update highscores set highscorestring='$topscorestring' where id='1' limit 1";
mysql_query($updatehighscores) or die("Could not update highscorestring");
$currenttime = time();
$updatehighscores="update highscores set timestamp='$currenttime' where id='1' limit 1";
mysql_query($updatehighscores) or die("Could not update highscorestring");
} // &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
else
{
$topscorestring = $highscoreinfo3['highscorestring'];
}
echo $topscorestring;
?>