boba fett Posted July 9, 2010 Share Posted July 9, 2010 Hi - I posted this in the thread where I found the code from the 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): <?php //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>"; } } } } } ?> Parsed in 0.083 seconds, using GeSHi 1.0.8.4 Link to comment https://forums.phpfreaks.com/topic/207287-search-engine-echo-ing-too-many-negative-results/ Share on other sites More sharing options...
Pikachu2000 Posted July 10, 2010 Share Posted July 10, 2010 You're looping through every record in specieslist the first query's while{} loop, and echoing "No Results" each and every time a record that has no results is encountered in the loop. I took the liberty of indenting and adding consistent curly brace usage to your code, so I could read it and use bracket-matching in my editor. <?php //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>"; } } } } } ?> Link to comment https://forums.phpfreaks.com/topic/207287-search-engine-echo-ing-too-many-negative-results/#findComment-1083904 Share on other sites More sharing options...
boba fett Posted July 12, 2010 Author Share Posted July 12, 2010 Thanks for looking at the code. I built this following a tutorial from phpacademy on Youtube, and for the most part am happy with its functionality. I am new to php and am working on it in Dreamweaver CS4, so I'm sorry if it's not spaced regularly (I'm new to this, and am learning as I go). I understand what it's doing by returning too many "no results", but am unclear on how to tell it to stop. I've been tinkering with the SELECT statement and the if foundnum else statements for several days before trolling the web to see if anyone can point me in the right direction as to what part of the code is causing my current headace. Link to comment https://forums.phpfreaks.com/topic/207287-search-engine-echo-ing-too-many-negative-results/#findComment-1084959 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.