Click to See Complete Forum and Search --> : Using a simple text file as a database


Daniel Mott
02-07-2004, 09:01 AM
I tried making a short script that reads a text file with the current month, and then takes todays date and looks to see if there is an event that happened that date this or any other year. It looks like this. The text file has all the days of that month, an excerpt is under the code. Is there any way to optimize or rewrite this?

<?php
$data= "data/" . date ("M") . ".dat";
$today= @fopen("$data","r");
if(!$today) die("Kunde inte öppna datafilen!");
while (!feof($today)) {
if (fgets($today, 3) == date("d")) {
if (fgets($today, 2) == "¤") {
echo "No event is noted. Feel free to mail me one.";
}
else {
echo fgets($today, 255);
}
}
}
fclose ($today);
?>

Excerpt from text file (Feb.dat)

01¤
02¤
03¤
04¤
05¤
06¤
07 This day has an event
08¤
09¤

drawmack
02-07-2004, 12:08 PM
If the text file is sorted and you only have one entry for each date then you could just use http://www.php.net/file to read the file into an array and then entry n-1 (where n is today's date) would be the entry for today. No need to nested if's within a while loop.

If the text is sorted but the days can have multiple entries then you could still use file to read in the file, create one while loop that reads until it hit's todays date and incrments an index counter (probably i), then created a second while loop that reads today's entries and then stops, that will get rid of the nested if statements.

If you want to be really creative you can start at the begining of the file for the first half of the month and at the end of the file for the second half of the month.

If you want to be really really creative you could do a binary search instead of a linear search.

If you want to be really really really creative you can start the binary search at different places int he array based on the date of the month.

Daniel Mott
02-07-2004, 12:16 PM
Thank you very much for a good, thourough reply, with a good pinch of humor. :)

As you can probably see, I've only been programming for like one or two days, which somewhat explains my nested if's and generally weird code.

Your reply was of great use to me, thanks! I'll post the updated code here when it's finished.

drawmack
02-07-2004, 10:58 PM
I was bored so I wrote a class I think you'll be interested in. I'll post it tomorrow, I have to do up a usage sample and throw together some documentation on it. Basically it does everything you would ever need it to in order to have something like this on your site and it's a generic class so it can be used for otehr section that you want to run off of text based data files as well.

Daniel Mott
02-08-2004, 08:30 AM
How about this?

<?php
// Make a file path for the current month
$data = "data/" . strtolower(date ("M")) . ".dat";

// Read the file into $date
$date = file("$data");

// If the current date has no data, echo that to the user
if ( rtrim($date[date ("j")-1]) == "¤") { echo "Ingen händelse har noterats för detta datum. Bidra gärna med en!"; }

// If it has, output the date data
else { echo $date[date ("j")-1]; }
?>

(Ecerpt from the text file):

¤
¤
¤
¤
¤
This line number has data
¤

Weedpacket
02-08-2004, 10:06 AM
I think this'd be better:

07 This day has an event
14 So does this one

drawmack
02-08-2004, 10:49 PM
You may be interested in this (especially the example) http://www.phpbuilder.net/board/showthread.php?s=&threadid=10267611