Boldonglen Posted May 9, 2011 Share Posted May 9, 2011 I have a foreach function within my website, it is used as part of a search engine i have created. The code was working and then i restarted my computer and now the code is not working. The code uses the explode function to separate each search term into an array. And then if there is more than one search term it adds a extra line of code onto the SQL query. If anyone could tell me why this is not working that would be a great help. $search_exploded = explode(" ",$search); $x = 0; foreach($search_exploded as $search_each) { $x++; if ($x==1) $construct .= "keywords LIKE '%$search_each%'"; else $construct .= " OR keywords LIKE '%$search_each%'"; } $construct = "SELECT * FROM products WHERE $construct"; Thanks Boldonglen Edit: I forgot to mention that what is happening is when i search for more than one term it is just showing the "SELECT * FROM products WHERE keywords LIKE '%$search_each%'" and not the OR statement. The $search_each is showing both the search results but just taking away the space. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted May 9, 2011 Share Posted May 9, 2011 Change your foreach loop to $searches = array(); foreach($search_exploded as $search_each) { $searches[] = "keywords LIKE '%$search_each%'"; } $construct = "SELECT * FROM products WHERE " . implode(' OR ' , $searches); Quote Link to comment Share on other sites More sharing options...
Boldonglen Posted May 9, 2011 Author Share Posted May 9, 2011 I tried using this code but when i test to see what the queery will look like when using the search terms "test test" i get SELECT * FROM products WHERE keywords LIKE '%testtest%' This is not the queery i need to produce. i would want the queery to look something like SELECT * FROM products WHERE keywords LIKE '%test%' OR keywords LIKE '%test%' Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted May 9, 2011 Share Posted May 9, 2011 If my code is outputting SELECT * FROM products WHERE keywords LIKE '%testtest%' Then I don't think you're using my code correctly. As my code should be generating your query as SELECT * FROM products WHERE keywords LIKE '%test%' OR keywords LIKE '%test%' How I tested my code $search_exploded = explode(' ', 'test test'); $searches = array(); foreach($search_exploded as $search_each) { $searches[] = "keywords LIKE '%$search_each%'"; } $construct = "SELECT * FROM products WHERE " . implode(' OR ' , $searches); echo $construct; Quote Link to comment Share on other sites More sharing options...
Boldonglen Posted May 9, 2011 Author Share Posted May 9, 2011 This is exactly how my code is: $search_exploded = explode(' ',$search); $searches = array(); foreach($search_exploded as $search_each) { $searches[] = "keywords LIKE '%$search_each%'"; } $construct = "SELECT * FROM products WHERE " . implode(' OR ' , $searches); And the output of this when using "test test" is: SELECT * FROM products WHERE keywords LIKE '%testtest%' Have i copied the code wrong? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted May 9, 2011 Share Posted May 9, 2011 What is outputted when you echo $search echo "'$search'"; Quote Link to comment Share on other sites More sharing options...
Boldonglen Posted May 9, 2011 Author Share Posted May 9, 2011 I have realised what the problem is and i am going to have to appologise for wasting your time. I was using the preg_replace() function to try and prevent SQL injection and this was taking the space out of the search term. Again im sorry but thank you for your help. 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.