Welcome Guest, Not a member yet? Register   Sign In
Pass variable into partial view in loop
#1

[eluser]codex[/eluser]
So what I'm trying to do is reuse a part that I use in different places. I've put that into a view. Thing is the variable $pict isn't available in the view. How do I get the variable to work in there? And also, is this way of doing things efficient?

Code:
foreach ($pictures as $pict)
{
  echo $pict['title'];
  $this->load->view('parts/picture_display_part', $pict, TRUE);
}
#2

[eluser]jedd[/eluser]
I'm confused - my first response is to point out that you need to be putting your $pict into an array, and passing that array to your view - but you've got 400+ posts .. so this seems like something you should already know, so maybe there's something more subtle about your question.

To answer your second question, no, I'd probably have a view partial that took the array proper, and did the loop in there - depends what kind of stuff it is, of course. I don't think view-loading is especially expensive, it just seems neater. Even more so if most of the places you're calling this view partial involves multiple items that you need to loop through. Easy enough to have a loop in your view that copes within single items as well as multiples.
#3

[eluser]codex[/eluser]
No, nothing subtle about my question. I was just not thinking straight. Of course it should be put into an array first, duh. Silly me ;-)

Thanks for your reply!




Theme © iAndrew 2016 - Forum software by © MyBB