简体   繁体   English

PHP-分页编号

[英]PHP - Pagination numbering

Here is my code: 这是我的代码:

<?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>';      
    ?>

This code makes this: 这段代码使得: 在此处输入图片说明

What i want is: 我想要的是:

When i click on page 14, the next one 15 must appear and page 2 must hide. 当我单击第14页时,下一个15必须出现,第2页必须隐藏。 All the times page 1 and page 111 must be shown. 必须始终显示第1页和第111页的所有时间。

I am stuck here, can you help me out achieve that ? 我被困在这里,你能帮我实现这个目标吗?

Thanks in advance! 提前致谢!

Try this: UPDATED 试试这个: UPDATED

<?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>';
    }
}  
?>

Just Write a code snippet before the for loop which i mention below. 只需在我下面提到的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>';      
?>
    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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM