我是php和CI的新手。我正在创建自己的Web应用程序,但遇到了分页问题。
我在索引上的主要分页正在毫无问题地发挥作用,但是如果我在其他页面上初始化分页,它会显示链接以及分页,但是当我单击时,页面会刷新,希望显示为/ 6(每页内容)显示从数据库生成的相同内容。
那是我在功能索引中使用的代码:
public function index($offset = 0){
$config['base_url'] = base_url() . '/usernames/index/';
$config['total_rows'] = $this->db->count_all('usernames');
$config['per_page'] = 6;
$config['uri_segment'] = 3;
$config['full_tag_open'] = '<ul class="pagination justify-content-center">';
$config['full_tag_close'] = '</ul>';
$config['attributes'] = ['class' => 'page-link'];
$config['first_link'] = false;
$config['last_link'] = false;
$config['first_tag_open'] = '<li class="page-item">';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = 'Previous';
$config['prev_tag_open'] = '<li class="page-item">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = 'Next';
$config['next_tag_open'] = '<li class="page-item">';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<li class="page-item">';
$config['last_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="page-item active"><a href="#" class="page-link">';
$config['cur_tag_close'] = '<span class="sr-only">(current)</span></a></li>';
$config['num_tag_open'] = '<li class="page-item">';
$config['num_tag_close'] = '</li>';
$config['attributes'] = array('class' => 'page-link');
$this->pagination->initialize($config);
$data['title'] = 'Lastest Usernames';
$data['posts'] = $this->post_model->get_usernames(FALSE, $config['per_page'], $offset);
$this->load->view('templates/header');
$this->load->view('usernames/index', $data);
$this->load->view('templates/footer');
}
这就是我在功能手套中使用的内容:
public function snapchat($offset = 0){
$config['base_url'] = base_url() . '/usernames/snapchat/';
$config['total_rows'] = $this->db->count_all('usernames');
$config['per_page'] = 6;
$config['uri_segment'] = 3;
$config['full_tag_open'] = '<ul class="pagination justify-content-center">';
$config['full_tag_close'] = '</ul>';
$config['attributes'] = ['class' => 'page-link'];
$config['first_link'] = false;
$config['last_link'] = false;
$config['first_tag_open'] = '<li class="page-item">';
$config['first_tag_close'] = '</li>';
$config['prev_link'] = 'Previous';
$config['prev_tag_open'] = '<li class="page-item">';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = 'Next';
$config['next_tag_open'] = '<li class="page-item">';
$config['next_tag_close'] = '</li>';
$config['last_tag_open'] = '<li class="page-item">';
$config['last_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li class="page-item active"><a href="#" class="page-link">';
$config['cur_tag_close'] = '<span class="sr-only">(current)</span></a></li>';
$config['num_tag_open'] = '<li class="page-item">';
$config['num_tag_close'] = '</li>';
$config['attributes'] = array('class' => 'page-link');
$this->pagination->initialize($config);
$data['title'] = 'Lastest Snapchat Usernames';
$data['posts'] = $this->post_model->get_usernames(FALSE, $config['per_page'], $offset);
$filter = "Snapchat";
$filteredData = $this->post_model->get_snapchat($filter);
$data=[
'F_username' => $filteredData->result_array()
];
$this->load->view('templates/header');
$this->load->view('usernames/snapchat', $data);
$this->load->view('templates/footer');
}
这是我在 model 中使用的代码:
public function get_snapchat($filter, $slug = FALSE, $limit = FALSE, $offset = FALSE){
if ($limit) {
$this->db->limit($limit ,$offset);
}
$filter = "Snapchat";
$query = $this->db->get_where('usernames', array('platform' => $filter));
return $query;
}
答案 0 :(得分:0)
这是简单的步骤:
在pagination.php
中创建application/config
,以将分页配置与控制器(https://gist.github.com/mikedfunk/3504432)分开
模型:
/* Your_model.php */
// -------------------
// Get Data With Limit
public function data($start, $limit) {
return $this->db->from('TABLE_NAME')
->limit($start, $limit)
->get();
}
// Total All Rows
public function total() {
return $this->db->count_all_results('TABLE_NAME');
}
// Get First Records
public function first_record($current_page, $per_page) {
return ($current_page - 1) * $per_page;
}
/* Your_controller.php */
// -------------------
public function output() {
// Library
$this->load->library('pagination');
// Pagination Config
$config['base_url'] = site_url('Your/Controller');
$config['per_page'] = 30;
$config['uri_segment'] = 3;
// Starting
$curpage = $this->uri->segment(3);
$start = $this->your_model->first_record($curpage, $config['per_page']);
$limit = $config['per_page'];
// Get Result
$data['result'] = $this->your_model->data($start, $limit);
// Get Total Records
$config['total_rows'] = $this->your_model->total();
// Process Pagination
$this->pagination->initialize($config);
// Prep Data
$data['navigation'] = $this->pagination->create_links();
// Load View (Passing $result & $navigation data)
$this->layout->view('YOUR_VIEW_HERE', $data);
}
您需要调整您的数据库配置:)