我有一个变量$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
这里有一种计算方法:
// 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);