Reputation: 11
i am getting sql result as array..Then it show me an error like `
trying to get property of non-object in controller in `$rate=$product_det->sale_rate;
This sale_rate
is a field in my table product.I am new to PHP. Can anyone help?
My model:
function get_productDet($item)
{
$this->db->select('*');
$this->db->from('product');
$this->db->where('id',$item);
$res=$this->db->get()->result_array();
return $res;
}
My controller:
function product_det()
{
$item=$this->input->post('item_id');
$quantity=$this->input->post('quantity');
$rate=$this->input->post('rate');
$amount="";
$cgst="";
$sgst="";
$igst="";
$product_det=$this->sale_model->get_productDet($item);
if(!empty($product_det))
{
if($rate=="" || $rate==0)
{
$rate=$product_det->sale_rate;
}
$amount=$rate*$quantity;
$cgst_per=$product_det->CGST;
$sgst_per=$product_det->SGST;
$igst=0;
}
echo $rate."-".$amount."-".$cgst."-".$sgst."-".$igst."";
}
My javascript function:
$.ajax({
url: '<?php echo base_url(); ?>sales/product_det',
data: ({"item_id":item_id,"quantity":quantity,"rate":rate }),
dataType: 'html',
type: 'post',
success: function(data)
{
alert(data);
}
});
Upvotes: 1
Views: 99
Reputation: 9707
Hope this will help you :
In model just Replace
$this->db->get()->result_array();
With
$this->db->get()->row();
Your model should be like this :
function get_productDet($item)
{
$this->db->select('*');
$this->db->from('product');
$this->db->where('id',$item);
$res = $this->db->get()->row();
return $res;
}
Upvotes: 1
Reputation: 3714
Since apparently you only need details of one product.
Change:
$res=$this->db->get()->result_array();
To:
$res=$this->db->get()->row_array();
Then, the result is an array not an object. To access the returned elements do the following instead.
Change:
$product_det->sale_rate;
$product_det->CGST;
$product_det->SGST;
To:
$product_det['sale_rate'];
$product_det['CGST']
$product_det['SGST'];
Upvotes: 1
Reputation: 2352
You are using result_array()
in your sql query.
Then use foreach loop
to show your record like this
if(!empty($product_det))
{
foreach($product_det as $p_det){
if($rate=="" || $rate==0)
{
$rate=$p_det['sale_rate'];
}
$amount=$rate*$quantity;
$cgst_per=$p_det['CGST'];
$sgst_per=$p_det['SGST'];
$igst=0;
}
}
Upvotes: 3