To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Code Critique

Code Critique Having someone critique your code is always a great way to hone the skills. Stop in and post your code to see what your peers may have done differently.

Reply
 
Thread Tools Rate Thread Display Modes
Old 02-07-2004, 09:01 AM   #1
Daniel Mott
Junior Member
 
Join Date: Feb 2004
Posts: 9
Using a simple text file as a database

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 Code:
<?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)

PHP Code:
01¤
02¤
03¤
04¤
05¤
06¤
07 This day has an event
08¤
09¤
Daniel Mott is offline   Reply With Quote
Old 02-07-2004, 12:08 PM   #2
drawmack
Computers can do that?
 
drawmack's Avatar
 
Join Date: Apr 2003
Location: Pocono Mtns PA
Posts: 3,268
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.
drawmack is offline   Reply With Quote
Old 02-07-2004, 12:16 PM   #3
Daniel Mott
Junior Member
 
Join Date: Feb 2004
Posts: 9
Talking

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.
Daniel Mott is offline   Reply With Quote
Old 02-07-2004, 10:58 PM   #4
drawmack
Computers can do that?
 
drawmack's Avatar
 
Join Date: Apr 2003
Location: Pocono Mtns PA
Posts: 3,268
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.
drawmack is offline   Reply With Quote
Old 02-08-2004, 08:30 AM   #5
Daniel Mott
Junior Member
 
Join Date: Feb 2004
Posts: 9
How about this?

PHP Code:
<?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):

PHP Code:
¤
¤
¤
¤
¤
This line number has data
¤

Last edited by Daniel Mott; 02-08-2004 at 08:33 AM.
Daniel Mott is offline   Reply With Quote
Old 02-08-2004, 10:06 AM   #6
Weedpacket
Custom User Title™
 
Weedpacket's Avatar
 
Join Date: Aug 2002
Location: Rapid Offensive Unit "Foreign Object Damage"
Posts: 19,122
I think this'd be better:
Code:
07 This day has an event
14 So does this one
__________________
On two occasions I have been asked [by Members of Parliament], "Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?" I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
Weedpacket is offline   Reply With Quote
Old 02-08-2004, 10:49 PM   #7
drawmack
Computers can do that?
 
drawmack's Avatar
 
Join Date: Apr 2003
Location: Pocono Mtns PA
Posts: 3,268
You may be interested in this (especially the example) http://www.phpbuilder.net/board/show...eadid=10267611
drawmack is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 03:07 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.