Pagination to show max value and limit the rest

前端 未结 2 1903
轮回少年
轮回少年 2021-01-07 04:19

Right now my pagination would show up something like this

[1] [2] [3] [4] [5] [6] [7] [8] [9]

How would i make it show up like this

[1] [2] [3] [4] [

相关标签:
2条回答
  • 2021-01-07 04:49

    Try this :

        <?php
            $link = "";
     // $page = $_GET['page'];
     // $pages=20; // Hardcoded for testing purpose
      $limit=5  ;
        if ($pages >=1 && $page <= $pages)
        {
            $counter = 1;
            $link = "";
            if ($page > ($limit/2))
               { $link .= "<a href=\"?page=1\">1 </a> ... ";}
            for ($x=$page; $x<=$pages;$x++)
            {
    
                if($counter < $limit)
                    $link .= "<a href=\"?page=" .$x."\">".$x." </a>";
    
                $counter++;
            }
            if ($page < $pages - ($limit/2))
             { $link .= "... " . "<a href=\"?page=" .$pages."\">".$pages." </a>"; }
        }
    
        echo $link;
    ?>
    

    OUTPUT :

    //At page=1
    1 2 3 4 ... 20 
    
    //At page=12
    1 ... 12 13 14 15 ... 20 
    
    //At page=18
    1 ... 18 19 20 
    
    0 讨论(0)
  • 2021-01-07 05:00
    <?php
        if ($pages >=1 && $page <= $pages) {
            $counter = 1;
            $link = "";
            for ($x=1; $x<=$pages;$x++) {
                if($counter < 5)
                    $link .= "<a href=\"?page=" .$x."\">".$x."</a>";
    
                $counter++;
            }
            $link .= "...";
            $link .=  "<a href=\"?page=" .$pages."\">".$pages."</a>";
        }
        echo $link;
    ?>
    
    0 讨论(0)
提交回复
热议问题