Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to ask how i can hide some of the page numbers on the result page so they show like this:

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

Now it's showing like this:

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

But i don't want that. I want to make them show like in the first example.

Here is the code that i use for making the results in pages :

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

So how i can make it? How i can hide some of the page numbers when they are more?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
276 views
Welcome To Ask or Share your Answers For Others

1 Answer

This is the relevant peace of code:

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

There are 3 variables which matter: $i, $page_count and $requested_page

  • $i = iterator
  • $page_count = total pages
  • $requested_page = page which is loaded

in 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...