duanlujiaji10335 2016-10-17 10:12
浏览 15
已采纳

如何在视图中检索数据独立于codeigniter中的控制器

I have a sidebar in my site that receive some information from db and I can't use controller for retrieve data because I have different controller and same sidebar. How can I print this data in view page. when I wrote in P.h.P code in the view it shows an error that it cant define variables.

How could I do this?

  • 写回答

3条回答 默认 最新

  • dta25920 2016-10-17 13:33
    关注

    When you find that you need the same code in many different controllers a "custom library" (class) is the perfect choice. Documentation for creating your own libraries is found HERE.

    Controllers should be using models to get data from the database. Custom libraries can also use models just like controllers. Here is a very basic custom library called Sidebar. It depends on a model (sidebar_model) that will not be shown. The purpose of the Sidebar library is to return the variables need by the sidebar_view file.

    File: application/libraries/Sidebar.php

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Sidebar
    {
        protected $CI; // Read the documentation link to see why this is needed.
    
        public function __construct()
        {
            $this->CI = & get_instance();
            $this->CI->load->database();  //only needed if not already done 
            $this->CI->load->model('sidebar_model');
        }
    
        public function get_sidebar_data()
        {
            return $this->CI->sidebar_model->get_sidebar();
        }
    
    }
    

    The library method get_sidebar_data() returns the variables for the view.

    Here is a controller that uses the custom library. It will use the custom library and a view file (not shown) containing HTML for the sidebar.

    File: application/controllers/Main.php

    class Main extends CI_Controller
    {
        function __construct()
        {
            parent::__construct();
            $this->load->library('sidebar'); //can also be autoloaded
        }
    
    
        public function index()
        {
            $data['sidebar'] = $this->sidebar->get_sidebar();
            $this->load
                 ->view('banner')
                 ->view('sidebar_view', $data)
                 ->view('main_view')
                 ->view('footer_view');
        }
    
    }
    

    Any other controller that needs to show the sidebar would use this same pattern.

    This controller loads four different view files and is using "method chaining" which is encouraged. Method chaining executes a tiny bit faster. But the best reason for using it? Less typing.

    The method chaning could also be type like this:

    $this->load->view('banner')->view('sidebar_view', $data)->view('main_view')->view('footer_view');
    

    But, IMO, putting each ->view() on a separate line makes it easier to read.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记