简体   繁体   中英

WHERE_IN returns only first match in codeigniter

I am trying to get a list of coupons through ajax when the checkboxes are selected. So everything else is working fine but the query is returning only the first match.

So my query is:

$this->db->from('tbl_coupons');
        if($storeids !=''){
         $ids = array($storeids);
$this->db->where_in('coupon_store', $ids );
        }       
        $this->db->where('coupon_cat', $catid);
        $this->db->where('coupon_status', 'active');
        $query = $this->db->get();

        if ($query->num_rows() > 0) {
            $ds = $query->result_array();}

According to this my SQLquery becomes

SELECT * FROM `tbl_coupons` 
WHERE `coupon_store` IN('1,97') 
AND `coupon_cat` = '16' 
AND `coupon_status` = 'active'

But this query is returning values with coupon_store=1 and no results are coming for coupon_store=97

I checked values for coupon store 97 which exists in that category.

use below way if data exist it will be part of query.

        storeids = explode(',',storeids);
        $ids = array();
        foreach($storeids as $val){
            $ids[] = $val;
        }
        if(!empty($ids)){
            $this->db->where_in('coupon_store', $ids );
        }

hope it will create proper sql query

The query is mostly correct, except at line 2, where you need to make the change as:

WHERE coupon_store IN('1','97')

everything else remains the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM