Welcome Guest, Not a member yet? Register   Sign In
Best Practices for No Results from Database Query?
#1

[eluser]kirkaracha[/eluser]
What's the best way to report that a database query doesn't return any results? I've been checking $query->num_rows() in my controllers, setting a message if there aren't any results, and loading a view to display the message. That makes for a lot of redundant code in my controllers. Is there a better/more efficient way of handling this, maybe in MY_Controller or MY_Model?
#2

[eluser]CroNiX[/eluser]
Assuming you are passing the array of results (or no results) why not just check it in the view? You don't need a separate view file for the error do you?
Code:
&lt;?php if(sizeof($results) < 1): ?&gt;
We are sorry, there were no results from your search.
&lt;?php else: ?&gt;
    &lt;?php foreach($results as $result): ?&gt;
        // display results
    &lt;?php endforeach; ?&gt;
&lt;?php endelse; ?&gt;
#3

[eluser]kirkaracha[/eluser]
I'd prefer to only load the view if there's something to display and keep the presentational login in them to a minimum.

The way I have it set up now, I run the query from the controller and load the view if there are results or set a flash_data message and load a shared view to display the message if there aren't any results. That seems cleaner to me than putting the logic in the view files.
#4

[eluser]theprodigy[/eluser]
you can check in your model, and return false if there are no results

Code:
$result = $this->db->get('table');
                          
return ($result->num_rows() > 0) ? $result : false;
Then in your controller
Code:
// ...code...
                
$entries = $this->model->function();
              
if($entries)
{
   // ...load your view...
}
                  
// ...code...
#5

[eluser]kirkaracha[/eluser]
Wouldn't I still have to handle no results in the controller, then?

Code:
if($entries)
{
   // ...load your view...
} else {
// show a message saying there weren't any results
}

That's basically what I'm doing now, except I'm checking $result->num_rows() in the controller.
#6

[eluser]theprodigy[/eluser]
Quote:I’d prefer to only load the view if there’s something to display
Sounds like you're going to have to do some kind of decision making in your controller if you don't want to display the view if no results are found.

The only other option I can see (which I advise AGAINST), is to tighten the coupling between your model and controller, and do something like
MODEL:
Code:
$result = $this->db->get('table');
if($result->num_rows() > 0)
{
    $return['results'] = $result;
    $return['view'] = 'view_name';
}
else
{
    $return['results'] = 'No Results found';
    $return['view'] = 'error_view_name';
}
                          
return ($result->num_rows() > 0) ? $result : false;
CONTROLLER:
Code:
$entries = $this->model->function();
              
$this->load->view($entries['view'], $entries);

but since the title of this post is "Best Practices for No Results from Database Query?", I won't be offering any bad practices (like the one above)




Theme © iAndrew 2016 - Forum software by © MyBB