Arrays and views |
[eluser]alejandronanez[/eluser]
Hey everyone, I'm using a template in my project and I want to pass an array to the view, here's the code. Part of my controller: Code: function load_create(){ This is my template file: Code: <?php $this->load->view('includes/header', /*I need some parameters of the array here*/); ?> How can I pass these parameters to the view and how can I acces to them when I call the view file? Am I clear? Thanks for helping me guys! 8-/ Bye!
[eluser]bapobap[/eluser]
First off, you are effectively erasing $data['main_content'] by overwriting it with $data = array(.... To send the $data array to the view, use: $this->load->view('includes/header', $data); Then in your view file, use the array key as the variable name, so: <?php echo $arg3; ?> That help?
[eluser]alejandronanez[/eluser]
Man if I do this: Code: function load_create(){ Code: <?php $this->load->view('includes/header', /*I need some parameters of the array here*/); ?> How can I send that data to my view? Thanks for your quick response!!!
[eluser]Colin Williams[/eluser]
Read over http://php.net/explode to understand what happens when you pass an array to a view (although it's pretty damn clear in the User Guide, IMO) Examples: Code: <?php $this->load->view('includes/header', array('main_content' => $main_content)); ?> Or, you can use $this->load->vars($data) from the controller, then all views will have $data's variables
[eluser]bapobap[/eluser]
It should just be: <?php $this->load->view('includes/header', $arguments); ?> then <?php echo $arg3; ?> or to send a single one: <?php $this->load->view('includes/header', $arguments['arg1']); ?>
[eluser]alejandronanez[/eluser]
So, that will be like this: Controller Code: $data['main_content'] = 'ar_en_nom_view'; If I want to pass the 'arg1' and 'arg2' here, I've tried this and say that there's and error View: Code: <?php $this->load->view($main_content, array('prueba'=> $arg1, $arg2)); ?> nake: I'm new to this, please be patient :'( Thanks again for helping me
[eluser]bapobap[/eluser]
I don't know why you are doing this: <?php $this->load->view($main_content, array('prueba'=> $arg1, $arg2)); ?> Try <?php $this->load->view($main_content, $prueba); ?> Then in that view you will have access to: <? $arg1 ?> <? $arg2 ?> etc
[eluser]alejandronanez[/eluser]
Oh thanks....I don't know why I was doing this, sorry! It worked OK! Thanks a lot guys! |
Welcome Guest, Not a member yet? Register Sign In |