簡體   English   中英

Codeigniter 多個 where 條件

[英]Codeigniter multiple where condition

我有一個問題,我只想選擇一個特定的行, user_id = $this->session->id ,狀態 = 4 小時,8 小時和在家工作。

編輯:我所有的狀態都是

  • 在家里工作

  • 8小時

  • 4個小時

  • 休假

  • 病假

但發生的事情是它只返回特定 user_id 的所有在家工作,而不包括 4 小時和 8 小時狀態

控制器

$order_by = "id desc";
$where = ['user_id' => $this->session->id,'status' => '4 hours', 'status' => '8 hours','status' => 'Work From Home'];
$this->Crud_model->fetch('record',$where,"","",$order_by);

模型

public function fetch($table,$where="",$limit="",$offset="",$order=""){
    if (!empty($where)) {
        $this->db->where($where);   
    }
    if (!empty($limit)) {
        if (!empty($offset)) {
            $this->db->limit($limit, $offset);
        }else{
            $this->db->limit($limit);   
        }
    }
    if (!empty($order)) {
        $this->db->order_by($order); 
    }

    $query = $this->db->get($table);
    if ($query->num_rows() > 0) {
        return $query->result();
    }else{
        return FALSE;
    }
}

請嘗試以下操作:

控制器:

$order_by = "id desc";
$where = ['user_id' => $this->session->id];
$where_in = ['name' => 'status', 'values' => ['4 hours', '8 hours', 'Work From Home']];
$this->Crud_model->fetch('record',$where,"","",$order_by, $where_in);

模型:

public function fetch($table,$where="",$limit="",$offset="",$order="", $where_in = false){
    if (!empty($where)) {
        $this->db->where($where);   
    }
    if (!empty($limit)) {
        if (!empty($offset)) {
            $this->db->limit($limit, $offset);
        }else{
            $this->db->limit($limit);   
        }
    }
    if (!empty($order)) {
        $this->db->order_by($order); 
    }

    if($where_in)
    {
        $this->db->where_in($where_in['name'], $where_in['values']);
    }

    $query = $this->db->get($table);
    if ($query->num_rows() > 0) {
        return $query->result();
    }else{
        return FALSE;
    }
}

這使用where_in功能來獲取這些status值的所有記錄。

關聯數組方法:

    $array = array('name' => $name, 'title' => $title, 'status' => $status);

    $this->db->where($array); 

 // Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active'

或者如果你想做一些比 = 比較以外的事情

   $array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);

$this->db->where($array);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM