0

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="&#xf005;" 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.

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for solving the problem. i used your code to solve this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.