使用php分页显示总记录的记录数

时间:2014-07-30 22:55:43

标签: php

我正在开发一个自定义的php分页。一切运作良好并显示记录,我正在尝试编码..显示X到X的X记录..但面临一些问题。任何建议。

当前代码。

 $total = 18 <- get the number of records with sql query.
 $limit = 15; <- number of records to display on a page
 $page_no = $current_page <- gets the page # from the url using smarty. 

// Showing from

$from = (($page_no * $limit) - $limit + 1); <- gives the starting record - (working)

// showing to

$to = ($page_no * $limit); <- of course it will work for page one but page 2 will display 
                           30. i know this is wrong but here is where i am stuck. i did 
                           some other coding but have problems there too. 

output is something like this.

Showing $from to $to of $total records

Page one i get: Showing 1 to 15 of 18 records

Page two i get: Showing 16 to 30 of 18 records

知道如何正常工作吗?

2 个答案:

答案 0 :(得分:0)

$ to = min(($ page_no * $ limit),$ total)

效果很好。谢谢Waxen

答案 1 :(得分:0)

您可以将$设置为以下内容。

  

$ to =($ total - $ page_no * $ limit&gt; 0?$ page_no * $ limit:$ total);