Hi, I am currently putting up scorecharts http://nzgames.org/games/guessinggame/highscore.php and there are some problems I am having, first problem... the scores are being sorted odd, how should I be sorting them? I am getting them from a MySQL DB, heres the code...
<?php
$conf = array();
include ('conf.php');
$DBUser = $conf['DB_USER'];
$DBPass = $conf['DB_PASS'];
$DBHost = $conf['DB_HOST'];
$DBName = $conf['DB_NAME'];
Function Connect_bdd()
{
Global $DBName, $DBHost, $DBPass, $DBUser;
$Link = mysql_connect($DBHost, $DBUser, $DBPass) Or die ("can't open the database");
mysql_select_db($DBName);
}
//------------------
Function Deconnect_bdd()
{
Global $Link;
@mysql_close ($Link);
}
//-------------------
Function Add_player_score ($player_name, $player_score, $player_kills)
{
$player_name = strToLower ($player_name);
Connect_bdd();
// $Requete = "INSERT INTO highscores ";
// $Requete .= "(id, player_name, player_score, player_kills) ";
// $Requete .= "VALUES (5, '$player_name','$player_score', '$player_kills')";
// $Result = mysql_query($Requete) Or die ($Requete . "<br>" . mysql_error());
$Requete = "Select * from highscores ";
$Requete .= "where player_name = '$player_name'";
$Result = mysql_query($Requete) Or die ($Requete . "<br>" . mysql_error());
//If (mysql_num_rows($Result) != 0) //REMOVED BY ME< MAY NEED TO ADD BACK -BRIAN
//{
//$Requete = "update highscores ";
//$Requete .= "set player_score='$player_score', player_kills='$player_kills' ";
//$Requete .= "where player_name = '$player_name'";
//$Resultat = mysql_query($Requete) Or die ($Requete . "<br>" . mysql_error());
//}
//Else
//{
$Requete = "INSERT INTO highscores ";
$Requete .= "(id, player_name, player_score, player_kills) ";
$Requete .= "VALUES (NULL, '$player_name','$player_score', '$player_kills')";
$Result = mysql_query($Requete) Or die ($Requete . "<br>" . mysql_error());
//}
Deconnect_bdd();
}
//-------------------
Function List_scores()
{
Connect_bdd();
$Requete = "Select player_name, player_score, player_kills from highscores order by player_score desc";
$Result = mysql_query($Requete) Or die ($Requete . "<br>" . mysql_error());
Deconnect_bdd();
If (mysql_num_rows($Result) != 0)
{
$strCache = "";
$Count = 0;
While ($Ligne=mysql_fetch_array($Result))
{
$Count++;
$strCache .= "$Count" . ". ";
$strCache .= $Ligne["player_name"] . ", guesses ";
$strCache .= $Ligne["player_score"] . " ";
//$strCache .= $Ligne["player_kills"] . " ";
$strCache .= "<br>";
}
}
Else
{
$strCache .= "score>no scores !!!n";
}
Print ($strCache);
}
//------------------- main program
$Op = $HTTP_GET_VARS['op'];
If (isset($Op))
{
switch($Op)
{
Case "Add":
echo 'URL Read, Ready to try and add to database.' . "<BR>";
Add_player_score ($HTTP_GET_VARS['player_name'], $HTTP_GET_VARS['player_score'], $HTTP_GET_VARS['player_kills']);
break;
Case "List":
List_scores();
break;
}
}
?>
<?php
@define('IN_PHPBB', true);
@$phpbb_root_path = '../../forums/';
global $phpbb_root_path;
include("../../HEADER.php"); ?>
Here is a small little online score chart demo.
<br>
<a href="http://www.nzgames.org/games/guessinggame/GuessingGame.zip">Download the guessing game to try it out.</a>
<br>
<br>
Score Chart.
<br>
<?php
List_scores();
include("../../FOOTER.php"); ?>
?>
The problem is in the Add_player_score function... I have tried many things but I cannot get it to sort properlly... also I want it to only hold the top 10 (or low 10, in this case) scores and to have them sorted numerically by score...
Also in the Add_player_score function I have noticed that whenever I add any score through a program that I write its all lowercase, is there any ways to store if the letters are capitalized?
Thanks ahead for your responses. 🙂