CodeIgniter Forums
return values - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Best Practices (https://forum.codeigniter.com/forumdisplay.php?fid=12)
+--- Thread: return values (/showthread.php?tid=65318)



return values - tanker456 - 05-29-2016

New to PHP and Codeigniter, but have experience in the Microsoft world (.NET, MSSQL).

I recently agreed to help a friend with a website. He's more of a design person and doesn't understand the backend/db portion, so I agreed to help him We are using Codeigniter as the framework.

Can someone give me some patterns of how to deal with getting data from a table but not getting any results back?

One of the views deal with a user using dropdowns to search for things in a table. I would like to know when there is no result and maybe give a modal saying that there was nothing it found.

Thanks in advance for your help.


RE: return values - PaulD - 05-29-2016

Normally I would have in my model something like

PHP Code:
public function get_something() 
{
     $results FALSE;
     ...
     $query $this->db->get();
     if ($query->num_rows() > 0)
     {
          $results $query->result_array();
     }
     return $results;


And in the controller you can just test if the return value is FALSE or not.

PHP Code:
     $data $this->some_model->get_something();
     if ($data)
     {
          ... deal with data
     
}
     else
     
{
          ... deal with no data
     


Or you can send the data to the view and deal with the FALSE condition in the view.

Hope that helps,

Paul


RE: return values - tanker456 - 05-29-2016

(05-29-2016, 08:33 AM)This is perfect. Thank you so much for the examples!---- PaulD Wrote: Normally I would have in my model something like

PHP Code:
public function get_something() 
{
     $results FALSE;
     ...
     $query $this->db->get();
     if ($query->num_rows() > 0)
     {
          $results $query->result_array();
     }
     return $results;


And in the controller you can just test if the return value is FALSE or not.

PHP Code:
     $data $this->some_model->get_something();
     if ($data)
     {
          ... deal with data
     
}
     else
     
{
          ... deal with no data
     


Or you can send the data to the view and deal with the FALSE condition in the view.

Hope that helps,

Paul