gabarab Posted August 9, 2008 Share Posted August 9, 2008 Hello. I am currently working on a website, and it needs a search engine. The current code i am using is: if(isset($_REQUEST['search_msgs'])) { if(isset($_REQUEST['searchall'])) { $searching = strtoupper($_POST['searching']); $searching = strip_tags($searching); $searching = trim ($searching); $query = "select * from ****** where upper(message) like '%$searching%' order by posted DESC"; } else { $searching = strtoupper($_POST['searching']); $searching = strip_tags($searching); $searching = trim ($searching); $select_category=$_POST['select_category']; $query = "select * from ****** where message like '%$searching%' AND category='$select_category' order by posted DESC"; } } else { $query="SELECT * FROM ****** where category='$showall' order by posted DESC"; $getmsg = mysql_query("$query") or die(mysql_error() The code works perfectly for single word search..."word" but i need to add the possibility to search by multiple words. Now, if I search for "some word" the search will be made only for "word"... I know it should be something that will include explode() but i don't know how to add to the query every word inserted. Can someone please help me with my problem? Thank you in advance. Quote Link to comment Share on other sites More sharing options...
CaptainChainsaw Posted August 9, 2008 Share Posted August 9, 2008 How about just loop over an array of the keywords adding to the SQL each time round the loop. something like: foreach($keywords as $keyword){ $sql .= " AND keyword LIKE '%$keyword%'"; } Quote Link to comment Share on other sites More sharing options...
dannyb785 Posted August 9, 2008 Share Posted August 9, 2008 if you search for "LIKE '%some word%' " every row that is fetched should have "some word" in it. Are you saying that if a user types "some word" that you want it to search for both "some" and "word" separately? Or do you wanna just make sure the rows searched have both "some" and "word" but not necessarily next to eachother? Quote Link to comment Share on other sites More sharing options...
gabarab Posted August 9, 2008 Author Share Posted August 9, 2008 if you search for "LIKE '%some word%' " every row that is fetched should have "some word" in it. Are you saying that if a user types "some word" that you want it to search for both "some" and "word" separately? Or do you wanna just make sure the rows searched have both "some" and "word" but not necessarily next to each other? I haven't worked with foreach() so far... did not need it, i guess... I want to search for both some and word from the input, but, as you said, not necessarily next to each other. my problem is that i haven't the slightest idea on how to make it. I suppose that foreach($keywords as $keyword){ $sql .= " AND keyword LIKE '%$keyword%'"; } would work...i just don't know how to implement it in to my script Quote Link to comment Share on other sites More sharing options...
CaptainChainsaw Posted August 9, 2008 Share Posted August 9, 2008 I would think that you just need to add the keywords to an array then loop over it as I previously suggested. the preg_split() function might be worth looking at is it will split the keywords for you based on a regex and return an array. Loop over the array and you're in business. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.