如何在Codeigniter分页中使用页码而不是偏移量?

har*_*ham 5 php mysql pagination codeigniter

我已经阅读了有关Stackoverflow的所有问题,并且没有看到问题的确切答案.我目前正在使用codeigniter分页库来使用限制和偏移量生成我的分页链接:

$config['base_url'] = base_url() . 'explore/featured/index/';
    $config['total_rows'] = $this->Projects_model->get_featured_count();//Count featured projects
    $config['per_page'] = 12;
    $config['uri_segment'] = 4;
    $config['display_pages'] = FALSE;
    $config['next_link'] = 'Older →';
    $config['prev_link'] = '← Newer';
    $this->pagination->initialize($config);
    $limit = $config['per_page'];
    $offset = $this->uri->segment(4);

    $data['pagination'] = $this->pagination->create_links();

    $data['projects'] = $this->Projects_model->get_featured($limit, $offset); //get featured projects from database
Run Code Online (Sandbox Code Playgroud)

上面的代码限制了我的查询的输出值,并使用了我的第4个URI段中包含的偏移量.

如何将以下代码更改为页码??? 似乎是不可能的,因为我将不再能够在我的网址中使用偏移量.

Wes*_*rch 18

在某些时候,CI更新了他们的分页库(最终)适应这个:

$config['use_page_numbers'] = TRUE;

默认情况下,URI段将使用您要分页的项的起始索引.如果您希望显示实际页码,请将其设置为TRUE.

如果您使用的是没有此功能但无法更新整个CI安装的版本,则应该可以直接使用最新的分页库.

要计算数据库偏移量,请使用$limit * $page_number"page_number"是URL中的值(在您的情况下为第4段).


moo*_*ker 5

$limit * $page_number上面提到的并没有返回一个真正的偏移作为起点,并在页码为1时产生问题,因为此时偏移量必须为0.这对我有用:

$offset = ($page_number  == 1) ? 0 : ($page_number * $config['per_page']) - $config['per_page'];
Run Code Online (Sandbox Code Playgroud)