CodeIgniter Forums

Full Version: $this->load->vars($array)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Yash[/eluser]
$this->load->vars($array)

i use this as
Code:
function Blog()
    {
        parent::Controller();    
        $pr[]=1;
        $pr[]=2;
        $this->load->vars($pr);
        
    }
    
    function index()
    {
        
        echo $pr;
        
    }

not working ...

Please me how to use this.

El Forum

[eluser]Pascal Kriete[/eluser]
The load->vars function is used to pass variables to views (not to mention it extracts them). What you want here is a class variable.
Code:
class Blog {

    var $pr;

    function Blog()
    {
        parent::Controller();
        $this->pr = array(1, 2);
    }

    function index()
    {
        print_r($this->pr);
    ]
}

El Forum

[eluser]xwero[/eluser]
The load->vars function adds the array to an Loader class variable that is used in the load->view function where the array gets extracted.

So in order to use it you have to load a view file.

If you need a array that is accessible throughout the class you have to do this
Code:
class Blog extends Controller
{
    var $pr = array();

    function Blog()
    {
        parent::Controller();    
        $this->pr=array(1,2);
        
    }
    
    function index()
    {
        
        print_r($this->pr);
        
    }
}

EDIT : inparo beat me that makes it 1-1 this morning Smile

El Forum

[eluser]Yash[/eluser]
lol same coding style.

how to use $this->load->vars($array) ?

El Forum

[eluser]xwero[/eluser]
Code:
function Blog()
    {
        parent::Controller();    
        $pr['one']=1;
        $pr['two']=2;
        $this->load->vars($pr);
        
    }

function index()
    {
        
        $this->load->view('viewname');
        
    }
// viewname.php
<?php echo $one ?> and <?php echo $two ?>

El Forum

[eluser]Yash[/eluser]
ohh thanx I got it now