1

In database table there is no rows means getting error Undefined variable: results using codeigniter,in case there is no row or empty table is there means i want to display view page

controller

$data['breview'] = $this->Profile_model->review();
  $this->load->view('supplierreview', $data);

Model

public function review() {

    $this->db->select('*');
    $this->db->from('reviews');
    $this->db->join('supplier_otherdetails', 'supplier_otherdetails.supplierid_fk = reviews.supplier_id');
     $this->db->join('customer_registration', 'reviews.customer_id=customer_registration.id');
     $this->db->join('sub3_category', 'reviews.product_id=sub3_category.id');
    $this->db->where('supplierid_fk', $this->session->id);
    $query = $this->db->get();
    if ($query->num_rows() > 0) {

        $results = $query->result();
    }

    return $results;
}

view page

<?php
            foreach ($breview as $row) {
                ?>
                    <div class="reviewsection">

                        <img src="<?php echo 'data:image;base64,' .$row->product_image; ?>" class="img-circle img-user" alt="" width="50px;" height="50px;"/>           
                        <span class="starfont"><botton class="btn btn-danger"> <?php echo $row->ratings; ?> &nbsp;&nbsp;<span class="fa fa-star starfont"></span></botton> </span>
                        <div class="content-left"> 
                            <p><b>Product Name:<?php echo $row->product_name; ?></b></p>

                            <p><?php echo $row->review_msg; ?></p>   


                         <?php  $buyer_review = strtotime($row->review_date);?>
                            <?php $date=date('d-F-Y',$buyer_review); ?>

                            <p>Buyer Name : <?php echo $row->first_name; ?>  <?php echo $date ; ?></p>
                        </div>
                    </div>
            <?php } ?>
Maruthi Prasad
  • 170
  • 1
  • 12
  • 2
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Jonnix May 03 '17 at 12:38
  • 1
    Define $results as an empty array before your num_rows check. – aynber May 03 '17 at 12:39

4 Answers4

2

$results is not defined.

Please add $results = [];

Here you go:

$results = []; 
if ($query->num_rows() > 0) {

    $results = $query->result();
}
return $results;
Dave
  • 3,073
  • 7
  • 20
  • 33
informer
  • 821
  • 6
  • 18
1

In my model, in most cases, i do the following

public function my_function() {
    //$qry = YOUR_QUERY

    if ($qry->num_rows() > 0) //or ==1 or whatever, depends on your structure
        return $qry->result_array(); // or row_array, depends on your structure
    return FALSE;
}

Then in your controller you can check if the result is FALSE or EMPTY like:

$result_from_model = $this->my_model->my_function();

if($result_from_model && !empty($result_from_model)) {
   //your code here
}
GeorgeGeorgitsis
  • 1,262
  • 13
  • 29
1

You can use the following code :

return (is_array($results)?$results:array()); 

Hope it works.

Tristup
  • 3,603
  • 1
  • 14
  • 26
1

All the answers given so far are good. Here is yet another way to do it. It's essentially the same as the accepted answer only using a ternary instead of if.

    $query = $this->db->get();
    return $query->num_rows() > 0 ? $query->result() : array();
}
DFriend
  • 8,869
  • 1
  • 13
  • 26