Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

TXT database search. Please help.

Posted on 2001-08-24
12
Medium Priority
?
291 Views
Last Modified: 2008-03-06
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?
0
Comment
Question by:andyJSP
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 4

Expert Comment

by:heddesheimer
ID: 6423098
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
0
 

Author Comment

by:andyJSP
ID: 6423158
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?
0
 
LVL 4

Expert Comment

by:heddesheimer
ID: 6423181
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:andyJSP
ID: 6424959
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.
0
 
LVL 4

Expert Comment

by:heddesheimer
ID: 6425196
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
0
 

Author Comment

by:andyJSP
ID: 6425220
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
0
 
LVL 4

Expert Comment

by:heddesheimer
ID: 6425261
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

0
 

Author Comment

by:andyJSP
ID: 6425343
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.
0
 
LVL 4

Expert Comment

by:heddesheimer
ID: 6425858
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
0
 

Author Comment

by:andyJSP
ID: 6425977
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
0
 
LVL 4

Accepted Solution

by:
heddesheimer earned 1200 total points
ID: 6426004
andy
you know what? The solution is easy!

Try to find "Andrew" instead of  "andrew"! The script just did not find the name because it is case sensitive :-)

Would be glad If that solve your problem and would bring me an A-grade ;-)

If you really want to give me more points, you are welcome to post another question. Hint: Ask how to make a search script that is case insensitive. I will provide that answert too :-)

Marian
0
 

Author Comment

by:andyJSP
ID: 6426012
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
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

704 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question