public static function paginate() { $output = ''; self::$total_pages = ceil(self::$_total_results / self::$_per_page); if (self::$total_pages <= 1) { return false; } if (self::$_current_page > self::$total_pages) { self::$_current_page = self::$total_pages; } if (self::$total_pages <= 5) { self::$_padding_l = self::$_current_page; self::$_padding_r = self::$total_pages - self::$_current_page; self::$tpl_last = NULL; self::$tpl_first = NULL; } elseif (self::$total_pages > 5) { if (self::$_current_page < 4) { self::$tpl_first = NULL; self::$_padding_r = 4 - self::$_current_page; $remain = self::$total_pages - self::$_current_page; self::$_padding_l = $remain == 2 ? 1 : ($remain == 0 ? 3 : 2); } else { if (self::$total_pages - self::$_current_page > 2) { self::$_padding_l = 1; } else { $remain = self::$total_pages - self::$_current_page; self::$_padding_l = $remain == 2 ? 1 : ($remain == 0 ? 3 : 2); } self::$_padding_r = self::$total_pages - self::$_current_page > 2 ? 1 : self::$total_pages - self::$_current_page; if (self::$_current_page + 2 >= self::$total_pages) { self::$tpl_last = NULL; } } } $start = self::$_current_page - self::$_padding_l < 1 ? 1 : self::$_current_page - self::$_padding_l; $finish = self::$_current_page + self::$_padding_r; ########################################## # ADD PREV TO OUTPUT IF CURRENT PAGE > 1 # ########################################## if (self::$_current_page > 1 && self::$tpl_prev != NULL) { $output .= preg_replace('/\\{link\\}/i', self::$link_prefix . (self::$_current_page - 1) . self::$link_suffix, self::$tpl_prev); } ########################################### # ADD FIRST TO OUTPUT IF CURRENT PAGE > 1 # ########################################### if (self::$_current_page > 1 && self::$tpl_first != NULL) { $patterns = array('/\\{link\\}/i', '/\\{page\\}/i'); $replaces = array(self::$link_prefix . '1' . self::$link_suffix, '1'); $output .= preg_replace($patterns, $replaces, self::$tpl_first); } ################################################ # GET LIST OF LINKED NUMBERS AND ADD TO OUTPUT # ################################################ $nums = array(); for ($i = $start; $i <= $finish; $i++) { if ($i == self::$_current_page) { $nums[] = preg_replace('/\\{page\\}/i', $i, self::$tpl_cur_page_num); } else { $patterns = array('/\\{link\\}/i', '/\\{page\\}/i'); $replaces = array(self::$link_prefix . $i . self::$link_suffix, $i); $nums[] = preg_replace($patterns, $replaces, self::$tpl_page_nums); } } $output .= implode(self::$separator, $nums); ############################################ # ADD LAST TO OUTPUT IF FINISH < MAX PAGES # ############################################ if (self::$_current_page < $finish && self::$tpl_last != NULL) { $patterns = array('/\\{link\\}/i', '/\\{page\\}/i'); $replaces = array(self::$link_prefix . self::$total_pages . self::$link_suffix, self::$total_pages); $output .= preg_replace($patterns, $replaces, self::$tpl_last); } ################################################## # ADD NEXT TO OUTPUT IF CURRENT PAGE < MAX PAGES # ################################################## if (self::$_current_page < self::$total_pages && self::$tpl_next != NULL) { $output .= preg_replace('/\\{link\\}/i', self::$link_prefix . (self::$_current_page + 1) . self::$link_suffix, self::$tpl_next); } return self::$_output = sprintf(self::$outside_template, $output); }