Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-16-2012, 12:39 AM   PM User | #1
sonny
Regular Coder

 
sonny's Avatar
 
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
sonny can only hope to improve
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 03:00 AM..
sonny is offline   Reply With Quote
Old 10-16-2012, 09:04 AM   PM User | #2
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,191
Thanks: 217
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
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 09:08 AM..
durangod is offline   Reply With Quote
Old 10-16-2012, 05:01 PM   PM User | #3
sonny
Regular Coder

 
sonny's Avatar
 
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
sonny can only hope to improve
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
sonny is offline   Reply With Quote
Old 10-16-2012, 05:46 PM   PM User | #4
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
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.
__________________
For professional Hosting and Web design.....


NetEssentials.co.uk

Last edited by Redcoder; 10-16-2012 at 05:49 PM..
Redcoder is offline   Reply With Quote
Old 10-16-2012, 11:18 PM   PM User | #5
sonny
Regular Coder

 
sonny's Avatar
 
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
sonny can only hope to improve
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
sonny is offline   Reply With Quote
Old 10-17-2012, 01:07 AM   PM User | #6
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
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

}

__________________
For professional Hosting and Web design.....


NetEssentials.co.uk
Redcoder is offline   Reply With Quote
Old 10-17-2012, 06:27 PM   PM User | #7
sonny
Regular Coder

 
sonny's Avatar
 
Join Date: Apr 2008
Location: United States
Posts: 567
Thanks: 88
Thanked 0 Times in 0 Posts
sonny can only hope to improve
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
sonny is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:58 AM.


Advertisement
Log in to turn off these ads.