分页错误未定义变量

时间:2014-08-06 22:32:04

标签: php codeigniter pagination

我正在处理分页功能,但它给了我一个undefined variable error。有人可以帮帮我吗?

我的控制器:

class Home extends CI_Controller 
{
    function __construct() 
    {
        // Call the Controller constructor
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->library("pagination");
        $this->load->model('place');
        $this->load->model('place_model');
        $this->load->helper('url');
        session_start();
    }

    function manage()
    {
        $this->load->model('Place_model');
        $per_page = 5;
        $total = $this->Place_model->count_posts();

        $base_url = site_url('home/search');
        $config['base_url'] = $base_url;
        $config['total_rows'] = $total;
        $config['per_page'] = $per_page;
        $config['uri_segment'] = '3';
        $data['posts'] = $this->Place_model->get_posts($per_page, $this->uri->segment(3));
        $this->pagination->initialize($config); 
        $this->load->view('search_result_page', $data);
    }
}

我的模型是place_model

class Place_model extends CI_Model 
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('Place');
        $this->load->database();
    }

    function get_posts($limit = NULL, $offset = NULL) 
    {
        $this->db->limit($limit, $offset);
        $qPlace = $this->db->get('place');
        return $qPlace;
    }

    function count_posts()
    {
        return $this->db->count_all_results('place');
    }
}

我的观点是search_result_page

</div>  
<?php
foreach ($posts->results() as $place):
    echo $place->name, '<br />';
    echo $place->placeID;
endforeach;
?>
</div>  
</pre>   
<?php echo $this->pagination->create_links(); ?>  

它给了我错误:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: posts

Filename: views/search_result_page.php

Line Number: 100

我认为这一行:

$data['posts'] = $this->Place_model->get_posts($per_page, $this->uri->segment(3));

给了我null,我不知道如何解决它。

有人可以帮我解决问题吗?

1 个答案:

答案 0 :(得分:5)

$data['posts'] = $this->Place_model->get_posts($per_page, $this->uri->segment(3));
//change this to 
$data['posts'] = $this->place_model->get_posts($per_page, $this->uri->segment(3));

我也得到了同样的错误,我通过在模型上使用小写字母而不是资本来解决它