Computing.Net > Forums > Web Development > PHP Minder Boggler

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

PHP Minder Boggler

Reply to Message Icon

Name: Jamie_McCoy
Date: May 17, 2005 at 16:47:12 Pacific
OS: XP Pro 5.1
CPU/Ram: Pentium 4 2.4Ghz
Comment:

OK, this script is used in a rank table i have for my site.. Click Here For Live View

It works great, but theirs one major problem in the flawless code i have analysed for hours !!

its removing users completely from SCORES.TXT (flat file database) the only reason they should be removed, as you will see in the code, is if their score, falls below the score its being compared with. That would cause it to be added on the below line.. knocking the Bottom user off (Line 50)

Now here is what scores.txt looks like, 50 of these

Jay69|7601|17 May 2005
walshleah|3967|18 May 2005
craig724|3784|17 May 2005
miss unique|3644|18 May 2005
nicola049|3417|17 May 2005
scousa|3264|17 May 2005
fluke|2844|17 May 2005
tonydaboxer|2697|17 May 2005
scouser lad 05|2685|18 May 2005

username|score|date

now heres the code


//define files
$handle = file_get_contents("scores.txt");
$userscore = file_get_contents("members/$page/score.txt");

// put each line into its own variable
$handle = explode ("\n", $handle);

// split each line up by '|'
$userscore = explode ("|", $userscore);

// added code to stop the code if it meats given criteria
if ($userscore[0] == "jaymc" || $userscore[0] == NULL) {}
else {

// define the container
$names = array ();

// loop each element in array $handle as $name... y
foreach ($handle as $name){

// explode each element by "|"
$items = explode ("|", $name);


// test if the loop has reached where the score is below member/score and no insertion made
if (($items[1] <= $userscore[1]) and (!isset ($marker))) {

// insert
$names[] = implode ("|", $userscore);

// mark, so it won insert again in the next loop (although the $items[1] <= $userscore[1] still satisfy)
$marker = "TRUE";

}

if ($items[0] != $userscore[0]){
$names[] = $name;
}


}

// Re-write all the entries back into scores.txt
$fp = fopen("scores.txt", "w+");
fputs($fp, $names[0]."\n");
fputs($fp, $names[1]."\n");
fputs($fp, $names[2]."\n");
fputs($fp, $names[3]."\n");
fputs($fp, $names[4]."\n");
fputs($fp, $names[5]."\n");
fputs($fp, $names[6]."\n");
fputs($fp, $names[7]."\n");
fputs($fp, $names[8]."\n");
fputs($fp, $names[9]."\n");
fputs($fp, $names[10]."\n");
fputs($fp, $names[11]."\n");
fputs($fp, $names[12]."\n");
fputs($fp, $names[13]."\n");
fputs($fp, $names[14]."\n");
fputs($fp, $names[15]."\n");
fputs($fp, $names[16]."\n");
fputs($fp, $names[17]."\n");
fputs($fp, $names[18]."\n");
fputs($fp, $names[19]."\n");
fputs($fp, $names[20]."\n");
fputs($fp, $names[21]."\n");
fputs($fp, $names[22]."\n");
fputs($fp, $names[23]."\n");
fputs($fp, $names[24]."\n");
fputs($fp, $names[25]."\n");
fputs($fp, $names[26]."\n");
fputs($fp, $names[27]."\n");
fputs($fp, $names[28]."\n");
fputs($fp, $names[29]."\n");
fputs($fp, $names[30]."\n");
fputs($fp, $names[31]."\n");
fputs($fp, $names[32]."\n");
fputs($fp, $names[33]."\n");
fputs($fp, $names[34]."\n");
fputs($fp, $names[35]."\n");
fputs($fp, $names[36]."\n");
fputs($fp, $names[37]."\n");
fputs($fp, $names[38]."\n");
fputs($fp, $names[39]."\n");
fputs($fp, $names[40]."\n");
fputs($fp, $names[41]."\n");
fputs($fp, $names[42]."\n");
fputs($fp, $names[43]."\n");
fputs($fp, $names[44]."\n");
fputs($fp, $names[45]."\n");
fputs($fp, $names[46]."\n");
fputs($fp, $names[47]."\n");
fputs($fp, $names[48]."\n");
fputs($fp, $names[49]);
fclose($fp);
}

One thing.. that code is automatically run every 120 seconds by everyone online. My website sometimes has 300+ users online. Now im not sure about processing time, i would of thought you would be dealing in NANO seconds, but maybe it could be as the code is being run... and just as its writing the new 50 entries to file, the same script is being run by another user and reading in the entries, which have only half been written to scores.txt

One thing to add, i have a similar script that does WHOS ONLINE, works great, but sometimes for no reason at all users online can drop from280 to 50... and then gradually adds the users back as their code is executed...


roamhosting.net



Sponsored Link
Ads by Google

Response Number 1
Name: jam14online
Date: May 17, 2005 at 22:53:21 Pacific
Reply:

Hi Jamie,

The huge list of fputs() calls can surely be replaced by a foreach() or for() loop?

Flat file databases are really quite limiting and VERY slow. I recommend you try out SQLite databases; they're like a smaller (hence 'lite') version of a full-blown MySQL database.

Download MiniApache from here, extract it to c:\www and run setup.cmd. (Follow the instructions on that site.) Then go to http://127.0.0.1/sqlite/ in your browser. The default username *and* password is admin. Finally, you can now add a new database and play around with the clever SQL-ness of it all.

I promise if you adapt to SQLite, you won't receive these slow downs when executing your script. It will also be much more reliable; I remember if you ever made a mistake in a flat file database, it took ages to find it.

Free PC Help forums
MiniApache


0
Reply to Message Icon

Related Posts

See More


Legality ? Setting up a site using M...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: PHP Minder Boggler

php n00b www.computing.net/answers/webdevel/php-n00b/265.html

PHP - saving a file www.computing.net/answers/webdevel/php-saving-a-file/2142.html

php help: submitting txt fields www.computing.net/answers/webdevel/php-help-submitting-txt-fields/2871.html