php-Codeigniter分页显示结果数和页数

我正在使用Codeigniter 3,并且有一个简单的PHP应用程序.

使用分页类,我想在每个页面的顶部显示以下内容;

Showing x to y of z results

哪里;

x = start row
y - end row
z = total rows

`Showing 1 to 10 of 5213 results.`
`Showing 11 to 20 of 5213 results.`
`etc`

我可以使用$config [‘total_rows’]变量检索总行.虽然不确定其余部分.

我的items.php控制器看起来像这样;

    public function index() {
        $config['base_url'] = '/items/index';
        $config['use_page_numbers'] = FALSE; 
        $config['reuse_query_string'] = TRUE;
        $config['total_rows'] = $this->db->get('item')->num_rows();
        $config['per_page'] = 10;
        $config['num_links'] = 10;
        $config['full_tag_open'] = '<div><ul class="pagination">';
        $config['full_tag_close'] = '</ul></div><!--pagination-->';
        $config['first_link'] = '&laquo; First';
        $config['first_tag_open'] = '<li class="prev page">';
        $config['first_tag_close'] = '</li>';
        $config['last_link'] = 'Last &raquo;';
        $config['last_tag_open'] = '<li class="next page">';
        $config['last_tag_close'] = '</li>';
        $config['next_link'] = 'Next &rarr;';
        $config['next_tag_open'] = '<li class="next page">';
        $config['next_tag_close'] = '</li>';
        $config['prev_link'] = '&larr; Previous';
        $config['prev_tag_open'] = '<li class="prev page">';
        $config['prev_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active"><a href="">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li class="page">';
        $config['num_tag_close'] = '</li>';
        $config['anchor_class'] = 'follow_link';
        $this->load->library('pagination');
        $this->pagination->initialize($config);

        $data = array(
        'items' => $this->items_model->itemList()
        );

        $this->load->view('item_list', $data);
    }

我的网址采用以下格式;

items/index    // displays results 1-10
items/index/10 // displays results 11-20
items/index/20 // displays results 21-30

任何帮助,将不胜感激.
谢谢

解决方法:

$data['z'] = $config['total_rows'];

$data['x'] = (int)$this->uri->segment(3) + 1;

if ($this->uri->segment(3) + $config['per_page'] > $config['total_rows']) {
    $data['y'] = $config['total_rows'];
} else {
    $data['y'] = (int)$this->uri->segment(3) + $config['per_page'];
}

另外,如果需要的页面在范围内,则可以在加载视图之前进行一次检查;如果不是,则应根据需要将访问者重定向到首页或最后一页.

上一篇:php-Codeigniter如何避免重复显示一对多关系


下一篇:使用ajax PHP在每个dowpdown中动态填充Dropdown