Displaying numbers

hi guys, im completely new to php and require a bit of assistance…

i am creatign a racing game in flash and have created a high scores table to post you best laptimes…

I have got all this working correctly appart from the fact that the score will only post to the decimal place. For example

if you get a time of 01.01.000 then the time will be displayed in the table as 1

how can i make it post the whole score?

the code im using is this…

PHP:

<?php $winscore = (int)$winscore; // Create a Blank File if it doesn't already exist if (!file_exists($filename)) { $file=fopen($filename, "w"); fclose ($file); } // Read the file in $oscores = file ($filename); $numreadin = count($oscores); // Break out the data into a new 2-d array called $tscores for ($i = 0; $i < $numreadin; $i++) { $g = unserialize($oscores[$i]); $tscores[$i][0] = $g[0]; $tscores[$i][1] = $g[1]; } // Fill in any missing data with none/0 for ($i = $numreadin; $i < $scoresize; $i++) { $tscores[$i][0] = 0; $tscores[$i][1] = "none"; } // Process the actions // Insert a score/name if ($action == "INSERT") { // Add name to end of list, and sort $tscores[$scoresize + 1][0] = $winscore; $tscores[$scoresize + 1][1] = $winname; rsort ($tscores); $file=fopen($filename, "w"); // Write them out for ($i = 0; $i < $scoresize; $i++) { $st = serialize($tscores[$i]) . "n"; fputs($file, $st); } fclose($file); } // Clear the list if ($action == "CLEAR") { $k[0] = 0; $k[1] = "none"; $ser = serialize($k); $file=fopen($filename, "w"); for ($i = 0; $i < $scoresize; $i++) { $st = $ser . "n"; fputs($file, $st); } fclose($file); } // Process the OUTPUT options if ($viewtype == "HTML") { for ($i = 0; $i < $scoresize; $i++) { echo (" "); echo ($i + 1); echo (" "); echo ($tscores[$i][1]); echo (" "); echo ($tscores[$i][0]); echo (" "); } ?>
    </table> 
  <? 

} 

// FLASH DATA CREATED HERE 
if ($viewtype == "FLASH") 
{ 
    for ($i = 0; $i < $scoresize; $i++) 
    { 
        echo ("NAME" . $i . "="); 
        echo ($tscores[$i][1]); 
        echo ("&SCORE" . $i . "="); 
        echo ($tscores[$i][0]); 
        echo ("&"); 
    } 
} 

?>

anybody’s help will be great!!

thanks

John

Well, I didn’t read the rest after seeing this, but I don’t think its really necessary…

$winscore = (int)$winscore;

Just take out that line. It is converting the winscore to an integer… what did you expect? Integers don’t have decimal places.

thanks mate, sorry i know it seems like i being dumb to you but i have never even looked at a peice of php coding!

thanks again
John

Yes that works thanks, next problem…

the code all works fine now but it is sorting it the wrong way! i would like to have the lowest score first as it is a table of best laptimes on a racing game! I have tried changing all the “<” to “>” but that doesnt work and to be honest i dont have a clue what im doing in PHP

any help would be great,

cheers
John

Well, now that I’ve taken the time to go through the code for you, I’ve found a few other things I’d like to comment on besides the one you are having troubles with.

// Create a Blank File if it doesn't already exist if (!file_exists($filename)) { $file=fopen($filename, "w"); fclose ($file); }
Replace that with “touch($filename)” and you are in business with less code.

Secondly, in several loops involving arrays on here you have used “for” instead of “foreach” - this makes your code MUCH more complex to read. I advise you change them to foreach.

Question… vars such as $scoresize… where did you get them? I see them used without first being set to anything. Are they post vars? If so, why do you have register globals turned off?

As for your problem of order, well… I don’t know about you, but if I was concentrating on the order of my array, I’d pay very close attention to any code that changed the array. Such as:
rsort($tscores);

If you look up this function, it is called “Reverse Sort” and sorts from highest to lowest. If that isn’t the way you want it, then just call “sort()” instead.

hi, thanks for your guidence,

the reason that its all written incorrectly is i obtained the code from a flash tutorial on flashkit.com and this is the first time i have ever even read php coding! i dable a bit in actionscript but have never used php up untill now!

So… as for your question on the vars such as $scoresize… i have no idea basically!

you say to replace the code creating a blank file, do you mean scrap all that code you quoted and replace with the “touch($filename)”??

About my original problem now i just tried changing rsort($tscores); to just sort(); and it didnt work?? i also tried sort($tscores); and that didnt work either!

any ideas?

thanks again
John

Sponsor our Newsletter | Privacy Policy | Terms of Service