« | '; public $tpl_last = ' | » '; public $tpl_prev = ' | '; public $tpl_next = ' | '; public $tpl_page_nums = '{page}'; public $tpl_cur_page_num = '{page}'; /** * In the above templates {link} is where the link will be inserted and {page} is * where the page numbers will be inserted. Other than that, you can modify them * as you please * * NOTE: You should have a separator of some sort at the right of $tpl_first and * $tpl_prev as above in the defaults, and also have a separator of some sort * before the $tpl_next and $tpl_last templates **/ ################################################################################## public function __construct($page, $query) { #Check page number is a positive integer greater than 0 and assign it to $this->_current_page if ((int)$page > 0) $this->_current_page = (int)$page; #Remove any LIMIT clauses in the query string and set if $query = trim(preg_replace('/[\s]+LIMIT[\s]+\d+([\s,]*,[^\d]*\d+)?/i', '', $query)); if (empty($query)) { return false; } else { $this->_query = $query; } } /** * pagination::paginate() * * Processes all values and query strings and if successful * returns a string of html text for use with pagination bar * * @return string; */ public function paginate() { $output = ''; ######################################### # GET TOTAL NUMBER OF RESULTS AND PAGES # ######################################### $result = mysql_query($this->_query); if (!$result) { $this->error = __line__ . ' - ' . mysql_error(); return false; } $this->total_results = mysql_num_rows($result); $this->total_pages = ceil($this->total_results / $this->results_per_page); ######################## # FREE RESULT RESOURCE # ######################## ################################ # IF TOTAL PAGES <= 1 RETURN 1 # ################################ if ($this->total_pages <= 1) { $this->_results_resource = $result; $this->_output = '1'; return $this->_output; } mysql_free_result($result); ################################################### # CHECK CURRENT PAGE ISN'T GREATER THAN MAX PAGES # ################################################### if ($this->_current_page > $this->total_pages) $this->_current_page = $this->total_pages; ###################################### # SET FIRST AND LAST PAGE VALUES AND # # ERROR CHECK AGAINST INVALID VALUES # ###################################### $start = ($this->_current_page - $this->_padding > 0) ? $this->_current_page - $this-> _padding : '1'; $finish = ($this->_current_page + $this->_padding <= $this->total_pages) ? $this-> _current_page + $this->_padding : $this->total_pages; ########################################### # CREATE LIMIT CLAUSE AND ASSIGN TO QUERY # ########################################### $limit = ' LIMIT ' . ($this->results_per_page * ($this->_current_page - 1)) . ',' . $this->results_per_page; $query = $this->_query . $limit; ############################################# # RUN QUERY AND ASSIGN TO $_result_resource # ############################################# $result = mysql_query($query); if ($result === false) { $this->error = __line__ . ' - ' . mysql_error(); return false; } $this->_results_resource = $result; ########################################### # ADD FIRST TO OUTPUT IF CURRENT PAGE > 1 # ########################################### if ($this->_current_page > 1) { $output .= preg_replace('/\{link\}/i', $this->link_prefix . '1' . $this-> link_suffix, $this->tpl_first); } ########################################## # ADD PREV TO OUTPUT IF CURRENT PAGE > 1 # ########################################## if ($this->_current_page > 1) { $output .= preg_replace('/\{link\}/i', $this->link_prefix . ($this-> _current_page - 1) . $this->link_suffix, $this->tpl_prev); } ################################################ # GET LIST OF LINKED NUMBERS AND ADD TO OUTPUT # ################################################ $nums = array(); for ($i = $start; $i <= $finish; $i++) { if ($i == $this->_current_page) { $nums[] = preg_replace('/\{page\}/i', $i, $this->tpl_cur_page_num); } else { $patterns = array('/\{link\}/i', '/\{page\}/i'); $replaces = array($this->link_prefix . $i . $this->link_suffix, $i); $nums[] = preg_replace($patterns, $replaces, $this->tpl_page_nums); } } $output .= implode($this->page_nums_separator, $nums); ################################################## # ADD NEXT TO OUTPUT IF CURRENT PAGE < MAX PAGES # ################################################## if ($this->_current_page < $this->total_pages) { $output .= preg_replace('/\{link\}/i', $this->link_prefix . ($this-> _current_page + 1) . $this->link_suffix, $this->tpl_next); } ############################################ # ADD LAST TO OUTPUT IF FINISH < MAX PAGES # ############################################ if ($this->_current_page < $finish) { $output .= preg_replace('/\{link\}/i', $this->link_prefix . $this->total_pages . $this->link_suffix, $this-> tpl_last); } $this->_output = $output; return $output; } /** * pagination::padding() * * Sets the padding for the pagination string * * @param int $val * @return bool */ public function padding($val) { if ((int)$val < 1) return false; $this->_padding = (int)$val; return true; } /** * pagination::resource() * * Returns the resource of the results query * * @return resource */ function resource() { return $this->_results_resource; } /** * pagination::__tostring() * returns the last pagination output * * @return string */ function __tostring() { if (trim($this->_output)) { return trim($this->_output); }else{ return ''; } } }