CodeIgniter Forums

Full Version: How to pass array value to a view page that is loaded into another view page?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2

El Forum

[eluser]developer10[/eluser]
Hello to everyone!

I'm new to CI but i must confess that i really liked it.

I'm trying to turn my existing site (http://www.poslovnicentar.ba) into a CI site.

The first obstacle in achieving that goal is how to pass an array (from a controller) to a view page that is loaded into another view page?

Here's explanation:

In my controller Show i load view page show_view and pass $data to it. But, in a show_view.php is loaded leftsection_view (as a part of page template). I need to use some PHP code in the latter to generate categories list. Since i want my code to be as clean as possible (thats the main reason i switched to CI) i don't like the idea of using standard PHP in leftsection_view.php ITSELF.


So, does anyone have a suggestion?

Denis K.
Bosnia H.

El Forum

[eluser]Sarfaraz Momin[/eluser]
Well the best thing I do is load the leftsection_view in the controller into an array variable. something like this.

controller
Code:
$data['leftsection'] = $this->load->view('leftsection_view',$leftdata);
$this->load->view('show_view',$data);

view
Code:
$leftsection

I hope this makes sense.

Good Day !!!

El Forum

[eluser]developer10[/eluser]
[quote author="Sarfaraz Momin" date="1215210052"]Well the best thing I do is load the leftsection_view in the controller into an array variable. something like this.

controller
Code:
$data['leftsection'] = $this->load->view('leftsection_view',$leftdata);
$this->load->view('show_view',$data);

view
Code:
$leftsection

I hope this makes sense.

Good Day !!![/quote]


well, seems like it DOES make sense! I'll try it and post again to inform you did it work Smile

thanks anyways, bro'!

El Forum

[eluser]developer10[/eluser]
[quote author="Sarfaraz Momin" date="1215210052"]Well the best thing I do is load the leftsection_view in the controller into an array variable. something like this.

controller
Code:
$data['leftsection'] = $this->load->view('leftsection_view',$leftdata);
$this->load->view('show_view',$data);

view
Code:
$leftsection

I hope this makes sense.

Good Day !!![/quote]


hmm, it returns an error message (along with the page content which is not rendered properly): Undefined variable "leftsection" (variable from your example) in my show controller file.. Do you know why? I only added before all of your code this:

Code:
$data['leftdata'] = "Some text";

to serve as a variable which i would try to use in my leftsection_view.

El Forum

[eluser]developer10[/eluser]
will anyone give me a hand here, please?

El Forum

[eluser]Pascal Kriete[/eluser]
The data that you pass to the view you load is available to all nested views.

Code:
// Controller
$data['something'] = 'Wohoo!';
$this->load->view('outer', $data);

// Outer view
<html>
<? $this->load->view('inner') ?>
</html>

// Inner view
<? echo $something; ?>

El Forum

[eluser]Scriptor[/eluser]
Yes, but it's nicer to keep the view loading in the controller.
Should be simple, with Momin's original code, change the first view->load line to:
Code:
$data['leftsection'] = $this->load->view('leftsection_view',$leftdata, TRUE);
Sending the 3rd paramter as TRUE makes it return the view's contents.

El Forum

[eluser]developer10[/eluser]
[quote author="Scriptor" date="1215425012"]Yes, but it's nicer to keep the view loading in the controller.
Should be simple, with Momin's original code, change the first view->load line to:
Code:
$data['leftsection'] = $this->load->view('leftsection_view',$leftdata, TRUE);
Sending the 3rd paramter as TRUE makes it return the view's contents.[/quote]


inparo, what do you say about this? IS THIS A BETTER SOLUTION?

El Forum

[eluser]Noinx[/eluser]
I always load the view data like this:
Code:
$this->load->vars($data);
$view['body'] = $this->load->view('myview', '', TRUE);
$view['left'] = $this->load->view('myleftview', '', TRUE);
$this->load->view('mytemplate', $view);
By using the load->vars(), the data is available to all views.

El Forum

[eluser]Pascal Kriete[/eluser]
[quote author="cold_fusion" date="1215444659"]
inparo, what do you say about this? IS THIS A BETTER SOLUTION?[/quote]

It's a different solution.

I personally use both of them. To me it always feels like a waste of memory to load a large view into a variable first. On the other hand, I wouldn't use my own solution to nest views beyond one level. It depends on the situation.
Pages: 1 2