我正在进行分页功能,但它给了我一个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);
}
}
Run Code Online (Sandbox Code Playgroud)
我的模型是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');
}
}
Run Code Online (Sandbox Code Playgroud)
我的观点是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(); ?>
Run Code Online (Sandbox Code Playgroud)
它给了我错误:
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: posts
Filename: views/search_result_page.php
Line Number: 100
Run Code Online (Sandbox Code Playgroud)
我想这一行:
$data['posts'] = $this->Place_model->get_posts($per_page, $this->uri->segment(3));
Run Code Online (Sandbox Code Playgroud)
给了我null,我不知道如何解决它.
有人可以帮我弄清楚问题吗?
小智 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));
Run Code Online (Sandbox Code Playgroud)
我也得到了同样的错误,我通过使用模型上的小写字母而不是资本来解决它