Jump to content

codeigniter mulitple LIKE db query using associative array- but all from the sam


Recommended Posts

Hi, I'm trying to query my database using codeigniter's active record class. I have a number of blog posts stored in a table. The query is for a search function, which will pull out all the posts that have certain categories assigned to them. So the 'category' column of the table will have a list of all the categories for that post in no particular order, separated by commas, like so: Politics,History,Sociology.. etc.

 

If a user selects, say, Politics and History, The titles of all the posts that have BOTH these categories should be returned.

 

Right? So, the list of categories queried will be the array $cats. I thought this would work-

 

                foreach ($cats as $cat){

                    $this->db->like('categories',$cat);

                }

 

By Producing this-

 

$this->db->like('categories','Politics'); $this->db->like('categories','History');

 

(Which would produce- 'WHERE categories LIKE '%Politics%' AND categories LIKE '%History%')

 

But it doesn't work, it seems to only produce the first statement. The problem I guess is that the column name is the same for each of the chained queries. There doesn't seem to be anything in the CI user guide about this (http://codeigniter.com/user_guide/database/active_record.html) as they seem to assume that each chained statement is going to be for a different column name. Of course it is not possible to use an associative array in one statement as it would have to contain duplicate keys- in this case every key would have to be 'categories'... Does anyone know how I could do this?

 

Thanks!

 

 

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.