0

我已经更改了我的应用程序核心/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 ?>

错误消息显示:消息:未定义变量:记录

4

2 回答 2

1

在您的控制器中,您似乎正在设置$data['records']您应该设置的时间$this->data['records']

于 2012-08-02T09:00:19.703 回答
0

我找到了解决方案。在我的 Home 控制器中,我将视图用作:

$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);


        }

我不知道为什么,但我以前试过这个。它没有成功,但它现在正在工作。

于 2012-08-02T13:07:23.543 回答