I am using this pagination script given below. The problem is that it gives the continuous count of the pages. Say for example if there are 10 pages it gives out numbers 1 2 3 4 5 6 7 8 9 10
(all clickable to respective pages). What I want is to break pagination count after 5. It should display 1 2 3 4 5.......9 10
. Then when I click on page number 5 it should hide 1 and display 6 as 2 3 4 5 6....9 10
. Please help me.
$start = 0;
$limit = 1;
if(isset($_GET['id'])){
$id = (!empty($_GET['id']))?$_GET['id']:null;
$start = ($id-1)*$limit;
}else{
$id = null;
}
$stmt = $pdo->prepare("SELECT ad_id, ad_description, ad_url, ad_memclicks, ad_visclicks, ad_status, avc_clicks FROM ads
LEFT JOIN ads_viewed_cost ON ads.ad_clicks = ads_viewed_cost.avc_id
WHERE ad_user = :user LIMIT $start, $limit");
$stmt-> bindValue(':user', $sessionid);
$stmt-> execute();
$count = $pdo->prepare("SELECT COUNT(*) as cnt FROM ads LEFT JOIN ads_viewed_cost ON ads.ad_clicks = ads_viewed_cost.avc_id WHERE ad_user = :user");
$count-> bindValue(':user', $sessionid);
$count-> execute();
$rf = $count->fetch();
$rows = $rf['cnt'];
$total = ceil($rows/$limit);
if($id>1){
echo "<span class='pre'><a href='?id=".($id-1)."' class='button'>PREVIOUS</a></span>";
}
if($id!=$total){
echo "<span class='next'><a href='?id=".($id+1)."' class='button'>NEXT</a></span>";
}
echo "<ul class='pagination'>";
for($i=1; $i<=$total; $i++){
if($i == $id){
echo "<li class='current'>".$i."</li>";
}else{
echo "<li><a href='?id=".$i."'>".$i."</a></li>";
}
}
echo "</ul>";
I don't think what you're describing is the best way to do pagination as you're losing the first page as you go, but this does what you are asking for.
$dots = false;
for ( $i=1; $i<=$total; $i++ ) {
if( $i == $id ) {
echo "<li class='current'>{$i}</li>";
} else {
if ( ( $id < 5 && $i < 5 ) || ( ( $i > $id-4 && $i < $id ) || $i > $total-2 || $i == $id+1 ) ) {
echo "<li><a href='?id=".$i."'>{$i}</a></li>";
} else {
if ( $i > $id && !$dots ) {
echo "...";
$dots = true;
}
}
}
}
If the current $id
is less than 5, it shows the first pages. After that, it shows the current page, three before it, and one after it (so when id=5, you see 2, 3, 4, 5, 6).
The dots are shown if there is a gap between the current page + 1 and the last two pages, but are only ever shown once (what the boolean is for).
The last two pages are always shown.
Tested with a larger page set (up to 100) and works as expected.
A more common way to do pagination, as StackOverflow itself does it, would be once you are outside the extremes, you show the first page, dots, current page and a couple either side, dots, and last page.
1 ... 12 13 [14] 15 16 ... 100
Without the first page anchor, with what I've provided for you here, it could take a LOT of clicking to get back to page one with a large page set.
To achieve this method, you can use this code instead. The changes are in the if logic and a second boolean with handling for both sets of dots.
$dotsLow = false;
$dotsHi = false;
for ( $i=1; $i<=$total; $i++ ) {
if( $i == $id ) {
echo "<li class='current'>{$i}</li>";
} else {
if (
( $i == 1 || $i == $total ) ||
( $id < 5 && $i < 5 ) ||
( $id > $total-4 && $i > $total-4 ) ||
( $i >= $id-2 && $i <= $id+2)
) {
echo "<li><a href='?id=".$i."'>{$i}</a></li>";
} else {
if ( $i > $id && !$dotsHi ) {
echo "...";
$dotsHi = true;
}
if ( $i < $id && !$dotsLow ) {
echo "...";
$dotsLow = true;
}
}
}
}