Usage of PHP paging class
page.class.php
class Page { private $total_rows;//Total number of entries in the database private $per_page_rows;//Number of entries displayed per page private $limit; private $ uri; private $total_pages;//Total number of pages private $config=array("header"=>"Number of records","prev"=>"Previous page","next"=>" Next page","first"=>"Home page","last"=>"Last page");
private $list_length=8;
public function __construct($total_rows,$per_page_rows=10,$url_args) {
$this->total_rows=$total_rows;
$this->per_page_rows=$per_page_rows;
$this->uri=$this->get_uri($url_args);
$this->page = !empty($_GET['page']) ? $_GET['page'] : 1;
$this->total_pages=ceil($this->total_rows/$this->per_page_rows);
$this ->limit=$this->set_limit();
}
private function set_limit() {
return "limit ".($this->page-1)*$this->per_page_rows.",{ $this->per_page_rows}";
}
private function get_uri($url_args) {
$url=$_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"],"?") ? "" : "?").$url_args;
$parse=parse_url($url);
if (isset($parse['query'])) {
parse_str($parse['query'],$params);/ /Parse the url string into an array
unset($params['page']);//Delete the value whose subscript is page in the array
$url=$parse['path'].'?'.http_build_query($params );//Build the url again
}
return $url;
}
public function __get($args) {
if ($args=="limit") {
return $this->limit;
}else{
return null;
}
}
private function start_page(){
if ($this->total_rows==0) {
return 0;
}else{
return (($this->page-1) *$this->per_page_rows)+1;
}
}
private function end_page(){
return min($this->page*$this->per_page_rows,$this->total_rows);
}
private function go_first() {
$html="";
if ($this->page==1) {
$html.=" {$this->config['first']}  ;";
}else{
$html.=" {$this->config['first']}< /a> ";
}
return $html;
}
private function go_prev() {
$html="";
if ($this->page==1) {
$html.=" {$this->config['prev']} ";
}else{
$html.=" {$this->config['prev']} ";
}
return $html;
}
private function go_next() {
$ html="";
if ($this->page==$this->total_pages) {
$html.=" {$this->config['next']} ";
}else{
$html.=" {$this->config['next ']} ";
}
return $html;
}
private function go_last() {
$html="";
if ($this->page==$this-> ;total_pages) {
$html.=" {$this->config['last']} ";
}else{
$html.=" {$this->config['last']} ";
}
return $html;
}
private function go_page() {
return ' ' ;
}
private function page_list() {
$link_page="";
$i_num=floor($this->list_length/2);
for ($i = $i_num; $i >= 1; $ i--) {
$page=$this->page-$i;
if ($page<1) { continue; }else{ $link_page.=" {$page} ";
}
}
$link_page.=" {$this->page} ";
for ($i = 1; $i < $i_num; $i++) { $page=$this->page+$i;
if ($page<=$this->total_pages) {
$link_page.=" {$page} ";
}else{
break;
}
}
return $link_page;
}
public function out_page($display=array(0,1,2,3,4,5,6,7,8)) {
$display_html='';
$html[0]=" 共有{$this->total_rows} {$this->config['header']} ";
$html[1]=" 每页显示".($this->end_page()-$this->start_page()+1)." 条,本页显示从{$this->start_page()} --{$this->end_page()} {$this->config['header']} ";
$html[2]=" {$this->page} /{$this->total_pages} 页 ";
$html[3]=$this->go_first();
$html[4]=$this->go_prev();
$html[5]=$this->page_list();
$html[6]=$this->go_next();
$html[7]=$this->go_last();
$html[8]=$this->go_page();
foreach ($display as $index){
$display_html.=$html[$index];
}
return $display_html;
}
}
?>
复制代码
page_demo.php
header("content-type:text/html;charset=utf-8"); require_once './page.class.php'; require_once '../config/config.db.php'; //数据库中的总条数:total_rows; //每一页显示的条数:per_page_rows $sql="select * from cp_sd_day"; $rt=mysql_query($sql); $total_rows=mysql_num_rows($rt); $per_page_rows=10; $page=new Page($total_rows,$per_page_rows); $sql="select * from cp_sd_day {$page->limit}";
$rt=mysql_query($sql);
echo '';
echo 'cp_sd_day ';
while (!!$row=mysql_fetch_assoc($rt)) {
echo '';
echo ''.$row['date_no'].' ';
echo ''.$row['max_notwin'].' ';
echo ''.$row['sum_last_miss'].' ';
echo ''.$row['last_miss'].' ';
echo ''.$row['last_miss_sort'].' ';
echo ' ';
}
echo ''.$page->out_page(array(2,3,4,5,6,7,8)).' ';
echo '
';
?>
复制代码