Knowledge Base Navigation

Articles: 51 Categories: 8

KB Article: IPB like page numbers function

Article:IPB like page numbers function     Popular
Submitted By:Telli
Date Added:02-04-2008 3:04:56
Hits:41,787



There are 3 files that you will need to use.

1. CSS


.pagination {
   text-decoration: none;
   margin: 0px auto 0px auto;
}
.pagenumselect{
        font-size: 11px;
   background: #fff;
}
.pagenum,
.pagenumlast,
.pagecurrent{
   background: #fff;
   border: 1px solid #5379b8;
   padding: 1px 3px 1px 3px;
}
.pagenumlast{
   background: #efefef;
}
.pagecurrent{
   background: #cecece;
}
.pagenum a:active,
.pagenum a:visited,
.pagenum a:link,
.pagenumlast a:active,
.pagenumlast a:visited,
.pagenumlast a:link,
.pagecurrent a:active,
.pagecurrent a:visited,
.pagecurrent a:link{
   text-decoration: none;
}


You can add the CSS to an existing style sheet or you can make a new one for it thats up to you. Change the colors as you see fit. Its set up to use your site's default font, and you may have to adjust the sizes to fit your needs.


2. JavaScript (for dropdown page numbers)


function jumpto(selObj, target){
   target = (target) ? target : 'parent';
   gourl = selObj.options[selObj.selectedIndex].value;
   if (gourl) {
      eval(target+".location='"+gourl+"'");
   }
}


Same thing with the JavaScript you can add it to an existing js file or make a new one for it.

3. Pagenumber function


<?php
/*********************************************
  IPB emulation pagination
  ********************************************
  Copyright © 2002-2008 by Codezwiz Network, LLC.
  http://www.codezwiz.com/

  A free program released under the terms and conditions
  of the GNU GPL version 2 or any later version

  IPB like page numbers, for support goto http://www.codezwiz.com
  Instructions: http://www.codezwiz.com/kbview-55-ipb-like-page-numbers-function.html
***********************************************************************/
function pagination($page_url, $num_pages, $per_page, $start_page, $prevnext = true) {
   $total_pages = ceil($num_pages/$per_page);
   $on_page = floor($start_page / $per_page) + 1;
   $max_links_shown = 5;
   if ($total_pages == 1) { return ''; }
   $pagination = '';
   if($total_pages > 1) {
      $pagination .= "<div class=\"pagination\"><br />\n";
      if(($max_links_shown%2 != 0)) {
         $first_link = $on_page - (($max_links_shown - 1)/2);
         $last_link = $on_page + (($max_links_shown - 1)/2);
      } else {
         $first_link = $on_page - ($max_links_shown/2) + 1;
         $last_link = $on_page + ($max_links_shown/2);
      }
      if($first_link < 1) {
         $last_link -= ($first_link-1);
         $first_link = 1; //set first link to one
      }
      if($last_link > $total_pages) {
         if ($first_link > 1) {
            $first_link -= ($last_link-$total_pages);
            if ($first_link < 1) {
               $first_link = 1;
            }
         }
         $last_link = $total_pages;
      }
      $s = ($total_pages <> 1) ? 's' : '';
      $show_on_page = ($on_page > 1) ? 'Page ' . $on_page . ' of ' . $total_pages : $total_pages . ' page' . $s;
      if ($total_pages > 5) {
         $pagination .= "<span class=\"pagenum\">$show_on_page</span> ";
         $pagination .= "<select class=\"pagenumselect\" name=\"generate_pagination\" onchange=\"jumpto(this);\">";
         for($i = 1; $i <= $total_pages; $i++) {
            $selected = ( $i == $on_page ) ? ' selected="selected"' : ''; // highlight current page by default
            $pagination .= '<option value="' . $page_url . '&start=' . ( ( $i - 1 ) * $per_page ) . '"' . $selected . '>' . $i . '</option>';
         }
         $pagination .= '</select> ';
      }
      if ($on_page > 1) {
            $idp  = $on_page;
         $add = $start_page - $per_page;
         if ($on_page > 2) {
            if ($prevnext) {
                  $pagination .= "<span class=\"pagenumlast\"><a href=\"" . $page_url . "\" title=\"Go to first page\">«</a></span> ";
            }
         }
         if ($prevnext) {
               $pagination .= "<span class=\"pagenum\"><a title=\"Previous page\" href=\"" . $page_url . "&start=" . $add . "\"><</a></span> ";
         }
      }
      for($idp = $first_link; $idp <= $last_link; $idp++) {
         if ($idp == $on_page) {
               $pagination .= "<span class=\"pagecurrent\">$idp</span> "; // no need to create a link to current page
         } else {
               $pagination .= "<span class=\"pagenum\"><a title=\"$idp\" href=\"".$page_url . "&start=" . ( ( $idp - 1 ) * $per_page ) . "\">$idp</a></span> ";
         }
      }
      if ($on_page < $total_pages) {
         if ($start_page > 0) {
            $idp = $on_page * $per_page;
         } else {
            $idp = $per_page;
         }
         $nstart = ($total_pages - 1) * $per_page;
         if ($prevnext) {
            $pagination .= "<span class=\"pagenum\"><a title=\"Next page\" href=\"" . $page_url . "&start=" . $idp . "\">></a></span> ";
         }
         if ($on_page < $total_pages - 2) {
            if ($prevnext) {
               $pagination .= "<span class=\"pagenumlast\"><a title=\"Go to last page\" href=\"" . $page_url . "&start=" .$nstart . "\">»</a></span>";
            }
         }
      }
      $pagination .= "</div>\n";
      return $pagination;
   }
}
?>


Save that as pagination.php and include it where you will use the page numbers. Example:


include('pagination.php');


Ok! Now the easy part is over it's time for editing.

Wherever you want to use the pagination you will have to add a few PHP variables. Read the notation to help you understand how to set it up.


//include the pagination function
include('pagination.php');

//current page (you should not change this)
$start = (isset($_POST['start']) ? intval($_POST['start']) : (isset($_GET['start']) ? intval($_GET['start']) : 0));

//how many per page
$perpage = '10';

//this would be an added variable (category)
$cat = (isset($_POST['cat']) ? intval($_POST['cat']) : (isset($_GET['cat']) ? intval($_GET['cat']) : 0));


Now we have to have to make a few changes the where the data is called. This is just a guide and will have to changed for it to work in your site.


//if you where using another variable to call the data you want to add it here also so the page numbers add up
//I added cat as an example if you where using categories for instance.
list($num_pages) = $db->sql_fetchrow($db->sql_query("SELECT COUNT(*) FROM {YOUR_TABLE} WHERE cat = '$cat'")); //total pages

//this is where you get the data. Notice we added "LIMIT $start, $perpage" on the end of the query
$result = $db->sql_query("SELECT something FROM {YOUR_TABLE} WHERE cat = '$cat' ORDER BY something LIMIT $start, $perpage");
if ($db->sql_numrows($result) > 0) {
   while($row = $db->sql_fetchrow($result)) {
      //your data is here
      $something = $row['something'];
      echo $something;
   }
   $db->sql_freeresult($result);
   //This is the pagination.
   //The first item is the page URL and op if needed. You would also add any other variable you may have to pass to the next page.
   //Next item is total pages
   //Next item is per page
   //Last item is page there on
   $add_url = ($cat > 0) ? '&amp;cat=' . $cat : '';
   echo pagination('modules.php?name=Module&op=Someop' . $add_url, $num_pages, $perpage, $start);
} else {
   echo "Nothing here check back later...";
}


With a basic understanding of PHP this should be pretty easy to implement. If you have any problems leave a comment and we will try to help you out.

Current rating: 7.61 by 71 users
Please take one second and rate this article...

Not a Chance 12345678910 Absolutely

Please register or sign-in to post comments.


Jump to a selected article...