我是CodeIgniter Framework的新手,我试图用分页显示结果,但我有一个分页变量undefined
。
这是我的模型:
class Campaign_model extends CI_Model {
public function __construct() {
parent::__construct();
}
public function get_campaign_lists($limit, $start) {
$sql = 'SELECT * FROM ch_campaigns LIMIT '.$limit.', '.$start;
$query = $this->db->query($sql);
return $query->result();
}
}
控制器:
class Pagination extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->database();
$this->load->library('pagination');
$this->load->model('campaign_model');
}
public function index() {
//pagination settings
$config['base_url'] = site_url('pagination/index');
$config['total_rows'] = $this->db->count_all('ch_campaigns');
$config['per_page'] = "5";
$config["uri_segment"] = 1;
$choice = $config["total_rows"] / $config["per_page"];
$config["num_links"] = floor($choice);
//config for bootstrap pagination class integration
$config['full_tag_open'] = '<ul class="pagination">';
$config['full_tag_close'] = '</ul>';
$config['first_link'] = false;
$config['last_link'] = false;
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = '«';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '»';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="active"><a href="#">';
$config['cur_tag_close'] = '</a></li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$this->pagination->initialize($config);
$data['page'] = ($this->uri->segment(1)) ? $this->uri->segment(1) : 0;
//call the model function to get the department data
$data['campaignlist'] = $this->campaign_model->get_campaign_list($config["per_page"], $data['page']);
$data['pagination'] = $this->pagination->create_links();
//load the department_view
$this->load->view('campaign_view',$data);
}
}
和查看:
<div class="main-container-footer text-center">
<?php echo $pagination; ?>
</div>
我现在尝试修复此错误一段时间,但仍然没有运气。请帮帮我。
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: pagination
Filename: views/campaign_view.php
Line Number: 26
Backtrace:
File: C:\xampp\htdocs\cheching\application\views\campaign_view.php
Line: 26
Function: _error_handler
File: C:\xampp\htdocs\cheching\application\controllers\Content.php
Line: 11
Function: view
File: C:\xampp\htdocs\cheching\index.php
Line: 315
Function: require_once
答案 0 :(得分:2)
我认为你应该在控制器的方法public function index() {
$data = array();
// [...] index method implementation.
}
的开头添加以下行:
{{1}}