I'm having a problem with our php/mysql search feature on our website. We have an item number, description, and long description that we'd like a user to be able to search through. Right now it is setup in two pieces. The first piece searches the item number and description. If it doesn't find anything it searches the long description. What we were thinking might happen though, is that there may be more records than the ones it returns. For example if it finds a term in the description it may stop, but that term may still appear in several long descriptions.
How can we set it up so that it will search through all three places and bring back all matching entries without duplicating the results?
Here is our code: // Searches Short Description (title) and Item Number
//error message begins //try another search error message? $XX = "Item Not Found"; //query details table begins $query = mysql_query(" SELECT coas50.pro_item.Item_Number, coas50.pro_item.Description, coas50.pro_item.ID FROM coas50.pro_item WHERE MATCH coas50.pro_item.Description AGAINST ('%$search%') OR coas50.pro_item.Item_Number LIKE ('%$search%') LIMIT $limitvalue, $limit ");
if ($query == FALSE) { die ('Query not working! ' . mysql_error()); }
while ($row = @mysql_fetch_array($query)) { $description=$row['Description']; $item_number=$row['Item_Number'];
$query = mysql_query(" SELECT coas50.pro_long_description.Long_Description, coas50.pro_long_description.ID, coas50.pro_long_description_look_up.Description_ID, coas50.pro_long_description_look_up.Item_ID, coas50.pro_item.Item_Number, coas50.pro_item.Description, coas50.pro_item.ID FROM coas50.pro_long_description, coas50.pro_long_description_look_up, coas50.pro_item WHERE MATCH coas50.pro_long_description.Long_Description AGAINST ('%$search%') AND coas50.pro_long_description.ID = coas50.pro_long_description_look_up.Description_ID AND coas50.pro_long_description_look_up.Item_ID = coas50.pro_item.ID LIMIT $limitvalue, $limit");
while ($row = @mysql_fetch_array($query)) { $description=$row['Description']; $item_number=$row['Item_Number'];
//table layout for results //adding javascript to the image creates a link for IE echo "<tr><td><b><a href='"?phpMyAdmin=4594f30712f4fabaff6997416810f3f2 . htmlspecialchars("product_output.php?Item_Number=" .urlencode($item_number)) . "'>$item_number</a></b></td><td>$description</td></tr>"; }
Well I think the easiest way to do it is to use NOT REGEXP.
Grab each ID that you don't want the 2nd query to return. You'll need to create it to work with NOT REGEXP like this.
//put this in your first while statement to grab each id and format for NOT REGEXP $idDontShow .= (!empty($idDontShow)) ? '|'.$row['ID'] : $row['ID'] ; //this will create a format like so "234|56|98734|45985"
In the query you want to not pull the same result sets, put this extra code
MATCH coas50.pro_long_description.Long_Description AGAINST ('%$search%') AND coas50.pro_long_description.ID = coas50.pro_long_description_look_up.Description_ID AND coas50.pro_long_description_look_up.Item_ID = coas50.pro_item.ID AND coas50.pro_item.ID NOT REGEXP '$idDontShow' LIMIT $limitvalue, $limit");
You will need to put quotes around the REGEXP stored in the variable $idDontShow. That should work. Post if you need anymore explaining or help.