我正在使用CodeIgniter。我正在从控制器获取数据。
型号代码
$select_filter_status=$parameters['select_filter_status'];
$select_filter_action= $parameters['select_filter_action'];
if(($select_filter_status=='') && ($select_filter_action=='')){
$where_2=" "; // I don't have any condition here. I have to display all the records
}
else if(($select_filter_status=='') || ($select_filter_action=='')){
if ($select_filter_action=='') {
$where_2="product_order.o_order_status=".$select_filter_status;
}
else{
$where_2="product_order.o_order_status_confirm=".$select_filter_action;
}
}
else{
$where_2="product_order.o_order_status=".$select_filter_status." and product_order.o_order_status_confirm=".$select_filter_action;
}
$this->db->select("*");
$this->db->from('tbl_customer');
$this->db->join('tbl_customer_billing', 'tbl_customer.cust_id=tbl_customer_billing.c_b_id', 'LEFT');
$this->db->join('tbl_customer_shipping', 'tbl_customer.cust_id=tbl_customer_shipping.cust_id', 'LEFT');
$this->db->where($com_report);
$this->db->where($where_2); // I want to remove this where clause when there is empty status and action
$this->db->order_by('product_order.o_date_of_added','DESC');
$query = $this->db->get();
$result = $query->result();
我的问题是,查询中有两个where子句,如果发现where condition
和$select_filter_status
为空,则必须删除第二个$select_filter_action
。
有什么方法可以删除where子句吗?或者我必须使用类似的东西
if(){
//with two where clause
}
else{
//with one where clasue
}
答案 0 :(得分:1)
在这种情况下,您只能简单地放置一个。它将为您工作。
if (!empty($where_2){
$this->db->where($where_2);
}
如果$ where_2不为空,它将执行第二个where条件,如果为空,它将忽略第二个where。
或者您可以像这样连接查询字符串以仅使用一个查询子句
$com_report = $com_report . " " . $where_2;
现在不需要第二个where子句 希望它对您有用
答案 1 :(得分:0)
您必须在主查询中传递where_2查询。在$ com_report查询中添加变量$ where_2
if(($select_filter_status=='') && ($select_filter_action=='')){
$where_2=" "; // I don't have any condition here. I have to display all the records
}
else if(($select_filter_status=='') || ($select_filter_action=='')){
if ($select_filter_action=='') {
$where_2="and product_order.o_order_status=".$select_filter_status;
}
else{
$where_2="and product_order.o_order_status_confirm=".$select_filter_action;
}
}
else{
$where_2="and product_order.o_order_status=".$select_filter_status." and product_order.o_order_status_confirm=".$select_filter_action;
}
现在
$com_report ='your where statements ' .$where_2 ;
立即将其传递给查询生成器
$this->db->where($com_report);
我希望它对您有用
答案 2 :(得分:0)
是的,您可以基于Codeigniter中的条件添加where子句,像这样
$this->db->select("*");
$this->db->from('tbl_customer');
$this->db->join('tbl_customer_billing', 'tbl_customer.cust_id=tbl_customer_billing.c_b_id', 'LEFT');
$this->db->join('tbl_customer_shipping', 'tbl_customer.cust_id=tbl_customer_shipping.cust_id', 'LEFT');
$this->db->where($com_report);
if(!empty($select_filter_status) AND !empty($select_filter_action)){
$this->db->where($where_2);
}
$this->db->order_by('product_order.o_date_of_added','DESC');
$query = $this->db->get();
$result = $query->result();