我已經改變了我的應用程序的核心/ MY_Controller.php擴展。在我可以從mysql獲取數據之前,現在我得到消息:未定義的變量:記錄。我怎樣才能再次獲得數據? MY_Controller.php:MY_Controller延伸着的數據顯示(笨)
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 ?>
的ERR或消息顯示:消息:未定義的變量:記錄
您的代碼不作任何意義。這真的是你的代碼?這是什麼:'$ data ['records'] = $ this-class Home擴展了MY_Controller {'?這個:'function __construct(){> services_model-> getAll();'?請修正問題併發布您的實際代碼。 – Mischa 2012-08-02 08:57:20
Mischa,這是一個複製/粘貼錯誤。我現在已經意識到了。謝謝 – ytsejam 2012-08-02 13:00:15