Jump to content

SQL commas seperated


Canman2005

Recommended Posts

Hi all

 

I have the following QUERY

 

SELECT * FROM `products` WHERE ((`title` LIKE '%Apple%') || (`title` LIKE '%Google%') || (`title` LIKE '%Microsoft%'))

 

Which all works super.

 

But I am adding a form onto the front end which allows people to enter a "keyword" into a form field, I am asking them to seperate keywords with commas, when they submit the form, it will create a variable called

 

$words

 

so that value could be something like

 

$words = "apple, microsoft, google, ebay";

 

or

 

$words = "iphone, ipod";

 

my question is, how can I get PHP to turn that variable into something like

 

((`title` LIKE '%Apple%') || (`title` LIKE '%Google%') || (`title` LIKE '%Microsoft%'))

 

so that I can use it within my query.

 

I did have a thought though, and that was about people adding a comma at the start or end, so the variable could look like

 

$words = ",iphone, ipod";

 

or

 

$words = "iphone, ipod,";

 

or

 

$words = ",iphone, ipod,";

 

So I guess I would need to strip any spaces and commas at the start and end of the variable

 

I have been trying something like

 

str_replace(',','%') || (`title` LIKE '%',$words);

 

but I cannot seem to tweak it so that it works correctly

 

Anyone have any ideas? Maybe my way is going to be too complex

 

Thanks

 

Dave

Link to comment
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.