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
https://forums.phpfreaks.com/topic/201860-sql-commas-seperated/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.