Hi all!
I've been working on an website which, I admit, is quite ambitious. Especially seeing as though I am teaching myself PHP as I go. (So if I sound like a n00b... well, I am. -to PHP that is, I do know Java.)
So, my problem is this:
I am trying to make a very basic search engine. It's supposed to search a text file line-by-line for the search term (one per line), and tell the user if the term was in the file. I tried making a function for this, but it turns out that it only reports back positively if the user was searching for the very last line, not any of the others.
Here's my code (basically, I edited out some extra stuff):
<?php
include("header.php");
function checkdb($search)
{
$file = fopen("data.txt", "r");
$match=false;
$current = "whatever";
while(!feof($file))
{
$current = fgets($file);
if(substr_compare($search, $current, 0) == 0)
{
$match = true;
}
}
return $match;
}
echo("<div align=\"center\">");
echo("<b>Search!</b><br>");
echo("<form action=\"search.php\" method=\"get\">");
echo("Enter your term here: <input type=\"text\" name=\"searchterm\">");
echo("<input type=\"submit\" value=\"Search\"></form></div><br><br><br>");
$search = $_GET["searchterm"];
$found = false;
if ($search == "")
{
echo("<title>Search</title>");
}
else
{
echo("<title>Search Results</title>
$found = checkdb($search);
if ($found == true)
{
echo("<br>Congrats, ".$search." is in our database!");
}
else
{
echo("<br>Sorry, ".$search." is not in our database.");
}
}
?>
Also, the format of the file data.txt is:
phrase1
phrase2
etc, etc.
Does anyone know what the problem is??
Thanks so much!