Welcome Guest, Not a member yet? Register   Sign In
pass a parameter to the view
#1

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

[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è
#3

[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
#4

[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è
#5

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




Theme © iAndrew 2016 - Forum software by © MyBB