douli8428 2017-04-06 18:16
浏览 66
已采纳

打破分页页数

I am using this pagination script given below. The problem is that it gives the continuous count of the pages. Say for example if there are 10 pages it gives out numbers 1 2 3 4 5 6 7 8 9 10 (all clickable to respective pages). What I want is to break pagination count after 5. It should display 1 2 3 4 5.......9 10. Then when I click on page number 5 it should hide 1 and display 6 as 2 3 4 5 6....9 10. Please help me.

$start = 0;
$limit = 1;

if(isset($_GET['id'])){
  $id = (!empty($_GET['id']))?$_GET['id']:null;
  $start = ($id-1)*$limit;
}else{
  $id = null;
}

$stmt = $pdo->prepare("SELECT ad_id, ad_description, ad_url, ad_memclicks, ad_visclicks, ad_status, avc_clicks FROM ads
                       LEFT JOIN ads_viewed_cost ON ads.ad_clicks = ads_viewed_cost.avc_id
                       WHERE ad_user = :user LIMIT $start, $limit");
$stmt-> bindValue(':user', $sessionid);
$stmt-> execute();

$count = $pdo->prepare("SELECT COUNT(*) as cnt FROM ads LEFT JOIN ads_viewed_cost ON ads.ad_clicks = ads_viewed_cost.avc_id WHERE ad_user = :user");
$count-> bindValue(':user', $sessionid);
$count-> execute();
$rf = $count->fetch();
$rows = $rf['cnt'];
$total = ceil($rows/$limit);

if($id>1){
  echo "<span class='pre'><a href='?id=".($id-1)."' class='button'>PREVIOUS</a></span>";
}
if($id!=$total){
  echo "<span class='next'><a href='?id=".($id+1)."' class='button'>NEXT</a></span>";
}

echo "<ul class='pagination'>";
for($i=1; $i<=$total; $i++){
  if($i == $id){
    echo "<li class='current'>".$i."</li>";
  }else{
    echo "<li><a href='?id=".$i."'>".$i."</a></li>";
  }
}
echo "</ul>";
  • 写回答

1条回答 默认 最新

  • doulian1852 2017-04-06 18:54
    关注

    I don't think what you're describing is the best way to do pagination as you're losing the first page as you go, but this does what you are asking for.

    $dots = false;
    for ( $i=1; $i<=$total; $i++ ) {
        if( $i == $id ) {
            echo "<li class='current'>{$i}</li>";
        } else {
            if ( ( $id < 5 && $i < 5 ) || ( ( $i > $id-4 && $i < $id ) || $i > $total-2 || $i == $id+1 ) ) {
                echo "<li><a href='?id=".$i."'>{$i}</a></li>";
            } else {
                if ( $i > $id && !$dots ) {
                    echo "...";
                    $dots = true;
                }
            }
        }
    }
    

    If the current $id is less than 5, it shows the first pages. After that, it shows the current page, three before it, and one after it (so when id=5, you see 2, 3, 4, 5, 6).

    The dots are shown if there is a gap between the current page + 1 and the last two pages, but are only ever shown once (what the boolean is for).

    The last two pages are always shown.

    Tested with a larger page set (up to 100) and works as expected.


    A more common way to do pagination, as StackOverflow itself does it, would be once you are outside the extremes, you show the first page, dots, current page and a couple either side, dots, and last page.

    1 ... 12 13 [14] 15 16 ... 100

    Without the first page anchor, with what I've provided for you here, it could take a LOT of clicking to get back to page one with a large page set.

    To achieve this method, you can use this code instead. The changes are in the if logic and a second boolean with handling for both sets of dots.

    $dotsLow = false;
    $dotsHi = false;
    for ( $i=1; $i<=$total; $i++ ) {
        if( $i == $id ) {
            echo "<li class='current'>{$i}</li>";
        } else {
            if (
                ( $i == 1 || $i == $total ) ||
                ( $id < 5 && $i < 5 ) ||
                ( $id > $total-4 && $i > $total-4 ) ||
                ( $i >= $id-2 && $i <= $id+2)
            ) {
                echo "<li><a href='?id=".$i."'>{$i}</a></li>";
            } else {
                if ( $i > $id && !$dotsHi ) {
                    echo "...";
                    $dotsHi = true;
                }
                if ( $i < $id && !$dotsLow ) {
                    echo "...";
                    $dotsLow = true;
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么