最近再用CI框架,有時候會懵一下,因爲沒太多時間看手冊,所以用到的自己先總結一下,免得到時候自己還得花時間再看,下面貼代碼:
我先用的是
public function get_cands_where( $where, $begin=null, $size=null, $paixu = 'cand_city', $projs = '')
{
if( isset( $begin ) && isset( $size ) ){
$this->db->limit( $size, $begin );
}
$this->db->where( $where );
if (!empty($projs)) {
foreach ($projs as $projkey => $projvalue) {
$this->db->or_where('proj_id', $projvalue['proj_id']);
}
}
$this->db->order_by( $paixu );
$query = $this->db->get( $this->table_name );
$result = $query->result_array();
return $result;
}
SELECT COUNT(*) AS count FROM `tbl_cand` WHERE `cand_phone` = 2147483647 AND `cand_email` = '[email protected]' OR `proj_id` = '100137' OR `proj_id` = '100147'
但我想要的結果是這樣:
SELECT COUNT(*) AS count FROM `tbl_cand` WHERE `cand_phone` = 2147483647 AND `cand_email` = '[email protected]' AND (`proj_id` = '100137' OR `proj_id` = '100147')
so?加兩行就可,很簡單
public function get_cands_where( $where, $begin=null, $size=null, $paixu = 'cand_city', $projs = '')
{
if( isset( $begin ) && isset( $size ) ){
$this->db->limit( $size, $begin );
}
$this->db->where( $where );
if (!empty($projs)) {
$this->db->group_start();
foreach ($projs as $projkey => $projvalue) {
$this->db->or_where('proj_id', $projvalue['proj_id']);
}
$this->db->group_end();
}
$this->db->order_by( $paixu );
$query = $this->db->get( $this->table_name );
$result = $query->result_array();
return $result;
}
$this->db->group_start();
$this->db->group_end();
這倆代表後面的分組開始,也就是左括號,end代表結束,就是右括號
省的自己拼接了,畢竟自己拼的沒人家框架封裝的安全,穩定