使用模板页面从仪表板调用另一个控制器

时间:2019-05-23 08:04:39

标签: codeigniter

我在Codeigniter项目上工作,创建页面模板以加载页眉左菜单和页脚,一切正常,当我尝试菜单中的打开链接时,我想打开另一个控制器。我这样做了,但是当控制器视图打开里面的变量以加载每一行的数据库表不起作用..但是当我在没有模板的情况下打开数据库时加载数据库的控制器工作正常

仪表板控制器

class Dashboard extends CI_Controller{
    protected $data = array();
  function __construct()
  {
    parent::__construct();
    $this->data['pagetitle'] = 'Invoices Manager';
  }
  protected function render($the_view)
{
 $this->data['the_view'] = (is_null($the_view)) ? '' : $this->load->view($the_view,$this->data, TRUE);
 $this->load->view('templates/master_page', $this->data); 
}
    public function home() {
        // $this->load->view('templates/master_page', $this->data);
          $this->render( 'templates/homepage_view');
    }
    public function dashboard() {
        // $this->load->view('templates/master_page', $this->data);
          $this->render( 'dashboard/home');
    }
    public function purchaselist(){
         $this->render('purchase/index');
    }     
}

单独运行良好的采购控制器

class Purchase extends CI_Controller{
     protected $data = array();
     protected $mydata = array();  
         function __Construct()
         {
              parent::__Construct ();
                 $this->load->database(); // load database
                 $this->load->model('Purchase_model'); // load model 
                 $this->mydata['purchase']=null;
         }
    public function index()
        {
                 $query = $this->Purchase_model->getPurchaselist();
                     if($query)
        {
            $mydata['purchase'] =  $query;
        }
            $this->load->view('purchase/index', $mydata);
          //  $this->render( 'purchase/index');
        }
}

当我打电话给dashboard/purchaselist时,他们说

  

消息:未定义的变量:购买

     

文件名:purchase / index.php

     

行号:17

它应该在模板内加载数据库表

2 个答案:

答案 0 :(得分:0)

尝试使用dashboard / index.php / purchaselist或将模式$this->load->model('Purchase_model');加载为全局

答案 1 :(得分:0)

如果null为空,请在购买时通过array或空$query

public function index()
{
   $query = $this->Purchase_model->getPurchaselist();
   if(!empty($query)){
     $mydata['purchase'] =  $query;
   }else{
     $mydata['purchase'] =  array();
   }
   $this->load->view('purchase/index', $mydata);
   //  $this->render( 'purchase/index');
}