Search Engine echo-ing too many negative results
Posted: Fri Jul 09, 2010 2:56 pm
Hi - I posted this in the thread where I found the code from a YoutTube tutorial, but wanted to post it here as well to see if anyone can figure out what I've done. I get multiple "No Results" each time I run a search. It looks as through somehow I've told the page to display "No Results" for each of my 40 entries that it does not match. It will return positive results and display my data as I've told it, but it includes the negative hits, as well. Here's what I've managed to put together so far (I'm a newbie to php and have been building my database from the ground up, so I'm sure I've missed something somewhere ):
Code: Select all
//get data
$button = $_GET['submit'];
$search = $_GET['search'];
if (!$button)
echo "You didn't submit a keyword.";
else
{
if (strlen($search)<=2)
echo "Search not valid. Search term too short.";
else
{
echo "You searched for <b>$search</b> <hr size='1'>";
//connect to our database
mysql_connect("localhost:8888","root","root");
mysql_select_db("snakebook");
$get = mysql_query("SELECT * FROM specieslist");
while ($getrow = mysql_fetch_assoc($get))
{
//explode our search term
$search_exploded = explode(" ",$search);
foreach($search_exploded as $search_each)
{
//construct query
$x++;
if ($x==1)
$construct .="Scientific_Name LIKE '%$search_each%'
OR Common_Name LIKE '%$search_each%'
OR Physical_Characteristics LIKE '%$search_each%'
OR Geographic_Range LIKE '%$search_each%'
OR Diet LIKE '%$search_each%'
OR Venom LIKE '%$search_each%'
OR Habitat LIKE '%$search_each%'
OR Notes LIKE '%$search_each%'
";
else
$construct .=" AND Scientific_Name '%$search_each%'
AND Common_Name LIKE '%$search_each%'
AND Physical_Characteristics LIKE '%$search_each%'
AND Geographic_Range LIKE '%$search_each%'
AND Diet LIKE '%$search_each%'
AND Venom LIKE '%$search_each%'
AND Habitat LIKE '%$search_each%'
AND Notes LIKE '%$search_each%'
";
}
//echo out construct
$construct = "SELECT * FROM specieslist WHERE $construct";
$run = mysql_query($construct);
$foundnum = mysql_num_rows($run);
if ($foundnum==0)
echo "No results found.";
else
{
echo "$foundnum result(s) found.<p><hr size='1'>";
while ($runrows = mysql_fetch_assoc($run))
{
//get data to display
echo "<center><table border='0' cellpadding='5' cellspacing='0'>
<tr>
<td><strong>Thumbnail</strong></td>
<td><strong>Scientific Name</strong></td>
<td><strong>Common Name</strong></td>
<td><strong>View/Edit</strong></td>
</tr>";
$common = $runrows['Common_Name'];
$scientific = $runrows['Scientific_Name'];
$thumbnail = $runrows['Thumbnail'];
//display data
echo "<tr>
<td><strong>$thumbnail</strong></td>
<td><strong><i>$scientific</i></strong></td>
<td><strong>$common</td></strong>
<td><strong>
</tr>";
}
}
}
}
}