Requirements: An sql class with the following methods: sqlQuery sqlNumRows sqlFetchAssoc sqlFetchArray */ /* Example Usage: // This code should be on a seperate page. require_once('class.sql.php'); require_once('class.paging.php'); $total_query = 'select count(id) from table'; $paging_query = 'select id,name,date from table'; // Create paging object $paging_obj = new paging($total_query,$paging_query,'page',10,5,'#ffffff,#eeeeee','No records were found.'); // Output viewing text, paging links, records table, and paging links again $paging_obj->page_links = 'both'; echo $paging_obj->viewing; $paging_obj->build(); */ class paging extends sql { // Public var $per_page; // Int -> How many records per page var $per_row; // Int -> How many columns per row var $row_colors; // Arr -> CSS Class names to apply to alternate rows var $none_found; // Str -> Message to show user if no results were found var $rs; // Arr -> Stores the found record sets to be manipulated before building table var $get_page_var; // Str -> Name of the get variable that holds paging data var $page_links; // Str -> Where to put next/previous links. top, bottom or both as values var $eval_string; // Str -> String that will be executed in the eval() to build the cell data var $num_links; // Int -> Number of number links (page links) to show, set to zero to show none var $page_base; // Str -> Base URL for paging links // Private var $sql; // Obj -> Object of the sql class var $total; // Int -> Number of records found in total var $pages; // Int -> Total number of pages var $start_num; // Int -> Start offset for record Limit var $found; // Int -> Number of records found for the current page var $html; // Str -> Holds the html output var $viewing; // Str -> Holds the viewing 1 - 1 of 1 string function paging( $total_query,$page_query,$get_page_var="page",$per_page=20,$per_row=5,$row_colors="",$none_found="No results were found." ) { // Create and open sql object $this->sql(); $this->total = $this->get_total( $total_query ); $this->per_page = $per_page; $this->pages = ceil( $this->total / $this->per_page ); $this->get_page_var = $get_page_var; $this->page = !isset( $_GET[$this->get_page_var] ) || $_GET[$this->get_page_var] <= 0 || $_GET[$this->get_page_var] > $this->pages || !is_numeric( $_GET[$this->get_page_var] ) ? 1 : $_GET[$this->get_page_var]; $this->per_row = $per_row; $this->row_colors = $row_colors; $this->start_num = $this->page == 1 ? 0 : ( $this->per_page * ( $this->page - 1 ) ); $this->row_colors = @explode( ",",$row_colors ); $this->none_found = $none_found; $this->num_links = 5; // Retreive this pages records $this->get_page_records( $page_query ); } // Get the total records available FOR PAGING function get_total( $query ) { $this->sqlQuery( $query ); $result = $this->sqlFetchArray(); return $result[0]; } // Get the total records available FOR THIS PAGE function get_page_records( $query ) { // Append start_num and max limit on query $query = trim( $query ) . " LIMIT $this->start_num,$this->per_page"; // Execute query $this->sqlQuery( $query ); // Set found property $this->found = $this->page == 1 ? $this->sqlNumRows() : ( $this->per_page * ( $this->page - 1 ) ) + $this->sqlNumRows(); // Store returned record sets if( $this->sqlNumRows() > 0 ) { // Store returned record sets while( $rs = $this->sqlFetchAssoc() ) { foreach( $rs as $key => $value ) $rs[$key] = is_string( $value ) ? stripslashes( $value ) : $value; $this->rs[] = $rs; } } } // Build paging table function build() { if( !$this->page_links ) $this->page_links = "bottom"; else $this->page_links = strtolower( $this->page_links ); // Open table $this->html = ""; // Loop page record sets if( $this->found > 0 ) { // Viewing n - n of n $this->viewing = "Viewing " . ( $this->start_num + 1 ) . " - $this->found of $this->total"; // Put the page links at the top? if( $this->page_links == "top" || $this->page_links == "both" ) $this->html .= $this->build_page_links(); // Open paging row $this->html .= " row_colors[0] . "\">"; // Init row count $row_count = 0; if( !isset( $this->rs[0] ) ) die( "Record sets must be combined into a single pre-formatted key or query failed" ); foreach( $this->rs as $rs ) { // Do we need to alternate colors per table row? if( sizeof( $this->row_colors ) > 0 ) { // Incriment array key $row_class_key += !isset( $row_class_key ) ? 0 : 1; // Make sure this is a valid array key, else, reset to first key if( !isset( $this->row_colors[$row_class_key] ) ) $row_class_key = 0; // Get class name $row_class_name = $this->row_colors[$row_class_key]; } // Do we need a new table row? if( $row_count == $this->per_row ) { // Reset count $row_count = 0; // Create a new table row $this->html .= " "; } // Build cell contents $this->html .= " "; // Incriment table row count $row_count++; } // Add empty table data cells if needed to complete table row while( $row_count != $this->per_row ) { // Add empty cell $this->html .= " "; // Incriment row count $row_count++; } // Close paging row $this->html .= " "; // Put the page links at the bottom? if( $this->page_links == "bottom" || $this->page_links == "both" ) $this->html .= $this->build_page_links(); } // No records found else { $this->html .= " "; } // Close table $this->html .= "
" . eval( $this->eval_string ) . " 
$this->none_found
"; } // Build page links function build_page_links($divider=' | ') { $table = ""; if( $this->page > 1 || $this->page != $this->pages && $this->total > 0 ) { // Add paging querystring variables if( !strstr( $this->page_base,"&sort_on=" ) ) $this->page_base .= "&sort_on=" . $_GET["sort_on"]; if( !strstr( $this->page_base,"&sort_order=" ) ) $this->page_base .= "&sort_order=" . $_GET["sort_order"]; // Start table $table .= " per_row\" width=\"100%\"> "; // Link base if( strlen( $this->page_base ) == 0 ) $this->page_base = str_replace( "&".$this->get_page_var."=".$_GET[$this->get_page_var],"",$_SERVER["REQUEST_URI"]."?".$_SERVER["QUERY_STRING"] ); // Build page number links if( $this->num_links > 0 ) { $pos = floor( $this->page / $this->num_links ) * $this->num_links; if( $pos == 0 ) { $start = 1; $end = $this->num_links > $this->pages ? $this->pages : $this->num_links; } else { $start = $pos - 1; if( $pos + $this->num_links > $this->pages ) $end = $this->pages; else $end = $pos + $this->num_links > $this->pages ? $this->pages : $pos + $this->num_links; } // Prefix $paging_numbers = "$dividerskip to page "; // Loop and build paging numbers if( $start == 1 ) { if( $this->page == 1 ) $paging_numbers .= '1 '; else $paging_numbers .= "page_base&$this->get_page_var=1\" class=\"paging_links\">1 "; $start = 2; } else { $paging_numbers .= "page_base&$this->get_page_var=$start\" class=\"paging_links\">...$start "; $start++; } // Middle numbers for( $j=$start;$j<$end;$j++ ) { if( $j < $end ) $paging_numbers .= $j == $this->page ? '$j'.$divider : "page_base&$this->get_page_var=$j\" class=\"paging_links\">$j "; else $paging_numbers .= "page_base&$this->get_page_var=$j\" class=\"paging_links\">$j..."; } // Last number if( $end < $this->pages ) $paging_numbers .= "page_base&$this->get_page_var=$end\" class=\"paging_links\">$end..."; else $paging_numbers .= $end == $this->page ? $end : "page_base&$this->get_page_var=$end\" class=\"paging_links\">$end"; } else { $page_links = ""; } // Viewing n - n of n and skip to $table .= " "; // Next/Previous links $table .= "
Viewing " . ( $this->start_num + 1 ) . " - $this->found of $this->total total $paging_numbers"; // Show previous link? if( $this->page > 1 ) { // First link // $table .= "‹‹ get_page_var=1\" class=\"paging_links\">first page "; // Previous link if( $this->pages > 1 ) $table .= "‹ page_base&$this->get_page_var=" . ( $this->page - 1 ) . "\" class=\"paging_links\">previous page"; } // Do we need a previous/next divider? if( $this->page > 1 && $this->page != $this->pages ) $table .= $divider; // Show next link? if( $this->page != $this->pages ) { // Next link if( $this->pages > 1 ) $table .= "page_base&$this->get_page_var=" . ( $this->page + 1 ) . "\" class=\"paging_links\">next page ›"; // Last link // $table .= " get_page_var=$this->pages\" class=\"paging_links\">last page ››"; } // Close cell $table .= "
"; } // Return the table html if(isset($table)) return $table; } } ?>