Page 1 of 1

help me change this script

Posted: Mon Aug 04, 2003 1:10 pm
by Robslob
I need help changeing this script so it ranks lowscore as top.

Code: Select all

<?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] = "";
	}

	// 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] = "no-one";
		$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")
	{
	  // HTML PAGE CREATED HERE
	  ?>


		
<table cellpadding=2 cellspacing=2 border=0 width="330">
  <tr align=center> 
    <th bgcolor="#663300" width="80"><font color="#ffff00" face="Arial, Helvetica, sans-serif" size="1">Rank</font></th>
    <th bgcolor="#663300" width="126"><font color="#FFFF00" face="Arial, Helvetica, sans-serif" size="1">Name</font></th>
    <th bgcolor="#663300" width="104"><font color="#FFFF00" face="Arial, Helvetica, sans-serif" size="1">Score</font></th>
  </tr>
  <?
	
		for ($i = 0; $i < $scoresize; $i++)
		{
			echo ("<tr bgcolor='#669933' align='center'><td><font size='1' face='Arial, Helvetica, sans-serif'>");
			echo ($i + 1);
			echo ("</font></td><td><font size='1' face='Arial, Helvetica, sans-serif'>");
			echo ($tscores[$i][1]);
			echo ("</font></td><td><font size='1' face='Arial, Helvetica, sans-serif'>");
			echo ($tscores[$i][0]);
			echo ("</font></td></tr>");
		}

  	  ?>
</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 ("&");
		}
	}

?>
the change should be made here

Code: Select all

// 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);
	}

Posted: Mon Aug 04, 2003 1:34 pm
by m3rajk
the easiest place to reverse it is here:

Code: Select all

<?
   for ($i = 0; $i < $scoresize; $i++) 
      { 
         echo ("<tr bgcolor='#669933' align='center'><td><font size='1' face='Arial, Helvetica, sans-serif'>"); 
         echo ($i + 1); 
         echo ("</font></td><td><font size='1' face='Arial, Helvetica, sans-serif'>"); 
         echo ($tscores[$i][1]); 
         echo ("</font></td><td><font size='1' face='Arial, Helvetica, sans-serif'>"); 
         echo ($tscores[$i][0]); 
         echo ("</font></td></tr>"); 
      } 
?>
simply by counting down instead. otherwise, it might be more optimal to use a foreach.

and for this section,

Code: Select all

<?

   // 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); 
   } 
?>
have you thought of using push/pop ?that would actually do exactly what you wasn. push them on ther einstead of how you do it, and when pronting, change that to

Code: Select all

<?
while ( $array){
pop(array)
}
?>

Posted: Tue Aug 05, 2003 10:17 am
by Robslob
the script will not work if a reverse how it prints the score.
it will still push out lowscores from the db file.

i wanna make the script push out the highscores and ranks lowscore as no1

Posted: Tue Aug 05, 2003 10:52 am
by m3rajk
if you pull the scores from sql sorted desc and then each time you process a row push it onto the array, then using pop on the array when printing them will print them from the lowest score to the highest