我已经更改了我的应用程序核心/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);
}
服务模型:
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 ?>
错误消息显示:消息:未定义变量:记录