我尝试使用此算法创建一个简单的系统博客页面:
<?php
$page = floor(10 / 10);
$limit = 40;
$num= 100;
$page = 1;
for ($record = 0; $record <= $num ; ++$record )
{
if($record % $limit == 0)
{
if(!($record < $limit))
{
echo 'page:'.$page.'<br/>';
for($id = $record - $limit +1 ; $id <= $record ; ++$id)
{
echo $id.'<br/>';
}
$page ++;
echo '<hr>';
}
}
}
?>
但似乎不起作用例如......在$ limit = 40上我丢失了最后20个id!
可以帮助我改进此算法或建议我更好的方法&gt;
答案 0 :(得分:0)
<?php
$limit = 40;
$items = 100;
$page_counter = 1;
$excess_items = $items % $limit;
if($excess_items > 0)
$total_page = ($items / $limit) + 1;
$current_count = 1;
while($page_counter <= $total_page){
echo 'total_page:'.$page_counter.'<br/>';
$cutting_count = 1;
while($cutting_count <= $limit){
if($current_count <= $items)
echo $current_count.'<br/>';
$cutting_count ++;
$current_count ++;
}
echo '<hr>';
$page_counter++;
}
?>