illegal string offset(codeigniter 3)

Viewed 24

Hi been trying to retrieve records from my database, but I keep getting this error "Severity: Warning Message: Illegal string offset " in several fields.

controller

public function get_discount(){
    if($this->session->has_userdata('logged_in')){ 
        // redirect(base_url());
    }
    else{
        $output = array();
        $output['error']="failed";
        $coupon_code=$this->input->post('coupon_code');
        $product_price=$this->input->post('product_price');
                       
        $condition=array('coupon_code'=>$coupon_code,
                         'product_price'=>$product_price
        );
        $count['discount']= $this->Form_model->get_discount('coupon',$condition);

        foreach($count['discount'] as $row)
        {
            echo json_encode($count['discount']);
    
            if($count > 0){
                $discount = $row['discount'] / 100;
                $total = $discount * $product_price;
                $array['discount'] = $row['discount'];
                $array['product_price'] = $product_price - $total;
            
                $output['success']="success";
                echo json_encode($output);
            }
        }
    }
}

this is my model

public function get_discount($data){
    $sql = "SELECT * FROM coupon WHERE (coupon_code = ? ) AND  status= 'valid' ";
    $query = $this->db->query($sql, array($data['coupon_code']));
    $result = $query->row() ; 
    if($query->num_rows()==1){
        return $result;
    }
}

This is my view

<form action="/get_discount" method="post">
  <div class="form-group">
    <h4 class="text-warning">*Optional</h4>
    <label>Coupon Code</label>
    <input class="form-control" name="coupon_code" type="text" id="coupon_code" />
    <input type="hidden" value="<?php echo $product->product_price?>" id="price" />
    <div id="result"></div>
    <br style="clear:both;" />
    <button class="btn btn-primary" id="activate">Activate Code</button>
  </div>
</form>

<div class="form-group">
  <label>Total Price</label>
  <input class="form-control" type="number" value="<?php echo $product->product_price?>" id="total" readonly="readonly" lang="en-150" />
</div>
</div>
</div>
1 Answers

You're calling get_data() with too many arguments. It only $condition should be the only argument.

        $count['discount']= $this->Form_model->get_discount($condition);

The error is because $data is the first parameter, which you're passing the string 'coupon' to in your call.

Related