Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/235930-foreach-question/
Share on other sites

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%'

Link to comment
https://forums.phpfreaks.com/topic/235930-foreach-question/#findComment-1212926
Share on other sites

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;

Link to comment
https://forums.phpfreaks.com/topic/235930-foreach-question/#findComment-1212930
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/235930-foreach-question/#findComment-1212935
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.