PHP如何隐藏一些页码?

时间:2013-08-09 12:41:30

标签: php

我想问一下如何隐藏结果页面上的一些页码,以便它们显示如下:

[First] [Back] [1] [2] [3] [4] [5] ..... [10] [11] [12] [13] [14] [15] [Next] [Show Last]

现在它显示如下:

[1] [2] [3] [4] [5] [6] [7] [8] [8] [10] [11] [12] [13] [14] [15]

但我不想那样。我想让它们像第一个例子一样显示。

以下是我用于在页面中生成结果的代码:

<?PHP
$requested_page = isset($_GET['page']) ? intval($_GET['page']) : 1;

// Get the product count
$r = mysql_query("SELECT COUNT(*) FROM `oferts`");
$d = mysql_fetch_row($r);
$product_count = $d[0];

$products_per_page = 20;

// 55 products => $page_count = 3
$page_count = ceil($product_count / $products_per_page);

// You can check if $requested_page is > to $page_count OR < 1,
// and redirect to the page one.

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



// Then we retrieve the data for this requested page
$r = mysql_query("SELECT * FROM `oferts` ORDER BY id DESC LIMIT $first_product_shown, $products_per_page ");

    while($rowi = mysql_fetch_array($r))
                {
                $title = addslashes($rowi['title']);
                $fromweb = addslashes($rowi['fromweb']);
                $image = addslashes($rowi['image']);
                $link = addslashes($rowi['link']);
                $price = addslashes($rowi['price']);
                $realprice = addslashes($rowi['realprice']);
                $endDate = addslashes($rowi['endDate']);
                $calc = $realprice / $price;
                if ($realprice == 0)
                {
                $procent = 0;
                }
                if ($realprice != 0)
                {
                $percent = 100/$calc;
                $reshenie = 100-$percent;
                $procent = substr($reshenie,0,2);
                }
                if ($fromweb == '1') {
                $divname = "ozo";
                }
                if ($fromweb == '2') {
                $divname = "vip";
                }               
                $seconds = strtotime("$endDate") - time();

                $days = floor($seconds / 86400);
                $seconds %= 86400;

                $hours = floor($seconds / 3600);
                $seconds %= 3600;

                $minutes = floor($seconds / 60);
                $seconds %= 60;

                $position=65;
                $titleNumbered = mb_substr($title, 0, $position, "utf-8");

                $healthy = array('\"');
                $yummy   = array('');

                $TitleText = str_replace($healthy, $yummy, $titleNumbered);
                ?>
<div class="OfferBox">
<center>
    <div class="<?PHP echo $divname;?>"></div>
    <a href="<?PHP echo $link;?>" target="_blank"><img src="<?PHP echo $image;?>" style="width:190px;border:1px solid #E5E5E5;"/></a>
    <div class="OfferText"><?PHP echo "$TitleText...";?></div>
    <div class="p1"><b><?PHP echo $realprice;?>лв.</b></div><div class="p2"><b>-<?PHP echo $procent;?>%</b></div><div class="p3"><b><?PHP echo $price;?>лв.</b></div>
    <a href="<?PHP echo $link;?>" class="OfferButton" target="_blank"></a>
    <div class="timeleft">Изтича след:<br><?PHP echo "$days дни | $hours часа | $minutes мин.";?></div>
</center>   
</div>
<?PHP
}
?>
</div>
<?PHP
echo '<div style="clear:both;display:block;bottom:0;float:right;margin-right:20px;">';
for($i=1; $i<=$page_count; $i++) {
    if($i == $requested_page) {
        echo "<span class='pagenumberSelected'><b>$i</b></span> ";
    } else {
        echo '<a href="index.php?page='.$i.'" class="pagenumber"><b>'.$i.'</b></a> ';
    }
}
echo '</div><br>';
?>

那么我怎么能做到呢? 当他们更多的时候我怎么能隐藏一些页码呢?

2 个答案:

答案 0 :(得分:0)

这是相关的代码和平:

<?php
for($i=1; $i<=$page_count; $i++) {
    if($i == $requested_page) {
        echo "<span class='pagenumberSelected'><b>$i</b></span> ";
    } else {
        echo '<a href="index.php?page='.$i.'" class="pagenumber"><b>'.$i.'</b></a> ';
    }
}
?>

有3个变量很重要: $ i,$ page_count和$ requested_pa​​ge

  • $ i = iterator
  • $ page_count =总页数
  • $ requested_pa​​ge =已加载的页面

在PSEUDO CODE中

if $page_count > 10
  show if $i < 3
  show if $i = $requested_page -1
  show if $i = $requested_page
  show if $i = $requested_page +1    
  show if $i > $page_count - 3

答案 1 :(得分:-1)

这里是一个示例,说明如何仅显示当前页面之前的最后10页而不是每页:

for($i=$page_count-10; $i<=$page_count; $i++) {
    if($i == $requested_page) {
        echo "<span class='pagenumberSelected'><b>$i</b></span> ";
    } else {
        echo '<a href="index.php?page='.$i.'" class="pagenumber"><b>'.$i.'</b></a> ';
    }
}