Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keyword Searching

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
1
16
US
This was working but suddenly it isn't and I see nothing changed. I have verified the search form is submitting and it is setting the session from which the search is made. It is getting to the final ELSE, then giving this error five times when searching on a single keyword:

Notice: Undefined offset: 2 in C:\Sites\sitename\functions\site_search.php on line 224

so it is clear that somehow the explode() or sizeof() are misfiring but, as I've not changed anything, I cannot discover what's wrong. Can anyone spot what I've missed?

(I did it as a session so that the appropriate words can be highlighted once one selects and views the details page from the search results.)

Code:
if (isset($_SESSION['KeyString'])) :
	$ReplaceValues = array("\'"," the ","The ");
	$KeyString = trim($_SESSION['KeyString']);
	$KeyString = str_replace($ReplaceValues, "", $KeyString);

	$tok = explode(" ", $KeyString);
	$size = sizeof($tok);
			
	for ($i = 0; $i < $size; $i++);
		if (($i == 0) && ($i == $size-1)) :
			$Where .= " AND (rt.Title LIKE '%".$tok[$i]."%' OR Description LIKE '%".$tok[$i]."%' OR History LIKE '%".$tok[$i]."%' OR KeyWords LIKE '%".$tok[$i]."%' OR KeyWords LIKE '%".$tok[$i]."%' OR CatalogNo LIKE '%".$tok[$i]."%' OR r.ID LIKE '%".$tok[$i]."%')";
		elseif ($i == 0) :
			$Where .= " AND ((rt.Title LIKE '%".$tok[$i]."%' OR Description LIKE '%".$tok[$i]."%' OR History LIKE '%".$tok[$i]."%' OR KeyWords LIKE '%".$tok[$i]."%' OR CatalogNo LIKE '%".$tok[$i]."%' OR r.ID LIKE '%".$tok[$i]."%')";
		elseif ($i == $size-1) :
			$Where .= " AND (rt.Title LIKE '%".$tok[$i]."%' OR Description LIKE '%".$tok[$i]."%' OR History LIKE '%".$tok[$i]."%' OR KeyWords LIKE '%".$tok[$i]."%' OR CatalogNo LIKE '%".$tok[$i]."%' OR r.ID LIKE '%".$tok[$i]."%'))";
		else :
			$Where .= " AND (rt.Title LIKE '%".$tok[$i]."%' OR Description LIKE '%".$tok[$i]."%' OR History LIKE '%".$tok[$i]."%' OR KeyWords LIKE '%".$tok[$i]."%' OR CatalogNo LIKE '%".$tok[$i]."%' OR r.ID LIKE '%".$tok[$i]."%')";
		endif;
endif;
 
Since I could find nothing wrong with the code above and it was written many years ago, I went ahead and rewrote it while also simplifying it considerably. This works:

Code:
if (isset($_SESSION['KeyString'])) :
	$Keywords = explode(" ", trim($_SESSION['KeyString']));
	if (count($Keywords)) :
		foreach($Keywords as $KeyString) :
			$Where .= " AND (rt.Title LIKE '%$KeyString%' OR r.Description LIKE '%$KeyString%' OR History LIKE '%$KeyString%' OR KeyWords LIKE '%$KeyString%' OR KeyWords LIKE '%$KeyString%' OR CatalogNo LIKE '%$KeyString%' OR r.ID LIKE '%$KeyString%')";
		endforeach;
	endif;
elseif (isset($_SESSION['TitleSearch'])) :
	$Where .= " AND (rt.Title LIKE '%".$_SESSION['KeyString']."%')";
endif;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top