我需要在从 Content.php (View/Pages/Items.ctp) 调用的页面 (View/Layouts/default.ctp) 中显示 MySQL 中的一些项目
我怎样才能做到这一点?
最佳答案
这取决于您使用的表的名称。
您可以在 Controller 中使用简单的代码。 (PagesController.php
)
$this->set('data', $this->Model->find('all'));
// change Model for the name of your model.
如果模型没有被Cakephp自动加载。
您需要在上面相同 Controller 的函数中设置它。
$this->loadModel('Model'); // change Model for the name of your model.
在 View (Items.ctp
) 中:
foreach($data as $d){
echo $d['Model']['name'];
// change Model for the name of your model.
}
关于php - Cakephp在default.ctp处显示数据mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19892862/