PHP - 分页编号

时间:2015-10-27 10:20:48

标签: php

这是我的代码:

<?PHP
$rsp = mysql_query("SELECT COUNT(*) FROM `products` WHERE `Category`='$Cat'");
$d = mysql_fetch_row($rsp);
$product_count = $d[0];

$products_per_page = 20;


$page_count = ceil($product_count / $products_per_page);


$first_product_shown = ($requested_page - 1) * $products_per_page;                                                          

echo '<li><a href="category.php?id='.$Cat.'&page=1">1</a></li>';
        for($i=2; $i<=14; $i++) {
            if($i == $requested_page) {
                echo '<li class="active"><a href="#">'.$i.'<span class="sr-only">(current)</span></a></li>';
            } else {
                echo '<li><a href="category.php?id='.$Cat.'&page='.$i.'">'.$i.'</a></li>';
            }
        }
echo '<li><a href="category.php?id='.$Cat.'&page=1">'.$page_count.'</a></li>';      
    ?>

此代码使此: enter image description here

我想要的是:

当我点击第14页时,必须出现下一个15,并且必须隐藏第2页。 必须始终显示第1页和第111页的所有时间。

我被困在这里,你能帮我实现吗?

提前致谢!

3 个答案:

答案 0 :(得分:1)

试试这个:更新

<?PHP
$rsp = mysql_query("SELECT COUNT(*) FROM `products` WHERE `Category`='$Cat'");
$d = mysql_fetch_row($rsp);
$requested_page         = isset($_GET['page']) ? $_GET['page'] : 1;
$product_count          = $d[0];
$products_per_page      = 20;
$page_count             = (int)ceil($product_count / $products_per_page);
$links_to_show          = 12;
$first_product_shown    = max($requested_page - $links_to_show, 1);

$array_to_show          = range($first_product_shown, min($first_product_shown + $links_to_show + 1, $page_count));
array_unshift($array_to_show, 1);
array_push($array_to_show, $page_count);
$array_to_show = array_flip(array_flip($array_to_show));
ksort($array_to_show);

foreach($array_to_show as $i){
    if($i == $requested_page) {
        echo '<li class="active"><a href="#">'.$i.'<span class="sr-only">(current)</span></a></li>';
    } else {
        echo '<li><a href="?id='.$Cat.'&page='.$i.'">'.$i.'</a></li>';
    }
}  
?>

答案 1 :(得分:0)

只需在下面提到的for循环之前编写代码片段。

<?PHP
$rsp = mysql_query("SELECT COUNT(*) FROM `products` WHERE `Category`='$Cat'");
$d = mysql_fetch_row($rsp);
$product_count = $d[0];

$products_per_page = 20;


$page_count = ceil($product_count / $products_per_page);


$first_product_shown = ($requested_page - 1) * $products_per_page;                                                          

echo '<li><a href="category.php?id='.$Cat.'&page=1">1</a></li>';
    $page = $_GET['page'];
    if($page>13 && ($page < ($page_count -13))){
        $end = $page+1;
        $start = $end-12;
    } else if($page >= $page_count -13){
        $end = $page_count -1;
        $start = $page_count -13;
    } else {
        $end = 14;
        $start = 2;
    }
    for($i=$start; $i<=$end; $i++) {
        if($i == $requested_page) {
            echo '<li class="active"><a href="#">'.$i.'<span class="sr-only">(current)</span></a></li>';
        } else {
            echo '<li><a href="category.php?id='.$Cat.'&page='.$i.'">'.$i.'</a></li>';
        }
    }
echo '<li><a href="category.php?id='.$Cat.'&page=1">'.$page_count.'</a></li>';      
?>

答案 2 :(得分:-1)

    Use this:- 
  $page=$_REQUEST["page"]; 
  $limit=5 ;      
    if ($total_pages >=1 && $page <= $total_pages)
{
    $counter = 1;
    $link = "";
    if ($page > ($limit/2))
       { 
       $link .= "<a class='pad' href=\"?page=1\">1 </a> ... ";
       }
    for ($x=$page; $x<=$total_pages;$x++)
    {
        if ($x == $page)
            {
             $link .="<a class='pad active-num' href=\"?page=" .$x."\">".$x." </a> "; 
            }
        else if($counter < $limit)
            $link .= "<a class='pad' href=\"?page=" .$x."\">".$x." </a>";

        $counter++;
    }
    if ($page < $total_pages - ($limit/2))
     { $link .= "... " . "<a class='pad' href=\"?page=" .$total_pages."\">".$total_pages." </a>"; }
}
      echo $link;