“显示X到Y的Z结果”使用Codeigniter分页库? [英] "Displaying X to Y of Z results" using Codeigniter pagination library?
问题描述
我是使用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
code>显示12个结果中的1到5个
第2页:显示12个结果中的6到10个
< br>
第3页:显示12个结果中的11至12个
。
Page 1: Displaying 1 to 5 of 12 results
Page 2: Displaying 6 to 10 of 12 results
Page 3: Displaying 11 to 12 of 12 results
.
推荐答案
保持简单。
您需要3个变量。结果开始,结果结束(在页面中,而不是整个)和总结果。
You need 3 variables. Result start, result end (in the page, not the whole) and total result.
-
您已经知道总结果(来自分页)。让我们把它叫做
$ total
。
> $ 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;
发送所有这三个值进行查看。
send all those 3 values to view.
echo "displaying $result_start to $result_end of $total";
这篇关于“显示X到Y的Z结果”使用Codeigniter分页库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!