Hi
This is day two of php for me. My head hurts. I have a script that takes names, scores,minutes taken,and seconds taken by each player from a Flash game, sorts them into order by score and returns the records to the game and it actually works!
However I now wish to remove records that are over 7 days old and would be grateful for tips on the easiest way to do it.
The code I have is

$winscore = (int)$winscore;
	$keeptime = 7*(24*60*60);
	$scoretime= time();

// Create a Blank File if it doesn't already exist
if (!file_exists($x4high.sco))
{
	$file=fopen($x4high.sco, "w");
	fclose ($file);
}

// Read the file in
$oscores = file ($x4high.sco);
$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];
	$tscores[$i][2] = $g[2];
	$tscores[$i][3] = $g[3];

}

// 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;
	$tscores[$scoresize + 1][2] = $winmins;
	$tscores[$scoresize + 1][3] = $winsecs;


	sort ($tscores);

	$file=fopen($x4high.sco, "w");

	// Write them out
	for ($i = 0; $i < $scoresize; $i++)

	/
	{

		$st = serialize($tscores[$i]) . "\n";
		fputs($file, $st);}


	fclose($file);
}

I can't figure out how to add a new field with a timestamp for each record. I then need to delete those records where the (current time-timestamp)>keeptime. This is defeating me.

Any tips would be much appreciated.

    John. I think the reason that you haven't seen any responses to this question is that you're doing things the hard way by storing fields in a text file. Text files are fine for some purposes but you've stumbled on exactly the reason why people use databases for this sort of project. There is no tool that's designed to update a single "field" in a text file since text files don't have fields.

    It's true that databases take a little time to learn but it's worth it. It will save you millions of hours in the long run. Changing one "field" in a text file will require parsing with regular expressions and it will get messy fast. I know this sounds like a pain but since this is only your 3rd day with PHP, this is the perfect time to get into good habits.

    You or your ISP will need to create a MySQL database for you. They will give you a username, password, and database name. Then you will create a table in the database and write a PHP script to write records to the table that keeps the high scores. There are a bazillion tutorials on the Internet for doing this.

      This part of the code:

              $tscores[$scoresize + 1][0] = $winscore; 
              $tscores[$scoresize + 1][1] = $winname; 
              $tscores[$scoresize + 1][2] = $winmins; 
              $tscores[$scoresize + 1][3] = $winsecs; 

      appears to list the different columns each row has. This is where you would add a new column and use a function like [man]time/man to insert the current Unix timestamp.

      Also, your syntax definitely has errors. Such as the file name, which should be written as:

      if (!file_exists($x4high . '.sco'))

      and so on.

        thanks for you help. I think that I need to look into databases.

        Cheers
        John

          Write a Reply...