如何使用 codeigniter 分页库回显页码?

发布于 2025-01-07 10:09:54 字数 152 浏览 0 评论 0原文

我正在使用 codeigniter 分页库来对我的搜索结果进行分页。我使用限制和偏移来生成结果。

我想知道除了分页链接之外,如何回显用户当前正在查看的页面编号。

例如;

您当前正在查看 Y 个结果中的第 X 页。

这怎么可能?

I am using the codeigniter pagination library to page my search results. I am using a limit and an offset to generate the results.

I would like to know how, apart from in the pagination links, echo the number of the page the user is currently viewing.

For example;

You are currently viewing page X out of Y results.

How is this possible??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

和影子一齐双人舞 2025-01-14 10:09:54

这也给出了当前页号,

$currentPage = floor(($this->uri->segment(n)/$config['per_page']) + 1);   

其中 n 是段

echo "Current Page:".$currentPage;

This also gives the current page no

$currentPage = floor(($this->uri->segment(n)/$config['per_page']) + 1);   

where n is the segment

echo "Current Page:".$currentPage;
单身狗的梦 2025-01-14 10:09:54

您可以扩展 Pagination 类 (system/libraries/Pagination.php)。

创建一个名为 MY_Pagination.php 的文件,并将其放入 application/libraries/ 中,

将此代码添加到您的扩展分页类中;

class MY_Pagination extends CI_Pagination {

    public function __construct() {
        parent::__construct();
    }

    public function current_place() {
        return 'You are currently viewing page '.$this->cur_page.' out of '.ceil(($this->total_rows/$this->per_page)). 'results';
    }
}

不要忘记确保在 config.php 中将类扩展前缀正确设置为 MY_ (大约第 109 行),

然后您可以在控制器中访问它,如下所示:

$current = $this->pagination->current_place();

您可以将其传递到您的视图中。

You could extend the Pagination class (system/libraries/Pagination.php).

Create a file called MY_Pagination.php and put it in application/libraries/

Add this code to your extend pagination class;

class MY_Pagination extends CI_Pagination {

    public function __construct() {
        parent::__construct();
    }

    public function current_place() {
        return 'You are currently viewing page '.$this->cur_page.' out of '.ceil(($this->total_rows/$this->per_page)). 'results';
    }
}

Don't forget to make sure the class extension prefix is set correctly to MY_ in the config.php (about line 109)

Then you can access it in your controller like;

$current = $this->pagination->current_place();

Which you can pass into your view.

破晓 2025-01-14 10:09:54

取决于您如何设置偏移量。我们的设置是通过每页的偏移量来设置的。所以我们的默认值是每页 10 个。因此,对于第一页,偏移量为 0。对于第二页,偏移量为 10,依此类推。因此,要获取当前页面,我会将偏移量除以当前限制加 1。因此 ($cur_page/$per_page)+1 会获取当前页面。

Depending on how you are setting up the offset. Ours is setup with an offset of the perpage. So our default is 10 per page. So for page one the offset is 0. For page two the offset is 10 and so on. So for me to get the current page I would take the offset divided by the current limit plus 1. So ($cur_page/$per_page)+1 would get me the current page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文