Welcome Guest, Not a member yet? Register   Sign In
Views and output in a foreach
#1

[eluser]Unknown[/eluser]
Hello everybody !

Well, I'm a new user with CI but with the docs I use CI in a good way, with the MVC approach and other useful things.

But I have a problem with a view and particularly with the TRUE/FALSE option like this: $this->load->view('file_name', $data, true/false) (see http://ellislab.com/codeigniter/user-gui...oader.html for details)

The facts :

My controller calls a view called results, which loads normally.

In this view, there is a foreach like this :

Code:
// [...] html and php code before [...]

            foreach($goods as $item) {

                $data['name'] = '<img src="'.base_url().'l/'.$item['id'].'.png" /><br /> <b>'.$item['value'].'</b>';
                
                $data['desc'] = $this->load->view('quiz/description', $item, TRUE);

                $this->load->view('quiz/form', $data);
            }

// [...] html and php code after [...]

As you can see, there are 2 others views in this code.

The first, the description view is a very small file with gives a little line of HTML code.

This code is put in the $data array.

This array is passed to the form view. And this last view echoes the final result.


Well, that were the facts, now the problem :

The first item of the foreach is done normally. The description is inside the form view.

But the problem comes after the first one, the description echoes before the form view. Like there were no TRUE option at this line : $this->load->view('quiz/description', $item, TRUE);

Is there a solution ? An error in my code ? A output cache problem ? Is that a good view approach too ?

Thanks Smile

PS : sorry for my english, that isn't my first language.
#2

[eluser]Michael Wales[/eluser]
When the 3rd parameter of load->view() is FALSE it sends the output to the browser - this is why only your first one is working.

Load all of your data into the $data array, including the load->view() with TRUE as the 3rd parameter if you want - then call load->view('quiz/form', $data) to send the final output.

That should be the last thing you do in every controller method (well, almost every one).
#3

[eluser]coolfactor[/eluser]
You have 2 $this->load->view() calls INSIDE your foreach() loop. Move the second one outside of it.
#4

[eluser]Unknown[/eluser]
Thanks Smile

Now I call the description view within the model, that works the same way but without problem Smile




Theme © iAndrew 2016 - Forum software by © MyBB