showingFromTo(); Of course to keep the results per page limited you will need to query the DB on every page call to get a result resource to loop through. Use this per page result set to loop through and display your data. $page_result = $pgn8->findPageResult(); while($info = mysql_fetch_assoc($page_result)) { do stuff } And at the end of it all you can display your styled list of page numbers echo $pgn8->displayPagination(); // show clickable page numbers Yes, you can also use displayPagination() before you display your data loop to have it on the top as well as the bottom ///// optional parameters /////////////////////////////////////////////////// Set the base url to be used on all the page number links example: page.php or the result of my thispage() function or one of the $_SERVER['phpself thingies] $pgn8->base_url=thispage(); Do not use a query string when you set this value since the class appends '?page=2' to the links for the pagination. If you have more arguements you need to pass via query string use the appendURLString() function to append to the ?page=2[&this=that&apple=red] etc You can overwrite the whole url string using setUrlString or just append to it. $pgn8->appendUrlString("&island=eleuthera&settlement=tbay"); functions are camelCapped variables are under_scored usually... ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */ class pagination { ///////////////////////////////////////////////// // PUBLIC VARIABLES ///////////////////////////////////////////////// var $base_url; var $limit_per_page; var $url_string; // the display of 'showing from to' and the page numbers // can be turned off by setting these to FALSE var $display_showing_from = TRUE; var $display_pagination = TRUE; var $display_label = TRUE; var $display_first_last = TRUE; var $modRewrite = FALSE; // controls how the page number will be appended to the url /** * If you need separate areas of a site to use pagination * but don't want their sql clashing when going from section to section, * use a different session variable name on each page * for the stored sql used by the class * @var string */ var $session_var = 'pgn8_search_sql'; var $sql; // sql string that will find all of whats requested ///////////////////////////////////////////////// // VARIABLES CALCULATED INTERNALLY ///////////////////////////////////////////////// var $total_found; // total number of items found with $sql var $pages; // total number of pages needed based on calculating limit per page and amount found var $current_page; var $offset; var $limitstring; ///////////////////////////////////////////////// // VARIABLE METHODS ///////////////////////////////////////////////// function setBaseUrl($url) { $this -> base_url = $url; } function setUrlString($value) // overwrites url_string { $this -> url_string = $value; } function appendUrlString($value) // attaches to the end of url_string { $this -> url_string .= $value; } ///////////////////////////////////////////////// // RUNS ON CREATION ///////////////////////////////////////////////// /** * :: INITIALIZING METHOD :: * this function will auto run * upon the creation of a new pagination object * @param string $sql * @param int $sql * @return void */ function pagination($sql, $perpage=10, $session_varname='pgn8_sql') { $this->sql = $sql; $this->limit_per_page = $perpage; $this->session_var = $session_varname; $this->determinePage(); $this->calcOffset(); $this->setBaseUrl(basename($_SERVER['PHP_SELF'])); $this->findTotalResult(); $this->calcPages(); } ///////////////////////////////////////////////////////// // INTERNAL CALCULATION METHODS ///////////////////////////////////////////////////////// function findTotalResult() { $total_result = mysql_query($this->sql) or die('pgn8-findTotalResult-'.mysql_error()); $total = mysql_num_rows($total_result); $this->setTotalFound($total); return $total; } function setTotalFound($value) { // if the total found is 0, disable the showing of 'pagination' and 'showing from to' if($value<=0) { $this -> display_showing_from = FALSE; $this -> display_pagination = FALSE; } else { $this->total_found = $value;} } function calcOffset() { // calcOffset calculates the difference needed to select // just the range of results needed for one page // the vars needed for the calculation are the same ones used in the limitstring // so fill in the limitstring var at the same time which is appened to the sql var $this->offset = ($this->current_page*$this->limit_per_page) - $this->limit_per_page; $this->limitstring=" LIMIT {$this->offset}, {$this->limit_per_page}"; } function findPageResult() { $page_result = mysql_query($this->sql.$this->limitstring) or die('pgn8-findPageResult-'.mysql_error()); return $page_result; } ///////////////////////////////////////////////////////// // NON - SQL STUFF BELOW ///////////////////////////////////////////////////////// function determinePage() { if(isset($_GET['page']) and $_GET['page'] != 0) { $this->setCurrentPage($_GET['page']); // if i'm coming through a pagination link just use the stored sql // in the $_SESSION IF IT EXISTS // instead of rebuilding it. } if(!empty($_SESSION[$this->session_var])) { $this->sql = $_SESSION[$this->session_var]; } } else { $this->setCurrentPage(1); $_SESSION[$this->session_var] = $this->sql; } } function setCurrentPage($value) { $this->current_page = (!is_numeric($value) || $value > $this->pages || $value < 1) ? intval($value) : 1 ; } function calcPages() { // how many pages needed to display all the found results $this->pages = ceil($this->total_found / $this->limit_per_page); // based on the current page, what would be 5 pages before and 5 pages after if($this->current_page > 6) { $this->rangeStart = $this->current_page - 4; } else {$this->rangeStart = 1;} if($this->current_page <= 6) { $this->rangeEnd = $this->current_page + (10 - $this->current_page); } else { $this->rangeEnd = $this->current_page+5; if($this->rangeEnd > $this->pages) { $this->rangeEnd = $this->pages; } } if($this->current_page > $this->pages - 6) { $this->rangeStart = $this->pages - 9; } if($this->rangeStart < 1){ $this->rangeStart = 1;} if($this->rangeEnd >= $this->pages) { $this->rangeEnd = $this->pages; } } ///////////////////////////////////////////////// // OUTPUT METHODS ///////////////////////////////////////////////// function showingFromTo() { $this->calcOffset(); // just in case it wasn't already calculated $start = $this->offset + 1; $finish = $this->offset + $this->limit_per_page; if ($finish >= $this->total_found) { $finish = $this->total_found; } $to_echo = "
Showing $start - $finish of {$this->total_found} Results Found
"; if($this->display_showing_from === TRUE) {return $to_echo;} else {return '';} } function displayPagination() { $to_echo = ''; $to_echo .= '' . "\n"; if($this->display_pagination === TRUE) {return $to_echo;} else {return '';} } } //// end class //// //////////////////// /* ////// N O T E S ///////// IMPORTANT NOTES FOR USING WITH MOD-REWRITE * your html pages MUST have a tag in the header or else the base_url appended to all the links will just grow longer and longer with each page For css styling purposes a class of 'clearfix' is on the wrapping div and ul. .clearfix fixes the clearing floats problem using the method documented on positioniseverything.net :::Sample CSS Styling for display_pagination()::: /* /* Change Log --------------- June 30th * changed clearfix class because on some layouts having a clearfixed element in the content area could cause all the content to be forced below the left or right navigation. Besides, i added clearfix to the back when it was just displaying a page number for every page found (up in the hundreds on some search) and I needed the container to expand with the floated page numbers within it. But now that it only shows 10 pages at a time, needing only one line, the clearfix is not really needed :) June 19th, 2006 * accidentally used $pgn8-> instead of $this-> in the determinePage function that caused problems. * had to add the ability to change the name of the session_var in the default class so it happens upon the creation of the object June 16th, 2006 * Straightened out the naming conventions of the variables and functions. * Added mod-rewrite compatability * Added ability to change the name of the session variable containing the sql using setSessionVar() June 6th, 2006 * Continuing the major overhaul. Just trying to make it a bit 'smarter' overall. * Fixed the problem with the negative page ranges and incorrect appearance/disappearance of the first and last links * Updated the example css * Also renamed some of the toggle vars to be display_etc... * Could use a bit more documentation in the user guide area for the optional vars * Copied to the common site folder May 31, 2006 Updated lots and lots of stuff moved most of the stuff into the actual class and made it all more automated To invoke the pagination stuff in the page takes a lot less lines of code now. Feb 3, 2006 Copied to default site folder/scripts Jan 04, 2006: converted to object oriented class Oct 21, 2005: use of $this-> not allowed in PHP5+ because $this is a special reserved word for object oriented programming similar to 'this' in javascript Aug 15, 2005: fixed display_pagination_range function to show from offset to final total instead of from offset to +limit on the final page of search results. ie. from 41-47 when only 47 total instead of 41-50 with only 47 total */ ?>