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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?