1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help with sorting highscore - rsort()

Discussion in 'PHP' started by mofle, Apr 6, 2007.

  1. #1
    Hi all you beautiful people

    I'm making a Flashgame with a php highscore.
    In my game I have a timer and I want the highscore to show me the lowest time.
    But the code is created to show the best score.
    I think I have to do something with the "rsort" or something.

    Can anybody help me with this?

    It works as it is, but the highest time is sortet to the top not the bottom.

    If you help me I will include your name in the credits of the game.

    http://www.mofle.net/moflegame2/

    Start the game and click on the button named "Highscore" to test the Highscore.

    The game is far from be finished...


    <?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")
    	{
    	  // HTML PAGE CREATED HERE
    	  ?>
    
    
    		<table cellpadding=2 cellspacing=2 border=0 width="152">
    		<tr align=center> 
    		<th bgcolor="#000033"><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">#</font></th>
    		<th bgcolor="#000033"><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Name</font></th>
    		<th bgcolor="#000033"><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Score</font></th>
    		</tr>
    
       	  <?
    	
    		for ($i = 0; $i < $scoresize; $i++)
    		{
    			echo ("<tr bgcolor='#666666' align='center'><td><font size='2' face='Arial, Helvetica, sans-serif'>");
    			echo ($i + 1);
    			echo ("</font></td><td><font size='2' face='Arial, Helvetica, sans-serif'>");
    			echo ($tscores[$i][1]);
    			echo ("</font></td><td><font size='2' 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 ("&");
    		}
    	}
    
    ?>
    
    Code (markup):
     
    mofle, Apr 6, 2007 IP