lets say that i hv this view (main)
<body>
lorem epsim
<div table></div>
lorem epsim
</body>
in controller control1.php i do
$this->load->view('header');
$this->load->view('main',$data);
$this->load->view('footer');
Now i need to load content of div=table from another view (tbl.php),which is called from another controller
control2.php
function load_table(){
$data['x']=1;
$this->load->view('tbl.php',$data);
}
tbl.php view
<ul>$x</ul>
how can i do that ?
i tried to load controler 2 from controller 1 and assign the function load_table to variable and pass that to main view, but it didnt work cuz load->view is executed instead of saving output to variable..
Reason:
i need to do this is that tbl.php view is a complex table that i need to refresh and load via ajax calls, so i need it to be on different view alone
so can some one explain to me how can i work this out ?
You can't call one controller method from another, separate controller. You can, however, get the output of the table view and use that:
// main.php
<body>
lorem epsim
<div table><?php echo $table_content; ?></div>
lorem epsim
</body>
.
// control1.php
$table_data['x'] = 1;
$data['table_content'] = $this->load->view('tbl.php', $table_data, TRUE);
$this->load->view('header');
$this->load->view('main',$data);
$this->load->view('footer');
So, you get the data to pass to the tbl.php view and pass that to the load->view method - also passing TRUE
as the third parameter - which will return the contents of that view as a string (instead of outputting that to the browser). Now, you have a $data variable to pass to the main
view with the table html included and you can just echo that out in the main view.
How you get the $data['table_content']
data from the view is up to you. You can create another controller method inside control1.php, you can create a helper file that can load the view into a string and return that, etc.
Maybe you can create a view with the table code within and then you can do inside your div for ajax
<div id="for_ajax">
<?php $this->load->view('table'); ?>
</div>
I've similar needs but mine its like a comments wall for issues.
Inside a view use following in a php block
$CI = &get_instance();
$CI->load->view('view_name');