codeigniter mulitple LIKE db query using associative array- but all from the same column name...?

Posted by Inigo on Stack Overflow See other posts from Stack Overflow or by Inigo
Published on 2011-01-17T13:54:14Z Indexed on 2011/01/17 19:53 UTC
Read the original article Hit count: 222

Filed under:
|
|
|
|

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. Does anyone know how I could do this?

Thanks!

edit- 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'...

© Stack Overflow or respective owner

Related posts about query

Related posts about codeigniter