Search Engine echo-ing too many negative results

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
boba fett
Forum Newbie
Posts: 15
Joined: Fri Jul 09, 2010 2:48 pm

Search Engine echo-ing too many negative results

Post by boba fett »

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>";

}

}
}

}

}

cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Search Engine echo-ing too many negative results

Post by cpetercarter »

If you echo out the value of $construct immediately before you run the database query, is it what you expect?

I ask this because I suspect your logic is wrong. This bit is surely not right :

Code: Select all

$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%'
";
This says, in effect, that you are looking for records where the same search term (eg "green") matches something in each of 8 separate fields - which is improbable, and may be the reason why you keep getting the "no records found" message.
boba fett
Forum Newbie
Posts: 15
Joined: Fri Jul 09, 2010 2:48 pm

Re: Search Engine echo-ing too many negative results

Post by boba fett »

ok, but i want the search engine to search through all of my categories to find a keyword that was typed in. What code would i need to use in order to get that to happen???
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Search Engine echo-ing too many negative results

Post by cpetercarter »

I someone puts in several search terms (eg "green non-venomous Sumatra"), do you want to find results which contain ANY of these search terms, or ALL of them. In other words, do you want to find any snake which is green or is non-venomous or lives in Sumatra, or only snakes which are green and non-venomous and live in Sumatra?

Then you should be able to construct the logic of your database query - either

"WHERE fielda = 'green' OR fieldb = 'green' ... OR fielda = 'non-venomous' OR fieldb = 'non-venomous' ... OR fielda = 'Sumatra' OR fieldb = 'Sumatra' ....etc)

or

WHERE (fielda = 'green' OR fieldb = 'green' ....) AND (fielda = 'non-venomous' OR fieldb = 'non-venomous'....) AND (fielda = 'Sumatra' OR fieldb = 'Sumatra' ...)
boba fett
Forum Newbie
Posts: 15
Joined: Fri Jul 09, 2010 2:48 pm

Re: Search Engine echo-ing too many negative results

Post by boba fett »

OK, the search is working,and its finding what I want it to, but it is also echoing "No Results" for every time it encounters something in the table that does not match the search parameter. My problem is getting rid of that extra "No Result" displayed on my results page
Post Reply