Codeigniter mulitple LIKE query db using an associative array, but all from the same column name ...?

I am trying to query my database using the codeigniter active write class. I have several blog posts stored in a table. The query is for a search function that pulls out all messages that have specific categories assigned to them. Thus, the column “category” of the table will have a list of all categories for this message in a certain order, separated by commas, for example: “Politics, history, sociology”, etc.

If the user selects, say, a policy and history, the names of all posts that have BOTH these categories should be returned.

Correctly? Thus, the list of requested categories will be an array of $ cats. I thought it would work -

                foreach ($cats as $cat){
                    $this->db->like('categories',$cat);
                }

Having produced it -

$ this-> db-> like ('categories', 'policy'); $ This-> db-> how ('categories', 'History');

(which would create the "WHERE categories LIKE"% Politics% 'and the LIKE categories'% History%')

But this will not work, it seems, only produces the first statement. The problem, I think, is that the column name is the same for each of the chained queries. There is nothing in the CI user guide (http://codeigniter.com/user_guide/database/active_record.html) as they seem to assume that each associated statement will be for a different column name, Does anyone know how I can do this to do?

Thank!

edit. , , . ""...

+3
2

MySQL, .

 SELECT * 
 FROM  `TicketUpdates` 
 WHERE content LIKE  '%update%'
 AND content LIKE  '%footprints%';

, :

 $this->db->select('*'); 
 $this->db->from('TicketUpdates');
 $this->db->like('content', 'update');   
 $this->db->like('content', 'footprints');       
 print_r($this->db->get()->result());

"" CI, , :

 $this->db->select('*');
 $this->db->from('tablename');
 foreach($cats as $cat){
    $this->db->like('categories', $cat);
 }
 $result = $this->db->get();
+5

$this->db->select("*")->from($table);

foreach($cats as $single_name)
        $this->db->or_like("categories",$single_name);

$result = $this->db->get();
+3

Source: https://habr.com/ru/post/1785911/


All Articles