CodeIgniter Forums
Arrays and views - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Arrays and views (/showthread.php?tid=26611)



Arrays and views - El Forum - 01-18-2010

[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(){
        
        $data['main_content'] = 'create_view';
                $data = array( 'arg1' => 'value1',
                               'arg2' => 'value2',
                               'arg3' => 'value3',
                               'argn' => 'valuen'
                      );
        $this->load->view('includes/template',$data);
    }

This is my template file:

Code:
<?php $this->load->view('includes/header', /*I need some parameters of the array here*/); ?>

<?php $this->load->view($main_content, /*And I also need some parameters of the array here*/); ?>

<?php $this->load->view('includes/footer'); ?>

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!


Arrays and views - El Forum - 01-18-2010

[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?


Arrays and views - El Forum - 01-18-2010

[eluser]alejandronanez[/eluser]
Man if I do this:

Code:
function load_create(){
        
        $data['main_content'] = 'create_view';
                $data = array("arguments" => array(
                               'arg1' => 'value1',
                               'arg2' => 'value2',
                               'arg3' => 'value3',
                               'argn' => 'valuen')
                      );
        $this->load->view('includes/template',$data);
    }

Code:
<?php $this->load->view('includes/header', /*I need some parameters of the array here*/); ?>

<?php $this->load->view($main_content, /*And I also need some parameters of the array here*/); ?>

<?php $this->load->view('includes/footer'); ?>

How can I send that data to my view?

Thanks for your quick response!!!


Arrays and views - El Forum - 01-18-2010

[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)); ?>

<?php $this->load->view($main_content, array('main_content' => $main_content)); ?>

Or, you can use $this->load->vars($data) from the controller, then all views will have $data's variables


Arrays and views - El Forum - 01-18-2010

[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']); ?>


Arrays and views - El Forum - 01-18-2010

[eluser]alejandronanez[/eluser]
So, that will be like this:

Controller

Code:
$data['main_content'] = 'ar_en_nom_view';
        $data['prueba']['arg1'] = 'example1';
        $data['prueba']['arg2'] = 'example2';
        
        $this->load->view('includes/template',$data);

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)); ?>

Confusednake:

I'm new to this, please be patient :'(

Thanks again for helping me


Arrays and views - El Forum - 01-18-2010

[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


Arrays and views - El Forum - 01-18-2010

[eluser]alejandronanez[/eluser]
Oh thanks....I don't know why I was doing this, sorry!

It worked OK!

Thanks a lot guys!