0

我刚刚熟悉 Codeigniter (2.1.0) 并构建了一个简单的站点,其中有一个产品表和一个位置表。我遵循 CI 文档并成功构建了一个页面来显示每个表中的数据。但我的目标是在主页上显示两组数据。CakePHP 用“元素”很好地处理了这个问题,其中来自不相关控制器的数据可以显示在由不同控制器处理的页面上。看起来很简单,我很难找到一个起点。

这是产品控制器的索引函数:

public function index()
{
$data['product'] = $this->product_model->get_product();
$data['title'] = 'title of page';

$this->load->view('templates/header', $data);
$this->load->view('product/index', $data);
$this->load->view('templates/footer');
}

我尝试通过在产品控制器中创建一个调用该模型的函数来包含位置模型,然后使用 $this->functionName(); 在上面包含该函数。那没有用。关于从哪里开始的任何建议?

4

2 回答 2

1

这是包含模型的程序

  1. 包括模型
  2. 调用特定方法

是的,您可以尽可能包含模型

Public function index()
{
$this->load->model('mymodel');
$this->load->model('mymodel2');
$this->load->model('mymodel3');

$data['product'] = $this->product_model->get_product();
$data['result']    = $this->mymodel->abc();
$data['result2']    = $this->mymodel2->xyz();

$data['title'] = 'title of page';

$this->load->view('templates/header', $data);
$this->load->view('product/index', $data);
$this->load->view('templates/footer');
}
于 2012-08-30T04:11:46.850 回答
0

只需在此方法中加载其他模型

public function index(){
     $this->load->model('products');
     $this->load->model('locations');

     $products = $this->products->index();
     $locations= $this->locations->index();


     $data['products'] = products ;
     $data['locations'] = locations;

     $this->load->view('templates/header', $data);
     $this->load->view('product/index', $data);
     $this->load->view('templates/footer');
}
于 2012-08-30T04:10:42.730 回答