Welcome Guest, Not a member yet? Register   Sign In
How can I avoid using foreach in a view?
#1

[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): ?>
<span>&lt;?php echo $row['id']; ?&gt;</span><span>&lt;?php echo $row['description']; ?&gt;</span><span>&lt;?php echo $row['time']; ?&gt;</span>
&lt;?php endforeach; ?&gt;
Perfect so far ... but even in this small example, I have 2 values that need to be changed a little. Let me show you the case, if this foreach is made in the controller:
Code:
$received = $this->tasks->received_tasks($user_id);

        if ($received)
        {
            foreach ($received as $row)
            {
                $tasks[] = array(
                    'id' => $row['task_id'],
                    'description' => character_limiter($row['description'], 60),
                    'time' => nice_time($row['time'])
                );
            }
        }
We have 2 things here ... I can use the text helper to cut the description and i can use a little helper which will make the time more human, like "2 weeks ago". How can I do the same if the foreach loop is in the view?
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 Sad


Messages In This Thread
How can I avoid using foreach in a view? - by El Forum - 01-27-2011, 09:20 AM
How can I avoid using foreach in a view? - by El Forum - 01-27-2011, 11:57 AM



Theme © iAndrew 2016 - Forum software by © MyBB