2016-08-22 50 views
1

我已經使用活動記錄在codeigniter中編寫了一個搜索函數。我想要做的是,如果payment_type = 5並且在相同的記錄order_status = 1中,那麼我想省略它。但是可以有記錄,其中payment_type = 5和order_status = 2。如果並且只有兩個條件都滿足,則必須省略該記錄。 (payment_type = 5 AND order_status = 1)。我已經添加了下面的模型功能。我想知道如何使用活動記錄來做到這一點。如果任何人有任何想法,這將是真正有用的。提前致謝。如何在Codeigniter活動記錄中使用where_not_in()和兩個條件?

public function getSalesByStore($from,$to,$status,$filter_stores,$offset, $amount, $limit = '') 
    { 
     $result = array(); 
     $this->db->select("*,a.created AS order_created,store_name, users.name, a.id as orderid, a.payment_type AS payment"); 
     $this->db->from("$this->table a"); 
     $this->db->join('users', "a.customer_id = users.id"); 
     $this->db->join('stores', "a.store_id = stores.id"); 
     $this->db->order_by("a.id", "desc"); 


     if(!empty($status)) 
     { 
      if ($status == 6) 
      { 
       $this->db->where('order_status != 3'); 
      } 
      elseif ($status == 1) 
      { 
       $this->db->where("a.payment_type != 5 AND order_status = $status"); 
      } 
      else 
      { 
       $this->db->where('order_status', $status);  
      } 
     } 

     if(!empty($filter_stores)) 
     { 
      $this->db->where('store_id in ('.$filter_stores.')'); 
     } 
     if(!empty($from)) 
     { 
      $this->db->where('date(a.created) >=', date($from)); 
     } 
     if(!empty($to)) 
     { 
      $this->db->where('date(a.created) <=', date($to)); 
     } 
     if(!empty($amount)) 
     { 
      if ($amount == 1) 
      { 
       $this->db->where("a.total_amount <=", 50); 
      }else if ($amount == 2) 
      { 
       $this->db->where("a.total_amount >=", 50);  
      }  
     } 
     $this->db->limit($limit, $offset); 
     $query = $this->db->get(); 
     if ($query->num_rows() > 0) 
     { 
      foreach ($query->result() as $row) 
      { 
       $row->order_status = $this->get_status($row->order_status); 
       $result [] = $row; 
      } 
     } 

     return $result; 

    } 
+0

答案看這裏的https://www.codeigniter。 com/userguide3/database/query_builder.html#this-for-specific-data for'$ this-> db-> where_in()'和'$ this-> db-> where_not_in()'。 – RanjanaLK

回答

1

使用此代碼

<?php 
public function getSalesByStore($from,$to,$status,$filter_stores,$offset, $amount, $limit = '') 
    { 
     $result = array(); 
     $this->db->select("*,a.created AS order_created,store_name, users.name, a.id as orderid, a.payment_type AS payment"); 
     $this->db->from("$this->table a"); 
     $this->db->join('users', "a.customer_id = users.id"); 
     $this->db->join('stores', "a.store_id = stores.id"); 
     $this->db->order_by("a.id", "desc"); 


     if(!empty($status)) 
     { 
      if ($status == 6) 
      { 
       //$this->db->where('order_status != 3'); 

       $this->db->where_not_in("order_status",'3'); 
      } 
      elseif ($status == 1) 
      { 
       // $this->db->where("a.payment_type != 5 AND order_status = $status"); 

       $this->db->where("order_status","$status"); 
       $this->db->where_not_in("a.payment_type",'5'); 
      } 
      else 
      { 
       $this->db->where('order_status', $status);  
      } 
     } 

     if(!empty($filter_stores)) 
     { 
      // $this->db->where('store_id in ('.$filter_stores.')'); 

      $this->db->where_in('store_id',"$filter_stores"); 
     } 
     if(!empty($from)) 
     { 
      $this->db->where('date(a.created) >=', date($from)); 


     } 
     if(!empty($to)) 
     { 
      $this->db->where('date(a.created) <=', date($to)); 
     } 
     if(!empty($amount)) 
     { 
      if ($amount == 1) 
      { 
       $this->db->where("a.total_amount <=", 50); 
      }else if ($amount == 2) 
      { 
       $this->db->where("a.total_amount >=", 50);  
      }  
     } 
     $this->db->limit($limit, $offset); 
     $query = $this->db->get(); 
     if ($query->num_rows() > 0) 
     { 
      foreach ($query->result() as $row) 
      { 
       $row->order_status = $this->get_status($row->order_status); 
       $result [] = $row; 
      } 
     } 

     return $result; 

    } 
?> 
+0

按照你建議的方式,如果$ status!只有空我可以得到正確的輸出。當狀態爲空且不爲空時,我想獲得正確的輸出。 –

+0

可能是!空將是您的代碼的正確條件 –

1

我沒有檢查這個代碼,但是這可能是你正在尋找

public function getSalesByStore($from,$to,$status,$filter_stores,$offset, $amount, $limit = '') 
{ 
    $result = array(); 
    $this->db->select("*,a.created AS order_created,store_name, users.name, a.id as orderid, a.payment_type AS payment"); 
    $this->db->from("$this->table a"); 
    $this->db->join('users', "a.customer_id = users.id"); 
    $this->db->join('stores', "a.store_id = stores.id"); 
    $this->db->order_by("a.id", "desc"); 
    $this->db->where_not_in("a.payment_type",5); 
    $this->db->where_not_in("order_status",1); 


    if(!empty($status) && !empty($filter_stores)){ 
     if ($status == 6){ 
      $this->db->where_not_in('order_status',3); 
     } 
     else{ 
      $this->db->where('order_status', $status);  
     } 
    } 
    if(!empty($filter_stores)){ 
     $this->db->where('store_id in ('.$filter_stores.')'); 
    } 
    if(!empty($from)){ 
     $this->db->where('date(a.created) >=', date($from)); 
    } 
    if(!empty($to)){ 
     $this->db->where('date(a.created) <=', date($to)); 
    } 
    if(!empty($amount)){ 
     if ($amount == 1){ 
      $this->db->where("a.total_amount <=", 50); 
     }else if ($amount == 2){ 
      $this->db->where("a.total_amount >=", 50);  
     }  
    } 
    $this->db->limit($limit, $offset); 
    $query = $this->db->get(); 
    if ($query->num_rows() > 0) { 
     foreach ($query->result() as $row){ 
      $row->order_status = $this->get_status($row->order_status); 
      $result [] = $row; 
     } 
    } 
    return $result; 
} 
+0

謝謝。即使認爲這不是確切的答案,這有助於得到答案。 –

相關問題