Reputation: 6386
I have an array of values that I want to check using the mysql IN() function but how do I use it with codeigniter?
This is what I'm currently doing which obviously doesn't work.
$p = array('page1', 'page2', 'page3','page4', 'page5');
$pages = implode(",", $p);
$sql = "select page from pages where domains_id = ? and page in(?) and is_deleted=0";
$fields = array($domains_id, $pages);
$this->db->query($sql, $fields)
the output of the pages variable comes out to page1,page2,page3,... but the in function needs them seperated like this:
'page1','page2','page3'....
Upvotes: 0
Views: 71
Reputation: 4250
Try this:
$p = array('page1', 'page2', 'page3','page4', 'page5');
$this->db->select('page');
$this->db->from('pages');
$this->db->where('domains_id', $domains_id);
$this->db->where_in('page', $p);
//Note where_in function expects 2nd parameter as array
$this->db->where('is_deleted', '0');
$query = $this->db->get();
For more details: http://ellislab.com/codeigniter/user-guide/database/active_record.html
Upvotes: 0
Reputation: 30170
Start with the outer quotes the implode with ','
$pages = sprintf( "'%s'", implode( "','", $p ) );
Upvotes: 0
Reputation: 219884
<?php
function addQuotes($string)
{
return "'{$string}'";
}
$p = array('page1', 'page2', 'page3','page4', 'page5');
$p2 = array_map("addQuotes", $p);
print_r($p2);
You can then use your implode()
normally.
Upvotes: 1