Cakephp在default.ctp上显示数据mysql

时间:2013-11-10 17:39:30

标签: php mysql cakephp

我需要在从Content.php(View / Pages / Items.ctp)调用的Page(View / Layouts / default.ctp)中显示MySQL中的一些项目

我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

这取决于您使用的表的名称。
您可以使用控制器中的简单代码。 (PagesController.php

$this->set('data', $this->Model->find('all'));  
// change Model for the name of your model.

如果Cakephp没有自动加载模型。
你需要在上面的同一个控制器的功能中设置它。

$this->loadModel('Model'); // change Model for the name of your model.

在视图中(Items.ctp):

foreach($data as $d){
   echo $d['Model']['name'];
   // change Model for the name of your model.
}

另请阅读 Cakephp Retrieving Your Data

相关问题