我已将我的应用程序核心/ MY_Controller.php更改为可扩展。在我从mysql获取数据之前,我现在得到消息:未定义的变量:记录。我怎样才能再次获取数据? MY_Controller.php:
class MY_Controller extends CI_Controller {
protected $data = array();
function __construct() {
parent::__construct();
}
function render_page($view) {
//do this to don't repeat in all controllers...
$this->load->view('templates/header', $this->data);
//menu_data must contain the structure of the menu...
//you can populate it from database or helper
$this->load->view($view, $this->data);
$this->load->view('templates/footer', $this->data);
}
我的家庭主管:
class Home extends MY_Controller {
function __construct() {
parent::__construct();
}
public function view($page = 'home')
{
$this->load->helper('text');
$this->data['records']= $this->services_model->getAll();
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->render_page('pages/'.$page,$this->data);
}
Services_model:
class Services_model extends CI_Model {
function getAll() {
$q = $this->db->get('services');
if($q->num_rows() > 0){
foreach ($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
}
这是我在home.php视图中出错的地方:
<ul class="blog-medium">
<?php foreach($records->result() as $row): ?>
<li><h1><a href="./post.html"><?php echo $row->title; ?></a></h1></li>
<?php endforeach ?>
错误消息显示:消息:未定义的变量:记录
答案 0 :(得分:1)
在您的控制器中,当您设置$data['records']
$this->data['records']
答案 1 :(得分:0)
我找到了解决方案。在我的家庭控制器中,我使用视图:
$this->render_page('pages/'.$page,$this->data);
但我必须在这里使用它:
class Home extends MY_Controller {
function __construct() {
parent::__construct();
}
public function view($page = 'home')
{
$this->load->helper('text');
$this->data['records']= $this->services_model->getAll();
if ( ! file_exists('application/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->render_page('pages/'.$page,$data);
}
我不知道为什么,但我之前尝过这个。它没有成功,但它现在正在运作。