Reputation: 1
I am getting error of " Trying to get property of non-object" my controller code is
public function productDetails($pro_name,$product_id) {
$data['prodRating'] = $this->ProductsModel -> get_one($product_id);
$this->load->view('home',$data);
}
Model Code
function get_one($pro_id){
$query= $this->db->select('ratingid, pro_item_id, pro_total_points, pro_total_rates, proid ')
->from('tblproducts')
->where('proid',$pro_id)
->join('tblprorating','tblprorating.pro_item_id = tblproducts.proid','left')
->get();
return $query->result();
}
view code
<span dir="ltr" class="inline">
<input id="input-<?=$prodRating->proid ?>" name="rating"
<?php if ($prodRating->$pro_total_rates > 0 or $prodRating->pro_total_points > 0) { ?>
value="<?php echo $prodRating->pro_total_points / $prodRating->pro_total_rates ?>"
<?php } else { ?>
value="0"
<?php } ?>
<?php if ($this->session->userdata('userid') == false) { ?>
data-disabled="false"
<?php } else { ?>
data-disabled="<?= $rated ?>"
<?php } ?>
class="rating "
min="0" max="5" step="0.5" data-size="xs"
accept="" data-symbol="" data-glyphicon="false"
data-rating-class="rating-fa">
</span>
What is problem in my code if i use foreach loop then i can solve my problem. But in this code i not want to use the foreach loop. I want to access the fields of database. but i am getting error.
Upvotes: 0
Views: 44
Reputation: 9265
Return return $query->row()
for single item. result()
is for many. Further you should check num_rows()
before attempting to access the result object/array and handle 0 rows properly as it is good practice.
In this case:
if ($query->num_rows() > 0) {
return $query->row();
}
return false;
Upvotes: 1