“显示 X 到 Y 的 Z 结果"使用 Codeigniter 分页库? [英] "Displaying X to Y of Z results" using Codeigniter pagination library?

查看:16
本文介绍了“显示 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.

  1. 您已经知道总结果(从分页).我们称之为$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 分页库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆