开发者

codeigniter and the use of multi index in where(array) function

开发者 https://www.devze.com 2023-04-03 16:23 出处:网络
When i use this in codeigniter it only selects the last array index $array = array(\"status !=\"开发者_Go百科 => \"deleted\", \"status !=\" => \"concept\");

When i use this in codeigniter it only selects the last array index

$array = array("status !="开发者_Go百科 => "deleted", "status !=" => "concept");
$this->db->where($array);

this is the result:

SELECT * FROM (`table`) WHERE `status` != 'concept'

Anyone knows why or knows a better way?


Since you've already figured out the reason for this on your own (you're simply overriding the array key), you should use one of the following options:

You can either pass it all in as a string in the first argument:

$this->db->where("status != deleted AND status != concept")->get('table');

Or you can make 2 separate method calls:

$this->db->where("status !=", "deleted")
         ->where("status !=", "concept")
         ->get('table');

The first one is simpler, but the second one is safer.


The way you do it should work, alternatively you can try this:

$array = array("status !=" => "deleted", "status !=" => "concept");
foreach($array as $k=>$v){
    $this->db->where($k,$v);
}


If the column is the same for all clauses, a better/cleaner choice than multiple where()s would be the where_not_in() method. It does just what it says - creates a NOT IN (..) clause

Sample usage:

$this->db->where_not_in('status', $bad_statuses);
0

精彩评论

暂无评论...
验证码 换一张
取 消