2011-06-09 107 views
1

我有一个变量$total这是结果的总数和$page这是页码。结果限于每页12个。php计算解决方案分页

假设如果$total为24,则脚本可能分别返回1和2,分别为$page = 1和$page = 2。如果输入数字小于1(负数或零)或者数字大于2,它也应该返回1

再次假设如果$total是25,则脚本可能返回1,2和3用于$page = 1,$page = 2和$page = 3。它也应该返回1,如果输入的数字小于1(负或零),或者如果数大于1

+0

http://stackoverflow.com/questions/2615213/php-page-navigation-by-serial-number – zod 2011-06-09 17:18:49

+0

喜@zod,这是一个完整的页面导航解决方案..我只想要一个带有输入和输出的计算脚本ks为你的建议.. :) – 2011-06-09 17:31:50

+0

我用来复制粘贴。所以我不能编码。如果你不想像我一样阅读这些链接并编写你自己的脚本:-) – zod 2011-06-09 17:38:15

回答

2

这里有一种方法来计算的话:

// Assuming you have the $total variable which contains the total 
// number of records 

$recordsPerPage = 12; 

// Declare a variable which will hold the number of pages required to 
// display all the records, when displaying @recordsPerPage records on each page  
$maxPages = 1; 

if($total > 0) 
    $maxPages = (($total - 1)/$recordsPerPage) + 1; 

// $maxPages now contains the number of pages required. you can do whatever 
// it is you need to do with it. It wasn't clear from the question.. 

return $maxPages; 

此外,如果你想生成包含每个可用的页面,你可能只是这样做的索引数组:

$pages = array(); 
for($i = 1; $i <= $maxPages; i++) 
{ 
    array_push($pages, $i); 
} 

print_r($pages); 
+0

只需要做floor($ maxPages)我认为。 – 2011-06-09 17:24:54

+0

是的,我知道了..此外,我做了一个'地板'到$ maxPages ..谢谢.. – 2011-06-09 17:49:16