为了账号安全,请及时绑定邮箱和手机立即绑定

如何解决codeigniter中的分页问题

如何解决codeigniter中的分页问题

PHP
紫衣仙女 2021-12-24 16:06:54
我的分页限制是 5,每页有 5 个帖子。现在在第 1 页上我有 5 个帖子,但是当我转到第 2 页时,它会在第一页之后跳过 5 个帖子,并从帖子 #11 开始第二页,从第 2 页到结束,所有帖子都正确显示。然后最后一页是空的。(这意味着跳过了第 6 到 10 个帖子)我尝试更改限制 $this->global_limit = 5; 到任何其他号码。问题仍然存在,例如如果我将限制更改为 10 个第一页将显示 10 个帖子,然后跳过第 11 到 20 个帖子并从第 21 页开始第 2 页。    /**     * Code in controller file     */            $this->global_limit = 5;            $this->params['lang_id'] = $this->lang_id;    $this->params['limit'] = $this->global_limit;    $this->params['slug'] = $this->slug;    /**     * Code in Helper file     */function get_blog_posts($params, $category_rewrite){    $CI = &get_instance();    /**     * Count and get all the posts     */    $posts_count = $CI->blog->getPosts($params, TRUE);    $posts = $CI->blog->getPosts($params);    /**     * Pagination     */    $config['base_url'] = $category_rewrite['slug'] . 'page/';    $config['total_rows'] = (int)$posts_count;    $config['per_page'] = isset($params['limit']) ? (int)$params['limit'] : FALSE;    $config['uri_segment'] = 5;    $config['num_links'] = 10;    $config['use_page_numbers'] = TRUE;    $config['suffix'] = $CI->uri->getpath() ? '/?' . $CI->uri->getpath() : '';    $config['first_url'] = $category_rewrite['slug'];    $CI->pagination->initialize($config);    $pagination = $CI->pagination->create_links();    /**     * Code in model file     */  public function getPosts($params, $count = FALSE){    $params = array_merge($this->params, $params);    extract($params);    $this->db->join('blog_posts_langs as bpl', 'bp.id = bpl.post_id', 'left');    $this->db->join('blog_categories as bc', 'bc.id = bp.category_id', 'left');    $this->db->join('blog_categories_multi as bcm', 'bcm.post_id = bp.id', 'left');    $this->db->join('url_rewrite as urwr', 'urwr.url_type = "blog_post" AND urwr.element_id = bp.id AND urwr.lang_id = '. $lang_id, 'left');    if ($category) {        $this->db->where('(bc.id = "' . $category . '" OR bc.parent_id IN (' . $in_categories . ') OR bcm.category_id = '. $category .' )');    }
查看完整描述

2 回答

?
收到一只叮咚

TA贡献1821条经验 获得超5个赞

在模型文件中编辑对我有用。下面是我的代码片段。非常感谢@balakrishnan


      $page = $this->uri->segment(5,1);

        if ($limit) {

            $this->db->limit($limit,($page-1) * $limit);

        }


查看完整回答
反对 回复 2021-12-24
?
偶然的你

TA贡献1841条经验 获得超3个赞

对于分页,


第 1 页:0 - 4(索引从 0 开始)


第 2 页:5 - 9


您需要按如下方式计算起始值


$start = ($page-1) * $limit;

然后它会正常工作。


您需要在控制器文件中添加此代码


$this->params['limit'] = $this->global_limit;

$start = $this->uri->segment(4);

$this->params['start'] = $start

您可能需要检查获取页码或起始偏移量的段值。如果您获得页码,则需要应用上述计算以从页码获取起始偏移量。此外,段号可以根据您的 url 参数而变化。


查看完整回答
反对 回复 2021-12-24
  • 2 回答
  • 0 关注
  • 174 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号