Jump to content

[SOLVED] Page Numbering..... 10 either side of the current page......


scooter41

Recommended Posts

Hi There....

 

I have a user based system I am creating, that has 97k users, so as you can imagine for page numbering I simply cant list every page number :)

 

I have a script that goes either side of the current page i.e if current page is 30, then page numbering starts from 20 and goes to 40 for example.

 

To do this I am using:

 

for($i = 1; $i <= $total_pages; $i++){

    if(($page) == $i){

 

        $pagelinks.= " |<span class='currPage'> $i </span>| ";

        } else {

 

if (($i < $page+10 && $i > $page-10 )){

            $pagelinks.="<a href=\"?page=$i$urlvars2\">$i</a> ";

}

    }

}

 

 

But my question is, is there anyway for the values under 10, for example, page 3 that only 10 page links are created instead of the regular 20, so it looks a bit inconsistent with the other pages. Is there a neat and tidy way to format that if statement to say if current page is under 10, bump up the top end limiter?

try this. It will also take care of the other end of the range when you get near the last page.

 

<?php
$total_pages = 100;
$page = isset($_GET['page']) ? $_GET['page'] : 1;                      // set from form for testing

if ($page < 1) $page = 1;
if ($page > $total_pages) $page = $total_pages;

if ($page < 10)
{
    $startnum = 1;
    $endnum = min(20, $total_pages) ;
}
elseif ($page > $total_pages-10)
{
    $startnum = max(1, $total_pages - 19);
    $endnum = min($page + 10, $total_pages);
}
else {
    $startnum = max(1, $page - 9);
    $endnum = min($page + 10, $total_pages);
}
for($i = $startnum; $i <= $endnum; $i++){
    if ($page == $i){
        $pagelinks.= " |<span class='currPage'> $i </span>| ";
    } else {
        $pagelinks.="<a href=\"?page=$i$urlvars2\">$i</a> ";
    }
}
echo $pagelinks;
?>
<form>
<input type="text" name="page" size="5">
<input type="submit" name="sub" value="Submit">
</form>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.