Pagination: cravikiran's version

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
cravikiran
Forum Newbie
Posts: 7
Joined: Sat Sep 18, 2004 4:55 pm

Pagination: cravikiran's version

Post by cravikiran »

Hello, I have not really tested the script above out. However, I have a pagination snippet of my own to submit. Its pretty nice... I like it. It is from my wiki-type site Sourceworld. Since this is the case, it needs a few customizations to work with your site but it should still be relatively easy to get it working with your site.

Code: Select all

//------------------------------------------------------------------------------
// Create hyperlink
//------------------------------------------------------------------------------
function hyperlink( $link, $text, $alt='' ) {
  if( $alt != '' ) $alt = "title="$alt"";
  return "<a href="$link" $alt>$text</a>";
}


//------------------------------------------------------------------------------
// Duplicate a GET query
//------------------------------------------------------------------------------
function duplicate_link( $key_0, $val_0 ) {
  $config_main_page = "index.php";
  $link = $config_main_page . '?';
  foreach( $_GET as $key => $value ) {
    if( $key == $key_0 ) $link .= "$key_0=$val_0&";
    else $link .= $key . '=' . $value . '&';
  }
  return substr( $link, 0, strlen( $link ) - 5 );
}


//------------------------------------------------------------------------------
// Create the Paginator
//------------------------------------------------------------------------------
function paginate( $page_key, $page, $rpp, $count ) {
  if( $rpp == 0 || $count / $rpp <= 1 ) return "";
  $pages = ceil( $count / $rpp );
  $left_page = ( (ceil($page / 10) == ($page / 10) ) ? 
                 floor( $page / 10 ) - 1 : floor( $page / 10 ) ) * 10 + 1;

  $ret = "<p>";

  if( $page != 1 ) $ret .= hyperlink( duplicate_link( $page_key, $page - 1 ), 
                                      "<<" );
  else $ret .= "<<";

  for( $i = 0; $i < 10; $i++ ) {
    if( $i == 0 ) {
       if( $left_page + $i != $page ) $ret .= 
         hyperlink( duplicate_link( $page_key, $left_page ), " $left_page" );
       else $ret .= " <b>[$page]</b>";
    }
    else if( $i == 9 ) {
      if( $left_page + $i != $page )
        $ret .= hyperlink( duplicate_link( $page_key, $left_page + 9 ), 
                           " " . ($left_page + 9) );
      else $ret .= " <b>[$page]</b>";
    }
    else if( $left_page + $i == $pages ) {
      if( $left_page + $i != $page )
        $ret .= hyperlink( duplicate_link( $page_key, $left_page + $i ), 
                           " " . ($left_page + $i) );
      else $ret .= " <b>[$page]</b>";
      break;
    }
    else {
      if( $left_page + $i != $page ) $ret .= 
        hyperlink( duplicate_link( $page_key, $left_page + $i ), " ." );
      else $ret .= " <b>[$page]</b>";
    }
  }

  if( $page != $pages ) $ret .= 
    hyperlink( duplicate_link( $page_key, $page + 1 ), " >>" );
  else $ret .= " >>";

  $ret .= "</p>";

  return $ret;
}
So, theres a few things you need to know about the results you are displaying with a paginator. You need to know what page you are on ($page), how many results there are in total ($count) and how many results there should be per page ($rpp). Then, you choose a GET variable that will hold the current page, say we choose "page". Then, all we need to do is $ret = paginate( 'page', $_GET['page'], $_GET['rpp'], $count ); Now, $ret will contain the paginator, with links that should work with your script if you use GET variables to form the requests. Here, of course, we are letting the user provide the results per page as a GET variable as well. The only thing you may need to change is the $config_main_page in duplicate_link.

Hope that helps. Check out the site where this script is used to paginate: Sourceworld
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

topic split from viewtopic.php?t=26002.
do not threadjack.
cravikiran
Forum Newbie
Posts: 7
Joined: Sat Sep 18, 2004 4:55 pm

Post by cravikiran »

Apologies. I suppose the Sticky about adding new code snippets could be clearer. I thought it said if a thread exists that deals with what your snippet does, then you post in the same thread.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Same-ish subject matter, totally different implementation.. that's why.
mhulse
Forum Commoner
Posts: 41
Joined: Fri Jun 11, 2004 12:50 am

Post by mhulse »

Do you have an example page?
cravikiran
Forum Newbie
Posts: 7
Joined: Sat Sep 18, 2004 4:55 pm

Post by cravikiran »

Well, as I said, this function is from my wiki-type site Sourceworld. Currently, the site has a very limited set of documents and so it is difficult to show the pagination. However, here's an example where I have reduced the results to 1 per page (there are 3 documents right now)... http://sourceworld.sourceforge.net/inde ... ents&rpp=1. This allows you to view the pagination (since it returns nothing if there is only 1 page).
Post Reply