TheLoveableMonty Posted February 2, 2009 Share Posted February 2, 2009 Firstly, the offending code... function searchPosts() { global $separator, $postdir, $entries; global $configUser, $configPass; $searchkey = isset($_POST['searchkey'])?$_POST['searchkey']:$_GET['searchkey']; if ($searchkey == ""){ echo "<TD BGCOLOR=\"#2A338D\" VALIGN=\"top\"><FONT COLOR=\"#FFF94A\" FACE=\"trebuchet ms\" SIZE=\"2\">The search you submitted was blank.</FONT></TD></TR>"; } else { echo "<TD BGCOLOR=\"#2A338D\" VALIGN=\"top\"><FONT COLOR=\"#FFFFFF\" FACE=\"trebuchet ms\" SIZE=\"2\">Here are your results for <FONT COLOR=\"#F3B809\">$searchkey</FONT>.</FONT></TD></TR>"; } $i=0; foreach ($entries as $value) { $entry =explode($separator,$value); $title =$entry[0]; $content=$entry[1]; $date1 =$entry[2]; $fileName=$entry[3]; $category=$entry[4]; $postType=$entry[5]; if ($searchkey == ""){ } elseif ((stristr($title,$searchkey)) || (stristr($content,$searchkey))) { echo "<TD BGCOLOR=\"#2A338D\" VALIGN=\"top\">"; echo "<FONT FACE=\"trebuchet ms\" COLOR=\"#F3B809\" SIZE=\"3\"><B>".$title."</B></FONT><br>"; echo "<FONT FACE=\"trebuchet ms\" COLOR=\"#FFFFFF\" SIZE=\"1\">Posted on ".$date1." in <a href=".$_SERVER['PHP_SELF']."?option=viewCategory&category=".$category.">".$category."</a></FONT><BR>"; echo "<FONT COLOR=\"#FFFFFF\" FACE=\"trebuchet ms\" SIZE=\"2\">".$content; if ($_COOKIE["u"] == $configUser && $_COOKIE["p"] == $configPass) { echo "<BR><A HREF=".$_SERVER['PHP_SELF']."?option=editEntry&filename=".$fileName.">Edit Entry</A>"; echo " - <A HREF=".$_SERVER['PHP_SELF']."?option=deleteEntry&filename=".$fileName.">Delete Entry</A>"; } echo "</FONT></TD></TR>"; $i++; } } if ($i == 0) {echo "<TD BGCOLOR=\"#2A338D\" VALIGN=\"top\"><FONT COLOR=\"#FFFFFF\" FACE=\"trebuchet ms\" SIZE=\"2\">There were no matches for your search.<BR><BR><A HREF=\"".$_SERVER['PHP_SELF']."\">Click here</A> to return to the blog index.</FONT></TD></TR>";} } It's extremely messy - I'm in a rush to launch a site, so implimenting the basic design of my website has been my key worry. I was just wondering if anyone knew how I could adjust the search function to search for keywords. I'm thinking splitting the result of the search bar with the space between words as the separator, then saving all split words to an array and searching that way. Unfortunately, I have not the foggiest on how to do it. Again, the script it extremely messy - it's freeware. Remarkably, it's not the messiest part of the script at all... Link to comment https://forums.phpfreaks.com/topic/143503-solved-php-search-form-query-searching-for-more-than-one-word/ Share on other sites More sharing options...
gevans Posted February 2, 2009 Share Posted February 2, 2009 Was just a very quick look, also you shouldn't really expect us to write code but maybe attempted it first yourself and come to us with specific problems. You basically wrote the technicality of the code in english but didn't try doing the code yourself..... If this does work (I'm not saying it will) make sure you look through and understand the few simple additions/changes <?php function searchPosts() { global $separator, $postdir, $entries; global $configUser, $configPass; $searchkeyFull = isset($_POST['searchkey'])?$_POST['searchkey']:$_GET['searchkey']; $searchkeyFull = trim($searchkeyFull); $searchkeySplit = explode(" ", $searchKeyFull); if ($searchkeySplit == ""){ echo "<TD BGCOLOR=\"#2A338D\" VALIGN=\"top\"><FONT COLOR=\"#FFF94A\" FACE=\"trebuchet ms\" SIZE=\"2\">The search you submitted was blank.</FONT></TD></TR>"; } else { echo "<TD BGCOLOR=\"#2A338D\" VALIGN=\"top\"><FONT COLOR=\"#FFFFFF\" FACE=\"trebuchet ms\" SIZE=\"2\">Here are your results for <FONT COLOR=\"#F3B809\">$searchkeyFull</FONT>.</FONT></TD></TR>"; $i=0; foreach ($entries as $value) { $entry =explode($separator,$value); $title =$entry[0]; $content=$entry[1]; $date1 =$entry[2]; $fileName=$entry[3]; $category=$entry[4]; $postType=$entry[5]; foreach ($searchkeySplit as $searchKey) { if ((stristr($title,$searchkey)) || (stristr($content,$searchkey))) { echo "<TD BGCOLOR=\"#2A338D\" VALIGN=\"top\">"; echo "<FONT FACE=\"trebuchet ms\" COLOR=\"#F3B809\" SIZE=\"3\"><B>".$title."</B></FONT><br>"; echo "<FONT FACE=\"trebuchet ms\" COLOR=\"#FFFFFF\" SIZE=\"1\">Posted on ".$date1." in <a href=".$_SERVER['PHP_SELF']."?option=viewCategory&category=".$category.">".$category."</a></FONT><BR>"; echo "<FONT COLOR=\"#FFFFFF\" FACE=\"trebuchet ms\" SIZE=\"2\">".$content; if ($_COOKIE["u"] == $configUser && $_COOKIE["p"] == $configPass) { echo "<BR><A HREF=".$_SERVER['PHP_SELF']."?option=editEntry&filename=".$fileName.">Edit Entry</A>"; echo " - <A HREF=".$_SERVER['PHP_SELF']."?option=deleteEntry&filename=".$fileName.">Delete Entry</A>"; } echo "</FONT></TD></TR>"; $i++; } } } } if ($i == 0) {echo "<TD BGCOLOR=\"#2A338D\" VALIGN=\"top\"><FONT COLOR=\"#FFFFFF\" FACE=\"trebuchet ms\" SIZE=\"2\">There were no matches for your search.<BR><BR><A HREF=\"".$_SERVER['PHP_SELF']."\">Click here</A> to return to the blog index.</FONT></TD></TR>";} } Link to comment https://forums.phpfreaks.com/topic/143503-solved-php-search-form-query-searching-for-more-than-one-word/#findComment-752824 Share on other sites More sharing options...
TheLoveableMonty Posted February 3, 2009 Author Share Posted February 3, 2009 I do apologise. I wasn't looking for someone to rewrite the code, just to place me in the right direction based on what I'd explained I was attempting to do. I am grateful though, and I can see how you've added $searchkeySplit to split the contents of the search bar, thus storing the contents as an array instead of a variable. You then use foreach to search for each item within the variable as $searchkey. I feel like I should've thought harder into that. My problem is I can turn the most simple of tasks into a complex journey. I'll check it now, but thanks again nonetheless. Link to comment https://forums.phpfreaks.com/topic/143503-solved-php-search-form-query-searching-for-more-than-one-word/#findComment-753528 Share on other sites More sharing options...
TheLoveableMonty Posted February 3, 2009 Author Share Posted February 3, 2009 It finally works. After checking and double-checking everything, it was simply the way the variables were declared - there were capitols where there shouldn't have been and such. Thanks dude. Link to comment https://forums.phpfreaks.com/topic/143503-solved-php-search-form-query-searching-for-more-than-one-word/#findComment-753609 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.