I am re-creating a flash game of mine and saving high scores in a MySQL database. In the earlier version each new score was simply added to the database and when retrieving the top 10 scores I simply ordered results based upon score descending. However, with time I had hundreds of scores saved even though I was only showing the top ten.
I changed my score script to simply update existing scores, under a certain name, if the score was better than the current saved score... and if it was worse just ignore it all together.. That at least helped reduce the number a little... but still there were hundreds of scores that were stored that no one would see.
So, this time I decided to write a script that would truncate the table each time scores were retrieved and then to simply re-insert the top 10.
This works most of the time... however, every once in a while it happens that I get double the scores... like it never emptied the table and just reinserted the scores...
Anyway... here's the code... interested in suggestions, thanks...
if($_POST['action']=="get"){
$tscores="";
$tempnames=array();
$tempscores=array();
$scorequery=mysql_query("SELECT * FROM ".$score_table." ORDER BY pscore ".$sort_order);
if(mysql_num_rows($scorequery)){
$til=@mysql_numrows($scorequery);
for($i=0;$i<$til;$i++){
$pn=mysql_result($scorequery,$i,"pname");
$ps=mysql_result($scorequery,$i,"pscore");
array_push($tempnames,$pn);
array_push($tempscores,$ps);
}
}
$cleanquery=mysql_query("TRUNCATE TABLE $score_table");
$scorecount=count($tempscores);
if($scorecount>$maximum_results){
$til=$maximum_results;
} else {
$til=$scorecount;
}
$a=1;
for($i=0;$i<$til;$i++){
$populatequery=mysql_query("INSERT INTO ".$score_table." (pname,pscore) VALUES ('".$tempnames[$i]."','".$tempscores[$i]."')");
$tscores.="name".$a."=".$tempnames[$i]."&score".$a."=".$tempscores[$i]."&";
$a++;
}
while ($a<=$maximum_results){
$tscores.="name".$a."=NONE&score".$a."=0&";
$a++;
}
$thisoutput=$tscores;
}