【问题标题】:Codeigniter Pagination showing that I am on the last pageCodeigniter 分页显示我在最后一页
【发布时间】:2017-12-17 14:06:58
【问题描述】:

所以这是我以前的。如果我去 ciblog/categories/posts/5/,它会将我带到一个页面,该页面显示所有类别 ID 为 5 的帖子。

这就是我想要做的。我想去 ciblog/categories/posts/5,它会显示 5 个帖子让我们说。然后当我转到 ciblog/categories/posts/5/3 时,它会偏移 3,所以现在我有接下来的 3 个帖子。

目前我有 4 个帖子,仅用于测试。如果我直接通过我的 url 去 ciblog/categories/posts/5 它显示 3 个帖子,如果我去 ciblog/categories/posts/5/3 它显示我的另一个帖子,所以我得到的帖子数量是根据网址正确。

但在底部它总是显示我在第 2 页。当我使用 F12 并查看元素时,我的两个分页链接都显示 ciblog/categories/posts/5 所以它没有在最后添加数字

这是我为此页面的分页所拥有的内容。我使用了 print_r 并且 $config 中的所有内容都是正确的。

public function posts($id, $offset=0){

    $category = $this->category_model->get_category($id);

    if(empty($category))
    {
        show_404();
    }
    $config['base_url'] = base_url().'categories/posts/'.$id;
    $config['total_rows'] = $this->post_model->get_posts_by_category_count($id);
    $config['per_page'] = 3;
    $config['uri_segment'] = 3;
    $config['attributes'] = array('class' => 'pagination-link');
    $this->pagination->initialize($config);

    $data['title'] = $category->name;
    $data['posts'] = $this->post_model->get_posts_by_category($id,$config['per_page'],$offset);

    $this->load->view('templates/header');
    $this->load->view('posts/index', $data);
    $this->load->view('templates/footer');

}

路线:

$route['categories/posts/(:num)/(:num)'] = 'categories/posts/$1/$2';

参考:

我一直在关注这里的教程:https://www.youtube.com/watch?v=WoQTjJepDWM&index=8&list=PLillGF-RfqbaP_71rOyChhjeK1swokUIS 本教程的代码在这里:https://github.com/bradtraversy/ciblog

我现在对此进行补充。我所做的唯一更改如上所示

【问题讨论】:

  • 你能在你的帖子视图中显示什么吗?

标签: php codeigniter codeigniter-3


【解决方案1】:
$config['base_url'] = base_url().'categories/posts/'. $id . '/' . $offset;
$config['uri_segment'] = 4;

因为您的分页取决于偏移量,而不是类别的 id,所以您应该让它找到 uri_segment 4 的偏移量,并且您的 base_url 必须发送一个偏移量变量来查找偏移量。

【讨论】:

  • 当我使用 $config['base_url'] = base_url().'categories/posts/'。 $id 。 '/' 。 $偏移量;最终发生的事情是它会生成一个 ciblog/categories/posts/5/0/3 的 url 我删除了它,只是将 uri_segement 保留为 4 并且它工作。所以它现在可以工作了
  • 是的,对你有好处。如果您像这样使用 uri_segment,则不需要路由来执行此操作,路由只会使您的 url 更短或其他。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-03
  • 2021-01-18
  • 1970-01-01
  • 2017-01-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多