quot;Displaying X to Y of Z resultsquot; using Codeigniter pagination library?(“显示 X 到 Y 的 Z 结果使用 Codeigniter 分页库?)
问题描述
我是,使用 Codeigniter 分页库,
I am, using the Codeigniter pagination library,
我想知道如何获取使用分页类显示的第一个和最后一个项目的数量?
I am wondering how I can grab the number of the first and last items displayed using the pagination class?
所以,如果我有 12 个结果,并且 per_page 设置为 5.我会想要
So, if I had 12 results, and per_page was set to 5. I would want
第 1 页:显示 12 个结果中的 1 到 5 个
第 2 页:显示 12 个结果中的 6 到 10 个
第 3 页:显示 12 个结果中的 11 到 12 个.
推荐答案
保持简单.
您需要 3 个变量.结果开始,结果结束(在页面中,而不是整个)和总结果.
You need 3 variables. Result start, result end (in the page, not the whole) and total result.
您已经知道总结果(从分页).我们称之为
$total.
所以,现在从 CI 实例获取当前页面 ($curpage) 值.然后,
so, now get the current page ($curpage) value from CI instance. Then,
$result_start = ($curpage - 1) * $per_page + 1;
if ($result_start == 0) $result_start= 1; // *it happens only for the first run*
对于$result_end,您只需要添加每页值,但考虑到它会少1,
for $result_end, you just need to add the per page value but considering it'll be 1 less,
$result_end = $result_start+$per_page-1;
if ($result_end < $per_page) // happens when records less than per page
$result_end = $per_page;
else if ($result_end > $total) // happens when result end is greater than total records
$result_end = $total;
发送所有这 3 个值进行查看.
send all those 3 values to view.
echo "displaying $result_start to $result_end of $total";
这篇关于“显示 X 到 Y 的 Z 结果"使用 Codeigniter 分页库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“显示 X 到 Y 的 Z 结果"使用 Codeigniter 分页库?
基础教程推荐
- php中的foreach复选框POST 2021-01-01
- 将变量从树枝传递给 js 2022-01-01
- 主题化 Drupal 7 的 Ubercart “/cart"页 2021-01-01
- php中的PDF导出 2022-01-01
- PHPUnit 的 Selenium 2 文档到底在哪里? 2022-01-01
- Web 服务器如何处理请求? 2021-01-01
- Yii2 - 在运行时设置邮件传输参数 2022-01-01
- 如何在数学上评估像“2-1"这样的字符串?产生“1"? 2022-01-01
- php 7.4 在写入变量中的 Twig 问题 2022-01-01
- 使用 scandir() 在目录中查找文件夹 (PHP) 2022-01-01
