Codeigniter 3:分页仅显示前10条记录

时间:2017-08-16 19:18:49

标签: php codeigniter pagination

我试图在不使用框架的分页库的情况下对 Codeigniter 3 应用程序进行分页。更确切地说,我想对一个包含大约100行的表进行分页。为此目的:

在家庭控制器中我有:

public function index() {
    $this->load->model('Customer');
    $this->load->library('pagination');
    $config = [
        'base_url' => base_url("index.php"),
        'per_page' => 10,
        'total_rows' => $this->Customer->get_num_rows(),
        'uri_segment' => 3,
        'first_tag_open' =>  '<li>',
        'first_tag_close' => '</li>',
        'last_tag_open' =>  '<li>',
        'last_tag_close' => '</li>',
        'full_tag_open' =>  '<ul class="pagination">',
        'full_tag_close' => '</ul>',
        'next_tag_open' =>  '<li>',
        'next_tag_close' => '</li>',
        'prev_tag_open' =>  '<li>',
        'prev_tag_close' => '</li>',
        'num_tag_open' =>   '<li>',
        'num_tag_close' =>  '</li>',
        'cur_tag_open' =>   '<li class="active"><a>',
        'cur_tag_close' =>  '</a></li>',
    ];
    $this->pagination->initialize($config);
    $customers = $this->Customer->getCustomers($config['per_page'], $this->uri->segment($config['uri_segment']));
    $this->load->view('home', ['records'=>$customers]);
}

在Model文件中我有:

class Customer extends CI_Model {
 public function __construct() {
    $this->load->database();
 }

 public function getCustomers($limit, $offset) {
    $this->db->limit($limit, $offset);
    $query = $this->db->get('customers');
    return $query->result();
 }
}

最后,观点:

<div class="pagination-container text-center">
    <?php echo $this->pagination->create_links(); ?>
</div> 

显示分页并正确格式化; 前10条记录也是如此: enter image description here

但如果我点击第2页或任何其他页面,我会得到相同的前10条记录,如下图所示:

enter image description here

第2页的网址是:

http://localhost/cicrud/index.php?page=2

我做错了什么,我无法理解。有帮助吗?谢谢!

5 个答案:

答案 0 :(得分:1)

也添加这些配置行......

$config['page_query_string'] = TRUE;
$config['query_string_segment'] = 'page';
$config['display_pages'] = TRUE;
$config['use_page_numbers'] = TRUE;

答案 1 :(得分:0)

因此,index()函数必须如下所示:

public function index() {
    $this->load->model('Customer');
    $this->load->library('pagination');
    $config = [
        'base_url' => base_url("index.php"),
        'page_query_string' => TRUE,
        'query_string_segment' => 'page',
        'display_pages' => TRUE,
        'use_page_numbers' => TRUE,
        'per_page' => 10,
        'total_rows' => $this->Customer->get_num_rows(),
        'uri_segment' => 3,
        'first_tag_open' =>  '<li>',
        'first_tag_close' => '</li>',
        'last_tag_open' =>  '<li>',
        'last_tag_close' => '</li>',
        'full_tag_open' =>  '<ul class="pagination">',
        'full_tag_close' => '</ul>',
        'next_tag_open' =>  '<li>',
        'next_tag_close' => '</li>',
        'prev_tag_open' =>  '<li>',
        'prev_tag_close' => '</li>',
        'num_tag_open' =>   '<li>',
        'num_tag_close' =>  '</li>',
        'cur_tag_open' =>   '<li class="active"><a>',
        'cur_tag_close' =>  '</a></li>'
    ];
    $this->pagination->initialize($config);
    $customers = $this->Customer->getCustomers($config['per_page'], $this->input->get('page'));
    $this->load->view('home', ['records'=>$customers]);
}

希望我的回答对其他人有用。非常感谢所有帮助。

答案 2 :(得分:0)

尝试更改

 'uri_segment' => 3,

 'uri_segment' => 1,

答案 3 :(得分:0)

如果您使用GET方法,则必须添加$this->input->get('page'/*your query string segment*/);而不是$this->uri->segment($config['uri_segment']);.

答案 4 :(得分:0)

为了使您的功能更易于阅读,我修改了一些变量名称,并创建如下所示的分页:

 /*
      $perpage - is nothing but limit value, no of records to display 
      $page    - is nothing but offset, thats page number 
 */
 public function getCustomers($perpage=10, $page=0) {
        $page = $page-1;
        if ($page<0) { 
            $page = 0;
        }
        /* decides from where to start*/
        $from = $page*$perpage;
        $this->db->limit($perpage, $from);
        $query = $this->db->get('customers');
        return $query->result();
 }