Welcome Guest, Not a member yet? Register   Sign In
Question regarding empty result sets
#1

[eluser]LinkFox[/eluser]
Hello chaps.

I would like to know how you handle your empty resultsets in CI (or the MVC pattern in general)

For example.

The user can search for an item or filter an item

The model goes off and gets the data but finds no matches.

You have your model return false rather than the resultset (This is how I like to do it).

Your controller checks this

if(!result) {
// no reusults
} else {
// results
}

Now my dilemma occurs on how to handle this in the view.

I would say it was against MVC to have things like

if(sizeof($results) > 0) {
// display results
} else {
echo 'no results';
}

I would say this is allowing your view to do application logic when it shouldn't be.

I would like to avoid having separate views for results and no results as this would lead to unacceptable code duplication.

How do/would yo guys handle what is I should imagine a very common occurrence.

Thanks in advance.
#2

[eluser]John_Betong[/eluser]
 
I would:
1. test the results from the model in the controller
2. process the results
3. pass the data to the views:
controller code:
Code:
// 1. test the results from the model in the controller
  $city    = 'New York';
  $object  = $this->my_model->get_array_of_names_from_city($city);
  $results = $object->result(); // error, changed query to object

  // 2. process the results
  $data['customer_names'] = array();
  if (empty($results))
  {
     $data['customer_names'][] = 'No customers available in: ' .$city;
  }else{
     foreach($results as $result)
       $data['customer_names'][] = ''; // add customer name here
     endforeach;
  }
  
  // 3. pass the data to the views:
  $this->load->view('view_invoices', $data, TRUE);
 
 
edit: amended incorrect code
 
#3

[eluser]bigtony[/eluser]
I don't claim to be an MVC guru, but in my opinion there's nothing wrong with putting the conditional test in the view. I see it as "view logic", and it makes more sense to me compared to conditioning in the controller. This is because it's up to the view to determine how data (or the lack of it) should be presented, and is not part of the business logic.

So I would do it along the lines of your original post.
#4

[eluser]obiron2[/eluser]
Sorry bigtony, I don't agree

You can do that where a NULL dataset could be legitimate, but the view is not the place to trap errors.

Your front end programmers should not need to put conditional statements into their views, every piece of data that comes through $data should be defined by the controller.

Having said that, I am guilty of doing it in the view because I needed a quick fix...


Obiron
#5

[eluser]bigtony[/eluser]
[quote author="obiron2" date="1248198184"]Sorry bigtony, I don't agree[/quote]
And you're perfectly entitled to!

[quote author="obiron2" date="1248198184"]You can do that where a NULL dataset could be legitimate, but the view is not the place to trap errors.[/quote]
Agreed, but I don't consider empty result sets to be an error. It's still 'data', just zero data.

[quote author="obiron2" date="1248198184"]Your front end programmers should not need to put conditional statements into their views, every piece of data that comes through $data should be defined by the controller.
[/quote]
The problem with doing it in the controller is that then the controller is effectively telling the view how things should be viewed, and it should be the job of the view to decide how things are viewed.

Here are some examples:

1. You have a table of sales figures and want to display all values in excess of $1,000 in bold red, but other values in normal font. This would be a conditional in the view, and would make no sense in the controller.

2. You have separate views for different devices, e.g. normal screen or mobile device. On the full screen you can say "No records found on the database" whereas on the mobile device you might just want it to say "Empty" to save space. But if you try to set it up in the controller then the controller has to know what each output device is going to do. Far cleaner to just pass the data in and let the views format it as they see fit.

3. On zero results, you might want to show an image and paragraphs of html formatted instructions. Again, let the view do the work. That's what views are for.


The job of the controller is, in an ideal world, ONLY to be the interface between the model(s) and the view(s). It invokes model functions to get or update data (without having to know how the model does it) and invokes views to present that data (without having to know how the view does it).


If you still disagree, please come back - this is just my interpretation of MVC. But I believe it best delineates the purposes of the three elements.




Theme © iAndrew 2016 - Forum software by © MyBB