CodeIgniter Forums
pass a parameter to the view - 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: pass a parameter to the view (/showthread.php?tid=39009)



pass a parameter to the view - El Forum - 02-26-2011

[eluser]dianikol85[/eluser]
Hi to all. I'll keep it short

Let's say we have a controller and within it two methods.
1) index
2) call_index

In the index method i load a view. In the call_index i call the index but i want to pass a parameter to the view when i run the call_index method. Here is an example of the code

Code:
class Welcome extends CI_Controller {
    
    function  index() {
        
       $this>load->view('some_view');
    }

    function  call_index() {
      
       $data['data'] = 'hello world';
       $this>index();
    }
}

I don't think so that to load the view again in the call_index is a good practice because i probably have already passed data in the index method. For example a pagination.

How i can pass the $data['data'] in the view within the call_index method.


pass a parameter to the view - El Forum - 02-26-2011

[eluser]Cristian Gilè[/eluser]
Code:
class Welcome extends CI_Controller
{    
    function index($data = '')
    {    
       if(is_array($data) AND count($data) > 0)
           $this->load->view('prova', $data);
       else
           $this->load->view('prova');
    }

    function call_index()
    {
       $data['data'] = 'hello world';
       $this->index($data);
    }
}

in the view:

Code:
...
if(isset($data))
{
    echo $data;
}
...


Cristian Gilè


pass a parameter to the view - El Forum - 02-26-2011

[eluser]dianikol85[/eluser]
is there a way without passing it as a variable in the index(). In my case i have already passed to it the variables of the offset for doing the pagination. So i don't want to view it from the url


pass a parameter to the view - El Forum - 02-26-2011

[eluser]Cristian Gilè[/eluser]
Code:
class Welcome extends CI_Controller
{
    private $data;
    
    function index()
    {    
       if(is_array($this->data) AND count($this->data) > 0)
           $this->load->view('prova', $this->data);
       else
           $this->load->view('prova');
    }

    function call_index()
    {
       $this->data = array();
       $this->data['data'] = 'hello world';
       $this->index();
    }
}


Cristian Gilè


pass a parameter to the view - El Forum - 02-26-2011

[eluser]dianikol85[/eluser]
Thank you Cristian. This is going to work!!