Link to home
Start Free TrialLog in
Avatar of andyJSP
andyJSP

asked on

TXT database search. Please help.

I am trying to create a script that will search a txt database.
I have built the part of my script that splits everything up into peaces. For Example:
$name $email $url $company $keywords
The user can chose to search, lets say the name. But I can?t figure out how to tell my script to search the name.
Can anyone help me?
Avatar of heddesheimer
heddesheimer

Here is the code:
first you have a text file like this:
===============
name; email; url; company; keywords
jon smith; joe@home.de; www.joe.com; joe ltd.; company, limited, best in town
eric jones; eric@myhome.com; www.eric.com; nocomp; best cook in town
==============

each item separated by semicolon ";"

Here is the PHP code:
=============
<?
// this is to illustrate the usage:
echo "found for name=smith: " . find_item("text.txt", "name", "smith") . "<br>";
echo "found for name=jon: " . find_item("text.txt", "name", "jon") . "<br>";
echo "found for key=town: " . find_item("text.txt", "keywords", "town") . "<br>";
echo "found for key=dummy: " . find_item("text.txt", "keywords", "dummy") . "<br>";
echo "found for key=cook: " . find_item("text.txt", "keywords", "cook") . "<br>";

// this is the actual find function:
function find_item($filename, $item_name, $word_to_find)
{
 $txt_ary = file($filename);
 switch($item_name)
 {
   case "name": $index = 0; break;
   case "email": $index = 1; break;
   case "url": $index = 2; break;
   case "company": $index = 3; break;
   case "keywords": $index = 4; break;
 }

 $line_num = 1;
 foreach($txt_ary as $line)
 {
  $fld_ary = split(";", $line);
  if (strpos($fld_ary[$index], $word_to_find) === false)
  {
  } else {
    // string found
    return $line_num;
  }
  $line_num ++;
 }
 // no string found in the loop
 return 0;
}
?>
=====================

This function runs only until it finds the first ocurrence in the file. If you want to find all, the script must be modified. I think you may get the idea.

If you have more than just a few lines (about 100 lines would be o.k.) you should consider using a database instead.

Marian
Avatar of andyJSP

ASKER

But there is still something i don't understand.
I have lines split up like this in the txt file,
Micah Dear|micah@mtl-bizz.com|http://mtl-bizz.com|MTL-BIZZ|Web Hosting
and so on...

I have the part of the script all ready built that splits up all the spaces and gives them $
So lets say i wanted to search for Bill in the $name, how would i do that?
Maybe what you need is a variable variable like this:

<?
$name = "john";
$adr = "london";
$varname = "name";

echo $$varname;
?>

Use the double $ for accessing a variable where the name of the variable is stored in (that is variable variable). Confusing huh?

Marian
Avatar of andyJSP

ASKER

Ok so I have a test box called "words".
<input type="text" name="words"><br><input type="submit">
And in my text database, I have the name, email, url, company, and keywords split up into variables.

<?php
$count=0;
$databin = file("database.dat");
$filesize = filesize("database.dat");
$file = fopen("database.dat","r");
$buffer = fread($file,$filesize);

$csvdata = explode("|",$databin[$count]);
$name = wordwrap($csvdata[0],"<br>");
$email = wordwrap($csvdata[1],"<br>");
if (strstr($csvdata[2],"http://")) {
$url=" <a href=\"".$csvdata[2]."\" target=\"_blank\">$csvdata[2]</a>";
}
$company = wordwrap($csvdata[3],"<br>");
$keywords = wordwrap($csvdata[4],"<br>");
?>

Now lets say I have a scroll down menu that allows the user to search the name, or company, or whatever and the user choses to search the name.
Would it be like this?
echo "search found: " . find_item($databin, "name", $words) . "<br>";

This is most probably wrong but I wrote it just to explain what I do not understand.
O.K. I understand your question. This code should work for your database file:

============
<?php
$words = "smith"; // $words comes from the entry form
echo "found for name=smith in line " . find_item("database.dat", "name", $words) . "<br>";


function find_item($filename, $item_name, $word_to_find)
{
 $txt_ary = file($filename);
 switch($item_name)
 {
   case "name": $index = 0; break;
   case "email": $index = 1; break;
   case "url": $index = 2; break;
   case "company": $index = 3; break;
   case "keywords": $index = 4; break;
 }

 $line_num = 1;
 foreach($txt_ary as $line)
 {
  $fld_ary = explode("|", $line);
  if (strpos($fld_ary[$index], $word_to_find) === false)
  {
  } else {
    // string found
    return $line_num;
  }
  $line_num ++;
 }
 // no string found in the loop
 return 0;
}
?>
=====================
The code can currently find only one word in the file. I suppose your variable "$words" will be more than one word. So you have to spit it up into single words and call the function for each word. I leave that as an exercise to yourself. If it will not work with this code, please post at least 5 lines of your textfile or provide a download link so I can have a look at the data.

Marian
Avatar of andyJSP

ASKER

I tryed it but it is still not working for me...
I'm very new with this aspect of PHP. I converted the search PHP file to txt format so that you can take a look it.
http://www.sonicmedia.net/v/search/search.txt
change the first line to:
<form action="test.php" method="post">

and name the file "test.php".

That should work (worked on my webserver so far).

If you want to use PHP_SELF you must use it this way:
<form action="<? echo $PHP_SELF?>" method="post">

because PHP_SELF is a variable so you must start it with "$" and it must be printed in the HTML file so you must use echo or print to have it visible in HTML.

If this not work, please give me more info on the error you receive. I understand from your questions (not your original one but the follow-ups) that you lack a basic understanding of PHP. I would suggest that you start with something more easy and straigtforward like a Form-Mailer or a simple Guestbook.

Hope that helps so far, if not, we can give it another try tomorrow.

Marian

Avatar of andyJSP

ASKER

It doesn?t give me an error. It?s just not performing the search. It says that it has found 0, that?s all. I realize that you have given me a lot of help for this and if you wanted I could open up another question and give you another 200 points there after my problem has been fixed.
Does that example still give you a result of 0? Are you sure the name you are searching is really in the file?
Please check that first.

by the way: I think you can increase your points offered without opening a new question. Would be nice if you would do that.
If you like, you can just give me a link to the data file too (or maybe just a part of if) so that I could set up a working solution for you on my webserver. Just to be sure the problem is really the code and not the data.

Marian
Avatar of andyJSP

ASKER

The max amount of points that I can have is 300, so I would need to create a new question to give you more points but I don?t mind, you?ve been great.

This is the current database:
http://www.sonicmedia.net/v/search/database.dat


This is the script that I made to create the database:
http://www.sonicmedia.net/v/search/post.php
And this is its source:
http://www.sonicmedia.net/v/search/viewTXT/post.txt


This is the script that I made to read from the database:
http://www.sonicmedia.net/v/search/view.php
And this is its source:
http://www.sonicmedia.net/v/search/viewTXT/view.txt


This is the search engine in action:
http://www.sonicmedia.net/v/search/search.php
And this is its source:
http://www.sonicmedia.net/v/search/viewTXT/search.txt

If you wanted to continue this through email I would be in agreement.

My email address is:
andrew@sonicmedia.net
Website:
sonicmedia.net


andy
ASKER CERTIFIED SOLUTION
Avatar of heddesheimer
heddesheimer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of andyJSP

ASKER

I have been the best!!
My next comment will be called
"Question for heddesheimer"
and you can help me a bit more with it.

Thanks,
your a smart guy