Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying 0 results in CodeIgniter query

I feel like this is a stupid question, but I cannot find good documentation that explains what I am looking for.

If I query the database in my Model, and pass the results array to my View, where do I state that if there are 0 results print "There are no results to display?"

like image 831
khejduk Avatar asked Nov 03 '25 13:11

khejduk


1 Answers

If you want to display text to the user, that's probably best done in the view.

Assuming you are passing the result() array to the view, you can check whether it's empty or not (i.e. has no records):

if(empty($query->result())){
    // no records to display
} else {
    // records have been returned
}

Otherwise, you can check via the num_rows() method if you're dealing with the db object as a whole (and not just result()):

if($query->num_rows() > 0){
    // records have been returned
} else {
    // no records
}
like image 172
Colin Brock Avatar answered Nov 05 '25 15:11

Colin Brock