CodeIgniter Forums
$this->load->vars($array) - 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: $this->load->vars($array) (/showthread.php?tid=9561)



$this->load->vars($array) - El Forum - 06-30-2008

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


$this->load->vars($array) - El Forum - 06-30-2008

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



$this->load->vars($array) - El Forum - 06-30-2008

[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


$this->load->vars($array) - El Forum - 06-30-2008

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

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


$this->load->vars($array) - El Forum - 06-30-2008

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



$this->load->vars($array) - El Forum - 06-30-2008

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