1

我想将数组从控制器传递给视图。我尝试使用下面的代码。我知道这是错误的,但想不出该用什么。find()函数从表中获取所有行,然后我想将这些行作为数组传递给视图。我该怎么做?

<?php
    class Blog extends CI_Controller{

        public function __construct(){
            parent::__construct();
            $this->load->model('blog_model');
        }

        public function index(){

             $data = $this->blog_model->find(); //which gets all entries from table
            $this->load->view('template/header');
            $this->load->view('template/content', $data);
            $this->load->view('template/footer');

        }

        public function create(){
            $this->blog_model->create();


        }

        public function delete(){


        }
    }
    ?>
4

1 回答 1

1
$data = $this->blog_model->find(); //which gets all entries from table
$this->load->view('template/header');
$this->load->view('template/content', $data);
$this->load->view('template/footer');

应该:

$data = array('myvar' => $this->blog_model->find());
$this->load->view('template/header');
$this->load->view('template/content', $data);
$this->load->view('template/footer');

然后在您的视图中访问它:

$myvar

请参阅以获得详尽的解释。

于 2013-01-03T21:03:06.257 回答