= 5.2.0 * @author emptyhua@gmail.com * @link http://bluehua.org */ class Pager { public $template = <<首页{/first} {pre}上一页{/pre} {next}下一页{/next} {last}末页{/last} {start}-{end}/{count} EOF; public $count = 0; public $count_per_page = 0; public $param = 'page'; public function page_count() { return ceil($this->count/$this->count_per_page); } /** * 返回当前页码 * * @return int */ public function current_page() { static $current; if (!empty($current)) return $current; if (!empty($_GET[$this->param])) $current = intval($_GET[$this->param]); else $current = 1; return $current; } /** * 按照分页返回数组片断 * * @param array $a * @return array */ public function slice(&$a) { $start = ($this->current_page() - 1) * $this->count_per_page; $end = $start + $this->count_per_page - 1; $rt = array(); while($start < $end) { if (!isset($a[$start])) break; $rt[] = $a[$start]; $start ++; } return $rt; } /** * 返回获取当前页数据的sql * * @return string */ public function get_sql($sql) { return str_replace('{limit}', 'limit ' . ($this->current_page() - 1) * $this->count_per_page . ',' . $this->count_per_page, $sql); } public function get_limit() { return array(($this->current_page() - 1) * $this->count_per_page, $this->count_per_page); } public function get_url($url, $page) { $current = $this->current_page(); $page_count = $this->page_count(); if (strpos($url, '?') === false) $url .= sprintf('?%s=%d', $this->param, $page); else $url .= sprintf('&%s=%d', $this->param, $page); return $url; } /** * 渲染分页 * @param string $url 默认为当前页的url */ public function render($url = null) { echo $this->_render($url); } public function _render($url) { $current = $this->current_page(); $template = $this->template; $page_count = $this->page_count(); $url = is_null($url) ? $_SERVER["REQUEST_URI"] : $url; $url = preg_replace('/&?' . $this->param . '=[^&]*/', '', $url); $url = str_replace('&', '&', $url); $start = ($this->current_page() - 1) * $this->count_per_page + 1; $end = min($start + $this->count_per_page - 1, $this->count); $template = str_replace('{count}', $this->count, $template); $template = str_replace('{count_per_page}', $this->count_per_page, $template); $template = str_replace('{start}', $start, $template); $template = str_replace('{end}', $end, $template); if ($current == 1) { $template = preg_replace('/\{first\}.*?\{\/first\}/', '', $template); $template = preg_replace('/\{pre\}.*?\{\/pre\}/', '', $template); } if ($current == $page_count) { $template = preg_replace('/\{last\}.*?\{\/last\}/', '', $template); $template = preg_replace('/\{next\}.*?\{\/next\}/', '', $template); } $template = str_replace('{first_url}', $this->get_url($url, 1), $template); $template = str_replace('{last_url}', $this->get_url($url, $page_count), $template); $template = str_replace('{pre_url}', $this->get_url($url, $current - 1), $template); $template = str_replace('{next_url}', $this->get_url($url, $current + 1), $template); $matches = array(); preg_match_all('/\{pages(\d*)\}(.*?)\{\/pages\d*\}/', $template, $matches); $match_c = count($matches[0]); for ($i = 0; $i < $match_c; $i ++) { $match = $matches[0][$i]; $o_html = $matches[2][$i]; if ($matches[1][$i] !== '') { $c_page_count = intval($matches[1][$i]); $start_p = max($current - floor($c_page_count/2) + 1, 1); $end_p = min($current + ($c_page_count - ($current - $start_p)) - 1, $page_count); } else { $c_page_count = $page_count; $start_p = 1; $end_p = $page_count; } $html = ''; $_tmp = ''; while($start_p <= $end_p) { $_tmp = $o_html; $_tmp = str_replace('{page_url}', $this->get_url($url, $start_p), $_tmp); $_tmp = str_replace('{page_num}', $start_p, $_tmp); if ($current != $start_p) { $_tmp = preg_replace('/\{current\}.*?\{\/current\}/', '', $_tmp); } $html .= $_tmp; $start_p ++; } $template = str_replace($match, $html, $template); } $template = preg_replace('/\{[^\}]+\}/', '', $template); return $template; } } if (realpath($_SERVER['SCRIPT_FILENAME']) == __FILE__) { $p = new Pager; $p->count = 100; $p->count_per_page = 20; $p->param = 'dongganchaoren'; echo ''; $p->render(); echo '
';
    echo $p->get_sql('select * from xxx where xxx {limit}');
    echo '
'; echo ''; } ?>