简体   繁体   English

Codeigniter 分页显示太多链接

[英]Codeigniter Pagination Displaying Too Many Links

Using Codeigniter 3 and PHP I have built a web application to display results from a MySQL database.我使用 Codeigniter 3 和 PHP 构建了一个 Web 应用程序来显示来自 MySQL 数据库的结果。 All is working as expected as in the pagination successfully allows users to navigate pages, and displays the correct number of results per page.一切都按预期工作,因为分页成功地允许用户导航页面,并显示每页正确数量的结果。

The issue I have is that there always seems to be too many links on the pagination navigation bar.我的问题是分页导航栏上的链接似乎总是太多。

For example, I will return 79 records from my database, and display 10 per page.例如,我将从我的数据库中返回 79 条记录,每页显示 10 条。 So, there should only be 8 links displaying in the pagination, right?那么,分页中应该只显示 8 个链接,对吗? Instead I can see 18 links.相反,我可以看到 18 个链接。 The links from 9 to 18 take me to a blank page.从 9 到 18 的链接将我带到一个空白页面。

My controller code is below;我的控制器代码如下;

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);
}

My view is below;我的观点如下;

echo $this->pagination->create_links(); 

My URLs are structured as follows;我的网址结构如下;

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

Any help is appreciated.任何帮助表示赞赏。 Thanks谢谢

I have the exact same problem: displaying too many link pages. 我有一个完全相同的问题:显示太多链接页面。 When it should be just showed 10 pages, it displayed 17 pages with the last 7 empty. 当只应显示10页时,它将显示17页,最后7页为空。 I follow this answer from the other thread: https://stackoverflow.com/a/21657962 . 我从另一个线程遵循这个答案: https : //stackoverflow.com/a/21657962 So I basically copy that and paste it to change my previous config. 因此,我基本上将其复制并粘贴以更改以前的配置。

Here is my previous code that results in error: 这是我以前的代码,导致错误:

$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>';

delete all that and change it with code from the above code: 删除所有内容,并使用上述代码中的代码进行更改:

$config['full_tag_open'] = "<ul class='pagination'>";
$config['full_tag_close'] ="</ul>";
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
$config['next_tag_open'] = "<li>";
$config['next_tagl_close'] = "</li>";
$config['prev_tag_open'] = "<li>";
$config['prev_tagl_close'] = "</li>";
$config['first_tag_open'] = "<li>";
$config['first_tagl_close'] = "</li>";
$config['last_tag_open'] = "<li>";
$config['last_tagl_close'] = "</li>";

then I also happen to have another problem: the result was duplicate, the last page was repeating other data to make a full rows per page. 然后我也碰巧遇到另一个问题:结果重复,最后一页重复其他数据,使每页全行。

my previous code: 我以前的代码:

$data['page'] = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;

I changed it to: 我将其更改为:

$data['page'] = ($this->uri->segment(4)) ? (($this->uri->segment(4) - 1) * $config['per_page']) : 0;

just for the record: my $config['uri_segment'] = 4; 仅作记录:my $config['uri_segment'] = 4;

This issue is because you need to change the following line of code for each page; 此问题是因为您需要为每个页面更改以下代码行;

$config['total_rows'] = // number of pages returned here from db

This needs to store the number of pages you wish to display. 这需要存储您希望显示的页面数。 If you set your pagination configuration within each method then this should be easu enought to do. 如果您在每种方法中都设置了分页配置,则应该很容易做到。 In your example you are doing this; 在您的示例中,您正在执行此操作;

$config['total_rows'] = $this->db->get('item')->num_rows();

Presumably you are returning this on each page which is incorrect. 大概您在每个页面上都返回了此错误信息。

You need to remember two things whenever you are going to implement pagination in Codeigniter: 每当要在Codeigniter中实现分页时,您都需要记住两件事:

First thing Configuration related to page generation which is actually: 与页面生成相关的第一件事配置实际上是:

    // Loads pagination library
    $this->load->library('pagination');

    // @params $url = your controller + method path
    $config['base_url'] = base_url() . $url;

    // @params $totalRows = Total  result found in query
    $config['total_rows'] = $totalRows;

    // @params $perPage = In your case it is 50
    $config['per_page'] = $perPage;

    // @params $segment = This is what you are missing in your code. Segment is the factor from where system reads which page records need to be shown
    $config['uri_segment'] = $segment;
    $this->pagination->initialize($config);

Second Thing Pagination configuration related to designing part: 与设计部分有关的第二个东西分页配置:

$config['full_tag_open'] = '<ul class="pagination  pagination-sm m-t-none m-b-none">';
$config['full_tag_close'] = '</ul>';
$config['prev_link'] = '<i class="fa fa-chevron-left"></i>';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';
$config['next_link'] = '<i class="fa fa-chevron-right"></i>';
$config['next_tag_open'] = '<li>';
$config['next_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>';

$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';

$config['first_link'] = '<i class="fa fa-chevron-left"></i> <i class="fa fa-chevron-left"></i>';
$config['last_link'] = '<i class="fa fa-chevron-right"></i><i class="fa fa-chevron-right"></i>';
$this->pagination->create_links();

This is the running script which I have been using in my projects. 这是我在项目中一直在使用的运行脚本。 Working fine. 工作正常。 You need to check uri_segment you are passing during initialization. 您需要检查初始化期间传递的uri_segment

Let me know if you face any issue. 让我知道您是否遇到任何问题。

you can try datatable.js first you display all record in table assign unique #id to table for identify after this use jquery and create you simple table with datatable which have all the features like search sorting record navigate page load jquery in footer part load datatable.css and datatable.js in footer part code for jquery 您可以尝试使用datatable.js,首先在表中显示所有记录,然后在表中分配唯一的#id以便进行标识,然后使用jquery创建具有数据表的简单表,该表具有所有功能,例如搜索排序记录导航页在页脚中加载jquery,在页脚中加载datatable jQuery的页脚部分代码中的.css和datatable.js

$('#table_id').dataTable(); $('#table_id')。dataTable();

Issue is per_page, total_rows in my case i solved it on :问题是 per_page, total_rows 在我的情况下我解决了它:

$page = 0;
$per_page = 8;
$config["total_rows"] = $this->db->get('item')->num_rows();
$config["total_rows"] = $config["total_rows"]/$per_page;
$config["per_page"] = 1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM