<?PHPDefine(' ACC ') | |Exit(' Permision Denied '); //total number of bars $total//page $perpage//Total pages $cnt = ceil ($total/$perpage); Division, rounding up//page page display (page-1) * $perpage + 1 ~ ~ ~ page* $perpage//c.php//c.php?cat_id = 3//c.php?cat_id =3 $page = 1//pag E parameters, should be based on the page number to generate, but also can not throw other parameters//first get the Address bar and save classpagetool{protected $total= 0; protected $perpage= 10; protected $page= 1; Public function__construct ($total,$page=false,$perpage=false){ $this->total =$total; if($perpage){ $this->perpage =$perpage; } if($page){ $this->page=$page; } } //main Functions Public functionShow () {$cnt=Ceil($this->total/$this->perpage);//Get Total Pages $url= ($_server[' Request_uri ']); $parse=Parse_url($url);//Separate path query two array type//print_r ($parse); Talk param split the group $param=Array(); if(isset($parse[' Query '])){ Parse_str($parse[' Query '],$param); } //Print_r ($param); Regardless of whether the $param array has a page element, it is unset to save the page element except the page unset($param[' Page ']); //Print_r ($param); Next key, calculate page navigation $url=$parse[' Path ']. '? '; if(!Empty($param)){ $param=Http_build_query($param); $url=$url.$param.‘ & '; Echo $url, "<br/>"; } //Calculate page Navigation $nav=Array(); $nav[] = ' <span class= ' page_now ' > '.$this->page. ' </span> '; for($left=$this->page-1,$right=$this->page+1;($left>=1 | |$right<=$cnt) &&Count($nav) <=5 ;){ if($left>= 1){ Array_unshift($nav, ' <a href= '.$url.‘ Page= '.$left.‘" >['.$left.‘] </a> ');//put to the beginning of the array $left--; } if($right<=$cnt){ Array_push($nav, ' <a href= '.$url.‘ Page= '.$right.‘" >['.$right.‘] </a> ');//put it at the end of the array $right++; } } //Print_r ($NAV); View Source Code return implode(‘‘,$nav); } }//$p = NEW pagetool (50,2,6); Total 50 items, currently 2nd page, each page 6 item//print_r ($p->show ()); Paged class call Test//new Pagetool (total number of bars, current page, number of bars per page);//echo $p->show (); return pagination Code $page=isset($_get[' Page ']) ?$_get[' Page ']: 1;$p=NEWPagetool (50,$page, 6);//Total 50 items, currently 2nd page, 6 item per pageEcho $p-Show (); ?>
Click on page 6th and 7th, respectively, and the test results are as follows:
PHP Paging Pager Page class implementation