Welcome Guest, Not a member yet? Register   Sign In
Displaying database results - Where should the looping logic go?
#2

[eluser]Colin Williams[/eluser]
I don't the answer to the latter questions, but I do have bit of an opinion on the main issue.

I don't think loop logic in a view is terrible, but I would reserve it only for properties of the main object the view is responsible for presenting. For example, a page that lists blog posts should call a blog_post view file in a loop.

Code:
$data['posts'] = "";
foreach ($this->blog->posts() as $post(
{
   $data['posts'] .= $this->load->view('blog/post', array('post' => $post), TRUE);
}
$this->load->view('blog/list', $data);

Say that $post object had an array of attached images. I wouldn't find it awful to have the views/blog/post.php file loop through $post->images to display each one

Code:
<ul class="images">
&lt;? foreach ($post->images as $img) : ?&gt;
   <li><image src="&lt;?= $img->src ?&gt;" alt="&lt;?= $img->alt ?&gt;" /></li>
&lt;? endforeach; ?&gt;
</ul>

I know there's no <image> tag, but the forum stripped out the proper img tag.


Messages In This Thread
Displaying database results - Where should the looping logic go? - by El Forum - 08-14-2008, 06:05 PM



Theme © iAndrew 2016 - Forum software by © MyBB