2016-08-22 43 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

谢谢。即使认为这不是确切的答案,这有助于得到答案。 –

相关问题