duanqun9618 2011-08-08 18:59
浏览 23
已采纳

PHP分页问题

I'm having a problem with my PHP pagination for a project.

It almost works but it doesn't seem to display the numbers correctly.

I want only 6 more page numbers to display after the selected and one before; (also if you are on page one display 7 after)

For example:

If on Page 1: 1/2/3/4/5/6/7/8

If on Page 2: 1/2/3/4/5/6/7/8

If on Page 5: 4/5/6/7/8/9/10/11

If on Page 10: 9/10/11/12/13/14/15/16

This is my code so far...

if($page == ceil($NumOfPages) && $page != 1){
    for($i = 1; $i <= ceil($NumOfPages)-1; $i++){
    if($i > 0){
        echo "<a href=\"/{$i}\">{$i}</a>";
    }
    }
}
if ($page == ceil($NumOfPages) ) {
    $startPage = $page;
}else{
    $startPage = 1;
}
for ($i = $startPage; $i <= $page+6; $i++){
        if ($i <= ceil($NumOfPages)){
            if($i == $page) {
                echo "<a href='/page/$i/' title='View movies page $i' id='pagelisel'>$i</a> ";
            }else{
                echo "<a href='/page/$i/' title='View movies page $i' id='pageli'>$i</a> ";
            }
        }
}

Any help would be greatly appreciated,

Thanks!

  • 写回答

2条回答 默认 最新

  • dsjbest2015 2011-08-08 19:08
    关注

    I assumed that (partly for myself... ;) ):

    • $page is the selected page
    • $startPage is the first page number you want to show
    • $numPages is already ceil-ed

    First you need to find $startPage. Depending whether $page is the first one (ie has the value one 1, another assumption) or not. Your check is slightly off, as it check if it is equal to the last page.

    if($page == 1) {
        $startPage = 1;
    } else {
        $startPage = $page - 1;
    }
    

    Then you need to find out the last page number you want to print ($lastPage). So check if $startPage is near the end and set ~$lastPage` accordingly:

    if($startPage + 7 > $numPages) {
        $endPage = $numPages;
    } else {
        $endPage = $startPage + 7;
    }
    

    Finally, use you for-loop which seem ok, but loop from $startPage to $endPage.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致