How can I avoid using foreach in a view? |
[eluser]victorche[/eluser]
I've read a lot about pros/cons of using foreach in a view file. Even according to this thread: http://ellislab.com/forums/viewthread/170579/ Using of foreach is something good ... But let me explain the problem. I am having an array of data, which should be displayed in a view. This data is taken from the database directly but I want to change some of the values, to be ... more human let's say. So the first case is to use foreach in the view like this: Code: <?php foreach ($received as $row): ?> Code: $received = $this->tasks->received_tasks($user_id); It is really hard for me to understand why we have to do the loop in the view files. Can I somehow avoid this? Or how can I use these nice helpers if the loop is in the view? Please, give advice
[eluser]Cristian Gilè[/eluser]
Helpers are available in the view so you can do: Code: <?php foreach ($received as $row): ?> If you don't want the foreach statements in the view you can build the html block in the controller, save it in a variable and pass it in the view: Code: $data = array(); In your view: Code: echo $html_block; I prefer to use foreach in the view. Cristian Gilè |
Welcome Guest, Not a member yet? Register Sign In |