Codeigniter 当我点击分页的下一页时,404错误运行

Codeigniter 当我点击分页的下一页时,404错误运行,codeigniter,pagination,codeigniter-2,Codeigniter,Pagination,Codeigniter 2,我试图在CodeIgniter中进行分页,但我想我做错了什么 分页链接显示在页面底部。好啊 当我单击分页链接上的数字时,出现了错误404。这是我无法解决的问题 我的代码如下 blog_模型 function posts($cat_id='') { $config['base_url'] = 'http://example.com/blog'; $config['total_rows'] = 3; $config['per_page'] = 1;

我试图在CodeIgniter中进行分页,但我想我做错了什么

分页链接显示在页面底部。好啊 当我单击分页链接上的数字时,出现了错误404。这是我无法解决的问题 我的代码如下

blog_模型

function posts($cat_id='')
{
    $config['base_url']     = 'http://example.com/blog';
    $config['total_rows']   = 3;
    $config['per_page']     = 1;
    $config['num_links']    = 20;

    $this->pagination->initialize($config);

    if($cat_id) $this->db->where('cat_id',$cat_id);
    $query = $this->db->get('posts', $config['per_page'], $this->uri->segment(3));
    return $query->result();
}
博客浏览

<?php echo $this->pagination->create_links(); ?>

CI中的分页可能会变得棘手。分页只是构造链接,您仍然需要正确地构造数据库查询的限制和偏移量

必须从URL将页码传递到函数中

function posts($cat_id='', $page = FALSE)
{
    $config['base_url']     = 'http://example.com/blog';
    $config['total_rows']   = 3;
    $config['per_page']     = 1;
    $config['num_links']    = 20;
    $config['uri_segment']  = 3; // <-  verify this one

    $this->pagination->initialize($config);

    // set the page to 1 if the page is missing from the URL
    $page   = ($page === FALSE ? 1 : $page); 

    // calculate the offset for the query
    $offset = ($page - 1 ) * $config['per_page'];

    if($cat_id) $this->db->where('cat_id',$cat_id);
    $query = $this->db->get('posts', $config['per_page'], $offset);
    return $query->result();
}
根据评论编辑:


我也不确定你的第3段的页码位置是否正确。如果是/blog/posts/cat_id/1,则偏移的页码位于段号4。

CI中的分页可能会变得棘手。分页只是构造链接,您仍然需要自己正确构造限制和偏移量。把精力集中在如何构造查询上,而不是分页上。好的!我会集中精力的。我会在网上寻找更多关于你建议的信息。也许这就是我作为jr开发人员所需要的。我也不确定你的第3段的页码位置是否正确。如果是/blog/posts/cat_id/1,那么您的偏移量所在的页码就是段号4。谢谢!这个建议救了我。我发布的代码在我看来是完全正确的。因为我在$config['base_url']=上更改了路由;而且我还改变了段的数量,这是可行的。现在看到的链接是example.com/blog/index/1实际上问题出在路由上。我是在你第二次给我关于分段的建议后发现的。在我用blog/index/等函数的名称更改了base_url之后,我还更改了段的数量,这就成功了!真的谢谢你。我会一直关注你关于构造的第一条建议。@Bari,是的,不看你的URL和路由很难知道。我已经更新了我的答案,以便对其他人更有用。之后,我将在发送问题之前提供更多详细信息:这是我的错。对不起。