我需要侧栏帮助。我在这里检查了一些解决方案,但无法弄清楚如何解决它。因此这篇文章。
侧边栏列出了所有可用的类别。此数据是从mysql中的类别表中提取的。场景是这样的,我需要在所有页面中显示这个侧边栏。我对侧边栏有一个看法。我现在正在做的是我通过扩展CI_Controller创建了一个自定义核心控制器MY_Controller。我在My_Controller中有一个名为get_all_categories的函数。然后在所有其他页面控制器的索引功能中,我有这个;
$categories = $this->get_all_categories();
$this->load->view('template/header');
$this->load->view('template/sidebar', $categories);
$this->load->view('home/index');
$this->load->view('template/footer');
我不是CI的专家。刚刚学习。所以只是想知道这是否是正确的方法。请指教...
你的方法没有什么问题,但是更常见的是将get_all_categories
函数移动到模型中,自动加载它,然后你可以做$this->somemodel->get_all_categories()
你甚至可以做一个方便的功能,所以你不必重复像页眉和页脚一样所以:
模型:
function template($main_view, $data = array()) {
$categories = $this->get_all_categories();
$this->load->view('template/header');
$this->load->view('template/sidebar', $categories);
$this->load->view($main_view, $data);
$this->load->view('template/footer');
}