提升代码的可读性系列--基础篇(增强代码可读性是函数的作用吗)
787
2022-07-31
class page{
private $total; //数据表中总记录数
private $listrows; //每页显示行数
private $limit;
private $uri; //自动获取url请求地址
private $pageNum; //总页数
private $config = array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'首页','last'=>'尾页');
public function __construct($total,$listrows=10,$pa=""){
$this->total = $total;
$this->listrows = $listrows;
$this->uri = $this->getUri($pa);
$this->page = !empty($_GET['page']) ? $_GET['page']:1 ;
$this->pageNum = ceil($this->total/$this->listrows);
$this->limit = $this->setLimit();
}
private function setLimit(){
return "limit ".($this->page-1)*$this->listrows.','.$this->listrows;
}
public function getUri($pa){
$url = $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$pa;
$parse = parse_url($url); //返回一个数组,数组有元素query和path
if(isset($parse['query'])){
parse_str($parse['query'],$params); //将query中的参数解析到$params,为数组
unset($params['page']);
$url = $parse['path'].'?'.http_build_query($params); //按照指定的参数生成一个请求字符串
}
return $url;
}
//limit为私有属性,通过这个魔术方法使limit变得可用
private function __get($args){
if($args=="limit"){
return $this->limit;
}else{
return null;
}
}
private function first(){
if($this->page==1){
$html.='';
}else{
$html.=" {$this->config['first']} ";
}
return $html;
}
private function prev(){
if($this->page==1){
$html.='';
}else{
$html.=" {$this->config['prev']} ";
}
return $html;
}
private function pagelist(){
$linkPage='';
//每边显示inum个页码
$inum = 3;
//左边的页码
for($i=$inum;$i>=1;$i--){
$page = $this->page-$i;
if($page<1){
continue;
}else{
$linkPage.=" {$page} ";
}
}
//当前页码
$linkPage.=" {$this->page} ";
//右边页码
for($i=1;$i<=$inum;$i++){
$page = $this->page+$i;
if($page>$this->pageNum){
break;
}else{
$linkPage.=" {$page} ";
}
}
return $linkPage;
}
private function next(){
if($this->page==$this->pageNum){
$html.='';
}else{
$html.=" {$this->config['next']} ";
}
return $html;
}
private function last(){
if($this->page==$this->pageNum){
$html.='';
}else{
$html.=" {$this->config['last']} ";
}
return $html;
}
private function goPage(){
return '
value="'.$this->page.'" style="width:25px" >
';
}
//*可见的成员方法
function fpage($arr=array(0,1,2,3,4,5,6,7)){
$html[0]=" 共有{$this->total}{$this->config['header']} ";
$html[1]=" {$this->page}/{$this->pageNum}页 ";
$html[2]=$this->first();
$html[3]=$this->prev();
$html[4]=$this->pageList();
$html[5]=$this->next();
$html[6]=$this->last();
$html[7]=$this->goPage();
$fpage='';
foreach($arr as $index){
$fpage.=$html[$index];
}
return $fpage;
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~