Internet Explorer 6 is not supported.
Upgrade to view this site.

Blog Archive

Results for May, 2011.

Looping with Nested Views in CodeIgniter

I have been using CodeIgniter on my most current project and I am really happy with the way it works. If you know anything about MVC and OOP, there is very little learning curve to get moving with this framework.

CodeIgniter allows you to create views to display information passed in from the controller and the models of the application. Usually if you are sending arrays of information to display you will have to loop over the array. I like to write as little markup as possible so I nest views of markup that are reusable. I came across a problem where I wanted to load a view in the middle of a loop and have that nested view be aware of where it is in the loop. I needed the information from that position in the loop. The nested views have access to the variables sent by the controller, but doesn’t keep track of foreach iteration variables.

Here is the solution that I cooked up to get that information over to the nested view. Instead of using a foreach loop I use just a for loop and pass the iterator over in the view’s data parameter. Below is pseudocode of how this might look.

load->view('nestedview', array('i' => $i)); ?>