Forum Moderators: coopster

Message Too Old, No Replies

search through a flat file database

         

lucesei

4:20 am on Jun 27, 2004 (gmt 0)

10+ Year Member



I am trying to write some code to search through a flat file db. It's just a very simple thing:

date¦name¦e-mail¦IP¦comments

All I need is a snippet that will return a list of entries that match the keyword/s I submit...

I know: mysql bla bla bla... But I want to stick to flat files for this. Try as I might, I just can't understand how to do it.
Can someone point me to some on-topic tutorials or scripts? Tutorials would be best, since I'd like to actually learn how-to, rather than just copy&paste. But scripts are also OK if you do some reverse engineering.

Thanks so much for all the help you'll give me!

Laura

Zipper

7:19 am on Jun 27, 2004 (gmt 0)

10+ Year Member



As far as I'm concerened you will have to read the file first and then search for the keyword(s) you are looking for. Unless you use SQLite which comes with PHP 5.

The below code will read the text file and search each entry that matches your keyword which will then be tokenized and written to arrays for each field.


$keyword = "test"; //whatever your keyword is

if (file_exists("db.txt")){
$fdata = file("db.txt");
$c = 0;
foreach($fdata as $fline){
if(strpos($fline, $keyword)){
$date[$c] = strtok($fline,"¦");
$name[$c] = strtok("¦");
$email[$c] = strtok("¦");
$ip[$c] = strtok("¦");
$comments[$c] = strtok("¦");
$c++;
}
}
}

You can then display the results for the particular fields you need,

if($c > 0){
echo $c." results found for ".$keyword."<br>";
for($i=0; $i<=$c; $i++){
echo $date[$i];
echo $name[$i];
echo $email[$i];
echo $ip[$i];
echo $comments[$i];
}
}else {
echo "No results found for ".$keyword;
}

You can modify the way of searching and probably eliminate unwanted arrays if they are not involved in the search or output and I haven't tested this thing as I just wrote it. So I apologize if there are any errors. :) Also, please use google to find some tutorials or the following url's might be useful.
[devarticles.com...]
[codewalkers.com...]
[free2code.net...]

Hope this helps.