Welcome Guest, Not a member yet? Register   Sign In
Best practices for passing lots of data to view
#1

[eluser]jprateragg[/eluser]
I have a form I need to display that consists of several select/drop-down fields. When prepare my $data variable, I'm dumping several arrays into it from the model:

Code:
$data['select_options1'] = $this->Model->get_categories1();
$data['select_options2'] = $this->Model->get_categories2();
$data['select_options2'] = $this->Model->get_categories2();
...
$data['select_options8'] = $this->Model->get_categories8();

$this->load->view('form/new', $data);

Is this how everyone else does it? Sometimes I find myself having to create lots of keys to store additional data and just want to make sure I'm doing this right. Thanks!
#2

[eluser]monoclonal[/eluser]
That's an interesting point.

I think that as the 'only' valid way of passing data to views from controllers is via the $data array, that is probably the only option.

That said, instead of using a new $data object for each select_option, i'd use a multidimensional array or an object:

http://ellislab.com/codeigniter/user-gui...views.html
#3

[eluser]jprateragg[/eluser]
I guess I wasn't clear in my example--each of those 'select_options' represents a new array with the contents of the drop-down. So I am passing a large multi-dimensional array to the view.
#4

[eluser]monoclonal[/eluser]
Yes, I understood your post.
If you are worrying about memory limits, then that's a problem for before you pass your data to the view. But I don't think there is a problem with the amount of data passed between controllers and views
#5

[eluser]jprateragg[/eluser]
Ah--that's more reassuring. I'm glad there's no limit to the amount of data that can be passed to the view!
#6

[eluser]Aken[/eluser]
You could pass much more than that through the $data array if you wanted. That's not really a concern, unless your server is severely underperforming.

If you find yourself repeating a lot of the same code, though, that's where a better solution probably lies. Maybe helpers or a MY_Controller would help.
#7

[eluser]LuckyFella73[/eluser]
Though I generally use to send a $data array to my views
I wonder if its kind of "wrong" setting class properties
in a controller and just echo out in the views? Would be
interesting for me to know the disadvantages doing that.
#8

[eluser]CroNiX[/eluser]
Personally I store all options and things of that nature as a serialized array (or json encoded to save space depending on what kind of data it is) in the db. Whenever something gets updated in the backend (an option changes, gets added, etc) I regenerate those options and restore the serialized array in the database.

Then when I need an option (or many different options), it's only a very simple single query to retrieve them all. Then only 1 db call instead of however many.

I like as few hits to the db per page as necessary.




Theme © iAndrew 2016 - Forum software by © MyBB