codeigniter侧边栏问题
我想将一些数据传递到我的侧边栏,但我不知道如何做到最佳实践,
我有一个像这样的模板文件:
<?php
$this->load->view('include/header');
$this->load->view($main_content);
$this->load->view('include/footer');
?>
我制作了一个像这样的帮助文件:
<?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;
}
然后我想我需要转到我的 header.php 文件并在那里做一些事情来获取 foreach 中的数据,但我不知道如何。
希望有人能帮我致以
最诚挚的问候 Sim
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将数据作为数组传递
然后只需在视图中
echo $mykey
您可以传递多个数据,例如
$data['sidebar']='mysidebar';
,如果您还是不清楚请在下面评论。如需了解更多信息,http://codeigniter.com/user_guide/general/views.html 滚动至“将动态数据添加到视图”
编辑
保持助手与您在问题中描述的相同。
将其写入控制器中
将其写入视图中
Pass the data as an array
Then simply
echo $mykey
in the viewYou 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
Write this in view
您可以加载变量,以便它们在您的所有视图和子视图中成为全局变量。
每当您有任何要在视图中全局使用的数据时,请使用 $this->load->vars($data_array) 。
此方法可以在应用程序中的任何位置多次使用,并将合并您添加的任何数据。
欲了解更多信息,请访问 http://codeigniter.com/user_guide/libraries/loader.html ( $this->load->vars)
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?!)