Manage an empty query - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10) +--- Thread: Manage an empty query (/showthread.php?tid=67697) |
Manage an empty query - cybersven - 03-28-2017 Hi, I got a model : PHP Code: public function biens_location() PHP Code: public function location() PHP Code: foreach ($biens as $bien) Everything is Ok, but in the case there is no result I got an error so I would like to check results and show a message on the page something like "There are no properties for rent". How can I achieve this ? RE: Manage an empty query - cybersven - 03-28-2017 Ok I answered myself, I've just modified the controller to test the result true or false and set the correct variables to pass... RE: Manage an empty query - dave friend - 03-28-2017 There is more than one way to handle this. In your case it might be most easily done by not returning FALSE from the model. Why? Because $query->result() returns an empty array when no records are found and that fact can be used in the view. Here's a revised model that potentially will return an empty array. PHP Code: public function biens_location() Your controller is unchanged. On the view your check the var and respond with your message if the var is empty. PHP Code: if(empty$biens)) You could also leave the model as is and revise your controller to conditionally provide an empty array to the view just described. PHP Code: public function location() |