codeigniter sidebar problem
i want to pass some data to my sidebar but i dont know how to do it best practice,
i have a template file like this:
<?php
$this->load->view('include/header');
$this->load->view($main_content);
$this->load->view('include/footer');
?>
i make a helper file like this:
<?php
//Getting all the Reviews and put them on the includes/header.php
function sidebar()
{
//Get the instance of the framework
$CI=& get_instance();
//Getting data from sidebarModel
$CI->load->model('sidebar_model');
$sidebarData = $CI->sidebar_model->getSidebarReviews();
return $sidebarData;
}
and then i think i need to go to my header.php file and do something there to get the data 开发者_如何学运维out in a foreach, but i dont know how.
hope some one can help me out
best regards Sim
Pass the data as an array
$data['mykey']='myvalye';
$this->load->view('include/header',$data);
Then simply echo $mykey
in the view
You can pass multiple data for example $data['sidebar']='mysidebar';
, if you are still not clear then please comment below.
For more http://codeigniter.com/user_guide/general/views.html scroll to "Adding Dynamic Data to the View"
Edit Keep the helper same as you described in question.
Write this in controller
$data['sidebarRow']=sidebar();
$this->load->view('template',$data);
Write this in view
echo $sidebarRow;
You can load variables so they become global in all of your views and subviews.
Use $this->load->vars($data_array) whenever you have any data to be used globally in your views.
This method can be used multiple times anywhere in your application and will merge any data you add.
Read more at http://codeigniter.com/user_guide/libraries/loader.html ($this->load->vars)
Another answer would be to autoload your sidebar helper and then just call that in your view file (your header?!)
$rows = sidebar();
foreach($rows as $row):
// Do something with your sidebar row
endforeach;
精彩评论