我有一个变量$total
,它是结果总数,$page
是页码。结果限制为每页12个。
假设如果$total
为24,则脚本可以分别为$page
= 1和$page
= 2返回1和2。如果输入数字小于1(负数或零)或者数字大于2,它也应返回1
同样,假设如果$total
为25,则脚本可以分别为$page
= 1,$page
= 2和$page
= 3返回1,2和3。如果输入数字小于1(负数或零)或者数字大于1,它也应返回1
答案 0 :(得分: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);