Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts

    Limit pageination links

    Hi

    I have been using this for years, problem is, I get a million page links
    at the bottom, I have been trying to add stages like 123...789 but cannot
    get it to work. I did find another pagination script that does
    that but I would rather stay with this one if possible. anyone see a quick edit
    not requiring a rewrite or anything complex.

    PHP Code:
    //-------------------------------TOP OF PAGE
    $limit=mysql_real_escape_string($_GET[limit]);//clean
    $page=mysql_real_escape_string($_GET[page]);//clean
    $query=mysql_real_escape_string($_GET[query]);//clean

    if (!($limit)){
         
    $limit 50;} // Default results per-page.

    if (!($page)){
         
    $page 0;} // Default page value.

    if ($numrows $limit) {
    $pages++;} // has remainder so add one page

    $current = ($page/$limit) + 1// Current page number.

    if (($pages 1) || ($pages == 0)) {
    $total 1;} // If $pages is less than one or equal to 0, total pages is 1.

    else {
    $total $pages;} // Else total pages is $pages value.

    $first $page 1// The first result.

    if (!((($page $limit) / $limit) >= $pages) && $pages != 1) {
    $last $page $limit;} //If not last results page, last result equals $page plus $limit.
     
    else{
    $last $numrows;} // If last results page, last result equals total number of results.
    //--------------------------- END TOP OF PAGE

    // Doing whatever this pageination is for.....

    //----------------------------BOTTOM OF PAGE
    // Bottom code of pagination
    if ($page != 0) { // Don't show back link if current page is first page.
    $back_page $page $limit;
    echo(
    "<a href=\"search.php?query=$query&page=$back_page&limit=$limit\">back</a>    \n");}

    for (
    $i=1$i <= $pages$i++) // loop through each page and give link to it.
    {
     
    $ppage $limit*($i 1);
     if (
    $ppage == $page){
     echo(
    "<b>$i</b>\n");} // If current page don't give link, just text.
     
    else{
     echo(
    "<a href=\"search.php?query=$query&page=$ppage&limit=$limit\">$i</a> \n");}
    }

    if (!(((
    $page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
    $next_page $page $limit;
    echo(
    "    <a href=\"search.php?query=$query&page=$next_page&limit=$limit\">next</a>");}
    //------------------------END BOTTOM OF PAGE 
    Thanks
    Sonny
    Last edited by sonny; 10-16-2012 at 04:00 AM.

  • #2
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,486
    Thanks
    279
    Thanked 32 Times in 31 Posts
    Hi bud,

    here is one that i have used for awhile, it was done for me as a favor by a very experience coder awhile back
    have used it alot and it works great

    its pretty simple and it also does what you say,
    and it allows you to set the range on either side of the page you are on.

    i use ps pagination and it does a query and then load the value in $rs
    and then i just copy the vars from that result from the pagination array into my little script

    basically i changed from using the pagination display output to my own thats why i copied the results from the array here

    here ya go, maybe if you look at this it will give you some hints bud.
    hope it helps.


    PHP Code:

    //also the record_per_page is set up here as well before the query 
    // the query is done up before this 

    $pager = new PS_Pagination($link,$query,$record_per_page,5);
     
    $rs $pager->paginate();


    //#############  added for page number display

    // i take what  need from the pager array

    $maxpg $pager->max_pages;

    $curpg $pager->page;

    $rppg =  $pager->rows_per_page;

    $off_setpg $pager->offset;



    function 
    pag_links() {

      global 
    $curpg$maxpg;


      
    /******  build the pagination links ******/

    // i believe this is the part your looking for 

      // range of num links to show on either side of current page

      
    $range 5;

      

     

      
    // show $range pages on either side of current page. if this includes page

      // 1, don't show "First". if this includes last page, don't show "Last"

      // show back links |< and < unless at page 1

      
    if ($curpg 1) {  

        if (
    $curpg $range 1) { // wouldn't show page 1 in range

          // show << link to go back to page 1

          
    echo "<a href='{$_SERVER['PHP_SELF']}?page=1'>|&lsaquo;</a> ";

        }

        
    // get previous page num   

        
    $prevpage $curpg 1;   

        
    // show < link to go back 1 page

        
    echo "<a href='{$_SERVER['PHP_SELF']}?page=$prevpage'>&lsaquo;</a> ";

      } 



      
    // loop to show links to range of pages around current page

      // if previous page <, there is a space after it



      
    for ($x $curpg $range

           
    $x $curpg $range 1

           
    $x++) {   

        
    // if it's a valid page number (not < 1 or > last page)...  

        
    if ($x && $x <= $maxpg) {      

          
    // if we're on current page, not a link...      

          
    if ($x == $curpg) { 

            
    // 'highlight' it but don't make a link

            
    echo "[<b>$x</b>] "

          } else { 

            
    // if not current page, make it a link

            
    echo "<a href='{$_SERVER['PHP_SELF']}?page=$x'>$x</a> "

          }

        } 

      } 

      

      
    // if not on last page, show forward > and last page >| links 

      
    if ($curpg $maxpg) {   

        
    // get next page   

        
    $nextpage $curpg 1;    

        
    // echo forward link for next page    

        
    echo "<a href='{$_SERVER['PHP_SELF']}?page=$nextpage'>&rsaquo;</a> ";

        
    // echo forward link for lastpage, unless last page already showing

        
    if ($curpg $range $maxpg) {

          echo 
    " <a href='{$_SERVER['PHP_SELF']}?page=$maxpg'>&rsaquo;|</a>";

        }

      }

      

      
    //echo "</span>";

      /****** end build pagination links ******/

    // end pag_links() 

    and then to show it just do this somewhere on your page


    PHP Code:


    <?php
    if(mysql_num_rows($rs)>0){
     
    pag_links();
    ?>
    Last edited by durangod; 10-16-2012 at 10:08 AM.

  • #3
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Thanks I will try it, I scraped that code above I posted
    its a shame because it worked great but left a ton of page
    links that grow after a a while. I Posted that here because I
    thought it might be a line or two edit but see it is too complex
    to save.

    I did visit the site I found it at,
    http://www.evolt.org/node/19340
    and read the old comments and did see some edits to manage all
    those page links, but none work I tried them, on page 1 and 2,
    they leave out next links and cause bad links.

    Thanks
    Sonny

  • #4
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    335
    Thanks
    2
    Thanked 49 Times in 48 Posts
    Quote Originally Posted by sonny View Post
    Hi

    PHP Code:
    for ($i=1$i <= $pages$i++) // loop through each page and give link to it.
    {
     
    $ppage $limit*($i 1);
     if (
    $ppage == $page){
     echo(
    "<b>$i</b>\n");} // If current page don't give link, just text.
     
    else{
     echo(
    "<a href=\"search.php?query=$query&page=$ppage&limit=$limit\">$i</a> \n");}
    }

    if (!(((
    $page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
    $next_page $page $limit;
    echo(
    "    <a href=\"search.php?query=$query&page=$next_page&limit=$limit\">next</a>");}
    //------------------------END BOTTOM OF PAGE 
    Check out this modification:

    PHP Code:
    for ($i=1$i <= $pages$i++) // loop through each page and give link to it.
    {
     
    $ppage $limit*($i 1);
     if (
    $ppage == $page){
     echo(
    "<b>$i</b>\n");} // If current page don't give link, just text.
     
    else{
     echo(
    "<a href=\"search.php?query=$query&page=$ppage&limit=$limit\">$i</a> \n");}

    //Change
    if($i>$page+10){
    $i+99//Adds 99 to $i hence from page 10, the pages skip by 100
             //Because there is another 1 being added at the for loop
            //Change 99 to whatever interval you want
    }

    //Change ends here
    }

    if (!(((
    $page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link.
    $next_page $page $limit;
    echo(
    "    <a href=\"search.php?query=$query&page=$next_page&limit=$limit\">next</a>");}
    //------------------------END BOTTOM OF PAGE 
    Hence they'll appear something like: 5,6,7,8,9,10,11,12,13,14,15,115,215,315

    If you have a specific interval and format of display you'd like, post again and i'll show you the code modification you'll need.
    Last edited by Redcoder; 10-16-2012 at 06:49 PM.

  • #5
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Thanks for replying

    It would be great if I could have stages like 3
    (three links before current page and three links after) or something
    like that, it would be nice to never have more then 15 links
    at the bottom.

    I'm glad you posted I wanted to keep this. I had it for a long time.

    Thanks
    Sonny

    PS I implemented your edit, but I only had 24 pages and it showed 24 links

  • #6
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    335
    Thanks
    2
    Thanked 49 Times in 48 Posts
    Quote Originally Posted by sonny View Post
    Thanks for replying

    It would be great if I could have stages like 3
    (three links before current page and three links after) or something
    like that, it would be nice to never have more then 15 links
    at the bottom.

    I'm glad you posted I wanted to keep this. I had it for a long time.

    Thanks
    Sonny

    PS I implemented your edit, but I only had 24 pages and it showed 24 links
    Sorry, I had used the wrong comparison for $i. Check the below code out.

    PHP Code:

    if ($page != 0) { // Don't show back link if current page is first page.
    $back_page $page $limit;
    echo(
    "<a href=\"search.php?query=$query&page=$back_page&limit=$limit\">back</a>    \n");}

    for (
    $i=1$i <= $pages$i++) // loop through each page and give link to it.
    {
     
    $ppage $limit*($i 1);
     if (
    $ppage == $page){
     echo(
    "<b>$i</b>\n");} // If current page don't give link, just text.
     
    else{
     echo(
    "<a href=\"search.php?query=$query&page=$ppage&limit=$limit\">$i</a> \n");

    //Change
    if($i>3){

    $i+9
    //Adds 9 to $i hence from page 3, the pages skip by 10
    //Because there is another 1 being added at the for loop
    //Change 9 to whatever interval you want
    }

    //Change ends here

    }


  • #7
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    That just gives links for all 124 pages I have, I found another that works great
    it never goes over 14 page links.

    Thanks
    Sonny


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •