【问题标题】:pagination error undefined variable分页错误未定义变量
【发布时间】:2014-09-30 00:51:28
【问题描述】:

我正在研究分页功能,但它给了我一个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,我不知道怎么解决。

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

【问题讨论】:

  • 你可以在你的view里做var_dump($post)看看有没有数据。
  • 它给了我空值。我知道它没有任何数据,但我不知道如何修复它。我认为控制器代码可能有问题......
  • 试试这个,$qPlace = $this-&gt;db-&gt;get('place',$limit,$offset);

标签: php codeigniter pagination


【解决方案1】:
$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));

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 2018-05-03
    • 2014-04-25
    • 2021-04-10
    • 2011-12-21
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多