知识屋:更实用的电脑技术知识网站
所在位置:首页 > 编程技术 > PHP编程

php实战第八天

发布时间:2015-05-27 19:27:10作者:知识屋

今天是学习做后台页面布局,用得是bootcss。那么效果展示一下。

 

 

/

 

 

布局的代码

[html] %20<!DOCTYPE%20html> <html%20lang="en"> <head>  %20<meta%20http-equiv="Content-Type"%20content="text/html;%20charset=utf-8"%20/>  %20<title>无标题文档</title>  %20<link%20rel="stylesheet"%20type="text/css"%20href="css/bootstrap.min.css"></head>  <body>   %20<div%20class="container-fluid">    %20<div%20class="row-fluid">      %20<div%20class="span4">        %20<h3>瀑布流留言板管理系统</h3>      %20</div>    %20</div>     %20<div%20class="row-fluid">      %20<div%20id="menu"%20class="span2">        %20<ul%20class="nav%20nav-list">          %20<li%20class="nav-header">留言管理</li>          %20<li%20class="active">            %20<a%20id="start"%20href="javascript:void(0);">所有留言</a>          %20</li>          %20<li%20class="nav-header">网站管理</li>          %20<li%20class="">            %20<a%20href="javascript:void(0);">基本设置</a>          %20</li>         %20</ul>      %20</div>       %20<div%20id="main"%20class="span10">        %20<div%20id="mainData"></div>      %20</div>    %20</div>   %20</div>  </body>  %20<script%20src="js/jquery-2.0.1.min.js"></script>   %20<script%20type="text/javascript"%20src="js/bootstrap.min.js"></script>  %20<script%20type="text/javascript"%20src="js/admin.js"></script>  </html> 

<!DOCTYPE%20html><html%20lang="en"><head> %20<meta%20http-equiv="Content-Type"%20content="text/html;%20charset=utf-8"%20/> %20<title>无标题文档</title> %20<link%20rel="stylesheet"%20type="text/css"%20href="css/bootstrap.min.css"></head>

<body>

 %20<div%20class="container-fluid">   %20<div%20class="row-fluid">     %20<div%20class="span4">       %20<h3>瀑布流留言板管理系统</h3>     %20</div>   %20</div>

   %20<div%20class="row-fluid">     %20<div%20id="menu"%20class="span2">       %20<ul%20class="nav%20nav-list">         %20<li%20class="nav-header">留言管理</li>         %20<li%20class="active">           %20<a%20id="start"%20href="javascript:void(0);">所有留言</a>         %20</li>         %20<li%20class="nav-header">网站管理</li>         %20<li%20class="">           %20<a%20href="javascript:void(0);">基本设置</a>         %20</li>

       %20</ul>     %20</div>

     %20<div%20id="main"%20class="span10">       %20<div%20id="mainData"></div>     %20</div>   %20</div>

 %20</div>

</body> %20<script%20src="js/jquery-2.0.1.min.js"></script>

 %20<script%20type="text/javascript"%20src="js/bootstrap.min.js"></script> %20<script%20type="text/javascript"%20src="js/admin.js"></script>

</html>

 

 

从中学到javascript函数

str.substr(0,20)意思是从0个字符开始取20个字符

Math.random()取随机数

switch语句

 

 

这后台是ajax的噢。写了好久的分页呢。好吧,直接上代码。

[javascript] %20//%20JavaScript%20Document  $('body').off('.data-api');  $(document).ready(function(e)%20{     %20$("#menu%20a").click(function()%20{         %20switch%20($(this).text())%20{            %20case%20'所有留言':                %20admin_content(1);                %20break;             %20case%20'基本设置':                %20$("#main%20#mainData").load('admin_config.html?r='+Math.random());                %20break;             %20default:                %20break;        %20}       %20});     %20$("#start").click(); });   /***留言管理*/ function%20admin_content(page)%20{    %20$.ajax({        %20url:%20'http://localhost/l/admin.php?m=admin&a=content&page='%20+%20page%20+%20'&rand='%20+%20Math.random(),        %20type:%20'get',        %20dataType:%20'json',        %20data:%20{},        %20complete:%20function(xhr,%20textStatus)%20{            %20//called%20when%20complete         %20},        %20success:%20function(json,%20textStatus,%20xhr)%20{            %20if%20(json['state']%20==%20'ok')%20{                %20var%20page_start%20=%20json['start'];//分页开始                 %20var%20page_end%20=%20json['end'];//分页结束                 %20var%20page_page%20=%20json['page'];//分页当前页面                 %20var%20data%20=%20json['data'];//分页数据  /***%20生成%20表格内容*/                %20var%20table_html%20=%20'<table%20class="table%20table-hover"><tr><th>操作</th><th>用户名</th><th>留言内容</th><th>发表时间</th></tr>';                %20for%20(i%20=%200;%20i%20<%20data.length;%20i++)%20{                    %20var%20trClass%20=%20(i%20%%202%20==%200)%20?%20'class="info"'%20:%20'';                     %20var%20tr_html%20=%20'<tr%20'%20+%20trClass%20+%20'><td%20width=100><a%20href="javascript:admin_content_del('+data[i].id+');">删除</a></td><td%20width=100>'%20+%20data[i].userName%20+%20'</td><td%20width=400%20><div%20style="max-width:400px;max-height:150px;overflow-y:auto;">'%20+%20data[i].content%20+%20'</div></td><td%20>'%20+%20getLocalTime(data[i].time)%20+%20'</td></tr>';                    %20table_html%20=%20table_html%20+%20tr_html;                %20}                %20table_html%20=%20table_html%20+%20'</table>';  /***生成分页*/                %20var%20page_html%20=%20'<div%20id="mainPage"><div%20class="pagination"><ul>';                %20if%20(page_end%20!==%200)%20{                    %20if%20(page_page%20==%201)%20{                        %20page_html%20=%20page_html%20+%20'<li%20class="disabled"><a%20href="JavaScript:void(0);">«</a></li>';                    %20}%20else%20{                        %20page_html%20=%20page_html%20+%20'<li><a%20href="JavaScript:void(0);">«</a></li>';                    %20}                %20}                %20for%20(var%20i%20=%20page_start;%20i%20<=%20page_end;%20i++)%20{                    %20if%20(page_page%20==%20i)%20{                        %20page_html%20=%20page_html%20+%20'<li%20class="active"><a%20href="JavaScript:void(0);">'%20+%20i%20+%20'</a></li>';                    %20}%20else%20{                        %20page_html%20=%20page_html%20+%20'<li><a%20href="JavaScript:void(0);">'%20+%20i%20+%20'</a></li>';                    %20}                 %20}                %20if%20(page_end%20!==%200)%20{                    %20if%20(page_page%20==%20page_end)%20{                        %20page_html%20=%20page_html%20+%20'<li%20class="disabled"><a%20href="JavaScript:void(0);">»</a></li>';                    %20}%20else%20{                        %20page_html%20=%20page_html%20+%20'<li><a%20href="JavaScript:void(0);">»</a></li>';                    %20}                 %20}                %20page_html%20=%20page_html%20+%20'</ul></div></div>';                  %20var%20mainData%20=%20$("#main%20#mainData");                %20mainData.html(table_html);                %20mainData.append(page_html);                 %20admin_content_page(page_page,page_end);%20//挂接分页点击事件             %20}               %20//alert(json.data);         %20},        %20error:%20function(xhr,%20textStatus,%20errorThrown)%20{            %20//called%20when%20there%20is%20an%20error         %20}    %20});  /***%20挂机分页事件*%20参数%20page_page%20当前分页*%20参数%20page_end %20分页数量*/ function%20admin_content_page(page_page,page_end)%20{    %20$("#mainPage%20a").click(function()%20{        %20var%20charStr%20=%20$(this).text();        %20var%20num%20=%20charStr;        %20if%20(charStr%20==%20"»")%20{            %20num%20=%20parseInt(page_page)%20+%201;            %20if%20(page_end%20<%20num)%20{                %20return;            %20}         %20}%20else%20if%20(charStr%20==%20"«")%20{            %20num%20=%20parseInt(page_page)%20-%201;            %20if%20(num%20<=%200)%20{                %20return;            %20}        %20}         %20admin_content(num);     %20});  function%20admin_content_del%20(id)%20{    %20alert(id);  function%20getLocalTime(nS)%20{    %20return%20new%20Date(parseInt(nS)%20*%201000).toLocaleString().replace(/:/d{1,2}$/,%20'%20'); 

//%20JavaScript%20Document$('body').off('.data-api');

$(document).ready(function(e)%20{

 $("#menu%20a").click(function()%20{

  switch%20($(this).text())%20{   case%20'所有留言':    admin_content(1);    break;

   case%20'基本设置':    $("#main%20#mainData").load('admin_config.html?r='+Math.random());    break;

   default:    break;  }

 

 });

 $("#start").click();});

/***留言管理*/function%20admin_content(page)%20{ $.ajax({  url:%20'http://localhost/l/admin.php?m=admin&a=content&page='%20+%20page%20+%20'&rand='%20+%20Math.random(),  type:%20'get',  dataType:%20'json',  data:%20{},  complete:%20function(xhr,%20textStatus)%20{   //called%20when%20complete  },  success:%20function(json,%20textStatus,%20xhr)%20{   if%20(json['state']%20==%20'ok')%20{    var%20page_start%20=%20json['start'];//分页开始    var%20page_end%20=%20json['end'];//分页结束    var%20page_page%20=%20json['page'];//分页当前页面    var%20data%20=%20json['data'];//分页数据/***%20生成%20表格内容*/    var%20table_html%20=%20'<table%20class="table%20table-hover"><tr><th>操作</th><th>用户名</th><th>留言内容</th><th>发表时间</th></tr>';    for%20(i%20=%200;%20i%20<%20data.length;%20i++)%20{     var%20trClass%20=%20(i%20%%202%20==%200)%20?%20'class="info"'%20:%20'';

     var%20tr_html%20=%20'<tr%20'%20+%20trClass%20+%20'><td%20width=100><a%20href="javascript:admin_content_del('+data[i].id+');">删除</a></td><td%20width=100>'%20+%20data[i].userName%20+%20'</td><td%20width=400%20><div%20style="max-width:400px;max-height:150px;overflow-y:auto;">'%20+%20data[i].content%20+%20'</div></td><td%20>'%20+%20getLocalTime(data[i].time)%20+%20'</td></tr>';     table_html%20=%20table_html%20+%20tr_html;    }    table_html%20=%20table_html%20+%20'</table>';

/***生成分页*/    var%20page_html%20=%20'<div%20id="mainPage"><div%20class="pagination"><ul>';    if%20(page_end%20!==%200)%20{     if%20(page_page%20==%201)%20{      page_html%20=%20page_html%20+%20'<li%20class="disabled"><a%20href="JavaScript:void(0);">«</a></li>';     }%20else%20{      page_html%20=%20page_html%20+%20'<li><a%20href="JavaScript:void(0);">«</a></li>';     }    }    for%20(var%20i%20=%20page_start;%20i%20<=%20page_end;%20i++)%20{     if%20(page_page%20==%20i)%20{      page_html%20=%20page_html%20+%20'<li%20class="active"><a%20href="JavaScript:void(0);">'%20+%20i%20+%20'</a></li>';     }%20else%20{      page_html%20=%20page_html%20+%20'<li><a%20href="JavaScript:void(0);">'%20+%20i%20+%20'</a></li>';     }

    }    if%20(page_end%20!==%200)%20{     if%20(page_page%20==%20page_end)%20{      page_html%20=%20page_html%20+%20'<li%20class="disabled"><a%20href="JavaScript:void(0);">»</a></li>';     }%20else%20{      page_html%20=%20page_html%20+%20'<li><a%20href="JavaScript:void(0);">»</a></li>';     }

    }    page_html%20=%20page_html%20+%20'</ul></div></div>';

    var%20mainData%20=%20$("#main%20#mainData");    mainData.html(table_html);    mainData.append(page_html);

    admin_content_page(page_page,page_end);%20//挂接分页点击事件   }

 

   //alert(json.data);  },  error:%20function(xhr,%20textStatus,%20errorThrown)%20{   //called%20when%20there%20is%20an%20error  } });}

/***%20挂机分页事件*%20参数%20page_page%20当前分页*%20参数%20page_end %20分页数量*/function%20admin_content_page(page_page,page_end)%20{ $("#mainPage%20a").click(function()%20{  var%20charStr%20=%20$(this).text();  var%20num%20=%20charStr;  if%20(charStr%20==%20"»")%20{   num%20=%20parseInt(page_page)%20+%201;   if%20(page_end%20<%20num)%20{    return;   }

  }%20else%20if%20(charStr%20==%20"«")%20{   num%20=%20parseInt(page_page)%20-%201;   if%20(num%20<=%200)%20{    return;   }  }

  admin_content(num);

 });}

function%20admin_content_del%20(id)%20{ alert(id);}

function%20getLocalTime(nS)%20{ return%20new%20Date(parseInt(nS)%20*%201000).toLocaleString().replace(/:/d{1,2}$/,%20'%20');}这是分页请求的服务端代码

[php] %20public%20function%20content()    %20//引入分页类     %20include%20"page.class.php";        %20//得到data表的数据数量     %20$rows%20=%20$this->db->count('select%20*%20from%20data');    %20//创建分页对象     %20$page%20=%20new%20Page($rows,%205,%20"");    %20$list=$this->db           %20->order('id%20DESC')           %20->table('data')           %20->limit($page->getLimit())           %20->select();    %20/*   %20echo%20"<pre>";   %20var_dump($list);   %20echo%20"</pre>";   %20*/    %20$json['state']='ok';    %20$json['start']=$page->getStart();    %20$json['end']=$page->getEnd();    %20$json['page']=$page->getPage();    %20$json['data']=$list;    %20echo%20json_encode($json);    %20//exit();  

  public%20function%20content()  {   //引入分页类   include%20"page.class.php";    //得到data表的数据数量   $rows%20=%20$this->db->count('select%20*%20from%20data');   //创建分页对象   $page%20=%20new%20Page($rows,%205,%20"");   $list=$this->db      %20->order('id%20DESC')      %20->table('data')   %20   %20->limit($page->getLimit())   %20   %20->select();   /*   echo%20"<pre>";   var_dump($list);   echo%20"</pre>";   */   $json['state']='ok';   $json['start']=$page->getStart();   $json['end']=$page->getEnd();   $json['page']=$page->getPage();   $json['data']=$list;   echo%20json_encode($json);   //exit();  }做这分页的时候由于是ajax,html代码生成都在客户端。经过我观察这分页类存在问题。实际上许多代码都没必要存在。有时间我去把他干掉。

附上代码%20page.class.php

 

 

[php] %20<?php    %20/**       %20file:%20page.class.php        %20完美分页类%20Page        %20@微凉%20QQ496928838   %20*/    %20class%20Page%20{        %20private%20$total;                        %20//数据表中总记录数         %20private%20$listRows;                     %20//每页显示行数         %20private%20$limit;                        %20//SQL语句使用limit从句,限制获取记录个数         %20private%20$uri;                          %20//自动获取url的请求地址         %20private%20$pageNum;                      %20//总页数         %20private%20$page;                         %20//当前页            %20private%20$config%20=%20array(                            %20'head'%20=>%20"条记录",                             %20'prev'%20=>%20"上一页",                             %20'next'%20=>%20"下一页",                             %20'first'=>%20"首页",                             %20'last'%20=>%20"末页"                        %20);                     %20//在分页信息中显示内容,可以自己通过set()方法设置         %20private%20$listNum%20=%2010;                 %20//默认分页列表显示的个数          %20/**           %20构造方法,可以设置分页类的属性           %20@param %20int%20$total     %20计算分页的总记录数           %20@param %20int%20$listRows  %20可选的,设置每页需要显示的记录数,默认为25条           %20@param %20mixed  %20$query %20可选的,为向目标页面传递参数,可以是数组,也可以是查询字符串格式           %20@param %20bool   %20$ord   %20可选的,默认值为true,%20页面从第一页开始显示,false则为最后一页        %20*/        %20public%20function%20__construct($total,%20$listRows=25,%20$query="",%20$ord=true){            %20$this->total%20=%20$total;            %20$this->listRows%20=%20$listRows;            %20$this->uri%20=%20$this->getUri($query);            %20$this->pageNum%20=%20ceil($this->total%20/%20$this->listRows);            %20/*以下判断用来设置当前面*/            %20if(!empty($_GET["page"]))%20{                %20$page%20=%20$_GET["page"];            %20}else{                %20if($ord)                    %20$page%20=%201;                %20else                    %20$page%20=%20$this->pageNum;            %20}             %20if($total%20>%200)%20{                %20if(preg_match('//D/',%20$page)%20){                    %20$this->page%20=%201;                %20}else{                    %20$this->page%20=%20$page;                %20}            %20}else{                %20$this->page%20=%200;            %20}                         %20$this->limit%20=%20"LIMIT%20".$this->getLimit();        %20}         %20/**           %20用于设置显示分页的信息,可以进行连贯操作           %20@param %20string %20$param %20是成员属性数组config的下标           %20@param %20string %20$value %20用于设置config下标对应的元素值           %20@return%20object         %20返回本对象自己$this,%20用于连惯操作        %20*/        %20function%20set($param,%20$value){            %20if(array_key_exists($param,%20$this->config)){                %20$this->config[$param]%20=%20$value;            %20}            %20return%20$this;        %20}                 %20/*%20不是直接去调用,通过该方法,可以使用在对象外部直接获取私有成员属性limit和page的值%20*/        %20function%20__get($args){            %20if($args%20==%20"limit"%20||%20$args%20==%20"page")                %20return%20$this->$args;            %20else                %20return%20null;        %20}                 %20/**           %20按指定的格式输出分页           %20@param %20int%200-7的数字分别作为参数,用于自定义输出分页结构和调整结构的顺序,默认输出全部结构           %20@return%20string %20分页信息内容        %20*/        %20function%20fpage(){            %20$arr%20=%20func_get_args();             %20$html[0]%20=%20"%20共<b>%20{$this->total}%20</b>{$this->config["head"]}%20";            %20$html[1]%20=%20"%20本页%20<b>".$this->disnum()."</b>%20条%20";            %20$html[2]%20=%20"%20本页从%20<b>{$this->start()}-{$this->end()}</b>%20条%20";            %20$html[3]%20=%20"%20<b>{$this->page}/{$this->pageNum}</b>页%20";            %20$html[4]%20=%20$this->firstprev();            %20$html[5]%20=%20$this->pageList();            %20$html[6]%20=%20$this->nextlast();            %20$html[7]%20=%20$this->goPage();             %20$fpage%20=%20'<div%20style="font:12px%20/'/5B8B/4F53/',san-serif;">';            %20if(count($arr)%20<%201)                %20$arr%20=%20array(0,%201,2,3,4,5,6,7);                             %20for($i%20=%200;%20$i%20<%20count($arr);%20$i++)                %20$fpage%20.=%20$html[$arr[$i]];                     %20$fpage%20.=%20'</div>';            %20return%20$fpage;        %20}                 %20/*%20格式为%201,5,*/        %20function%20getLimit(){            %20if($this->page%20>%200)                %20return%20($this->page-1)*$this->listRows.",{$this->listRows}";            %20else                %20return%200;        %20}         %20//当前的页面         %20function%20getPage(){            %20return%20$this->page;        %20}        %20//分页的开始         %20function%20getStart(){            %20$num1=$this->page-4;             %20return%20$num1>0?$num1:1;         %20}        %20//分页的结束         %20function%20getEnd(){            %20$num1=$this->pageNum;            %20$num2=$this->page+5;             %20return%20$num1<$num2?$num1:$num2;        %20}         %20/*%20在对象内部使用的私有方法,用于自动获取访问的当前URL%20*/        %20private%20function%20getUri($query){                %20$request_uri%20=%20$_SERVER["REQUEST_URI"];             %20$url%20=%20strstr($request_uri,'?')%20?%20$request_uri%20: %20$request_uri.'?';                         %20if(is_array($query))                %20$url%20.=%20http_build_query($query);            %20else%20if($query%20!=%20"")                %20$url%20.=%20"&".trim($query,%20"?&");                     %20$arr%20=%20parse_url($url);             %20if(isset($arr["query"])){                %20parse_str($arr["query"],%20$arrs);                %20unset($arrs["page"]);                %20$url%20=%20$arr["path"].'?'.http_build_query($arrs);            %20}                         %20if(strstr($url,%20'?'))%20{                %20if(substr($url,%20-1)!='?')                    %20$url%20=%20$url.'&';            %20}else{                %20$url%20=%20$url.'?';            %20}                         %20return%20$url;        %20}         %20/*%20在对象内部使用的私有方法,用于获取当前页开始的记录数%20*/        %20private%20function%20start(){            %20if($this->total%20==%200)                %20return%200;            %20else                %20return%20($this->page-1)%20*%20$this->listRows+1;        %20}         %20/*%20在对象内部使用的私有方法,用于获取当前页结束的记录数%20*/        %20private%20function%20end(){            %20return%20min($this->page%20*%20$this->listRows,%20$this->total);        %20}         %20/*%20在对象内部使用的私有方法,用于获取上一页和首页的操作信息%20*/        %20private%20function%20firstprev(){            %20if($this->page%20>%201)%20{                %20$str%20=%20"%20<a%20href='{$this->uri}page=1'>{$this->config["first"]}</a>%20";                %20$str%20.=%20"<a%20href='{$this->uri}page=".($this->page-1)."'>{$this->config["prev"]}</a>%20";                        %20return%20$str;            %20}         %20}             %20/*%20在对象内部使用的私有方法,用于获取页数列表信息%20*/        %20private%20function%20pageList(){            %20$linkPage%20=%20"%20<b>";                         %20$inum%20=%20floor($this->listNum/2);            %20/*当前页前面的列表%20*/            %20for($i%20=%20$inum;%20$i%20>=%201;%20$i--){                %20$page%20=%20$this->page-$i;                 %20if($page%20>=%201)                    %20$linkPage%20.=%20"<a%20href='{$this->uri}page={$page}'>{$page}</a>%20";            %20}            %20/*当前页的信息%20*/            %20if($this->pageNum%20>%201)                %20$linkPage%20.=%20"<span%20style='padding:1px%202px;background:#BBB;color:white'>{$this->page}</span>%20";                         %20/*当前页后面的列表%20*/            %20for($i=1;%20$i%20<=%20$inum;%20$i++){                %20$page%20=%20$this->page+$i;                %20if($page%20<=%20$this->pageNum)                    %20$linkPage%20.=%20"<a%20href='{$this->uri}page={$page}'>{$page}</a>%20";                %20else                    %20break;            %20}            %20$linkPage%20.=%20'</b>';            %20return%20$linkPage;        %20}         %20/*%20在对象内部使用的私有方法,获取下一页和尾页的操作信息%20*/        %20private%20function%20nextlast(){            %20if($this->page%20!=%20$this->pageNum)%20{                %20$str%20=%20"%20<a%20href='{$this->uri}page=".($this->page+1)."'>{$this->config["next"]}</a>%20";                %20$str%20.=%20"%20<a%20href='{$this->uri}page=".($this->pageNum)."'>{$this->config["last"]}</a>%20";                %20return%20$str;            %20}        %20}         %20/*%20在对象内部使用的私有方法,用于显示和处理表单跳转页面%20*/        %20private%20function%20goPage(){                %20if($this->pageNum%20>%201)%20{                %20return%20'%20<input%20style="width:20px;height:17px%20!important;height:18px;border:1px%20solid%20#CCCCCC;"%20type="text"%20onkeydown="javascript:if(event.keyCode==13){var%20page=(this.value>'.$this->pageNum.')?'.$this->pageNum.':this.value;location=/''.$this->uri.'page=/'+page+/'/'}"%20value="'.$this->page.'"><input%20style="cursor:pointer;width:25px;height:18px;border:1px%20solid%20#CCCCCC;"%20type="button"%20value="GO"%20onclick="javascript:var%20page=(this.previousSibling.value>'.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;location=/''.$this->uri.'page=/'+page+/'/'">%20';            %20}        %20}         %20/*%20在对象内部使用的私有方法,用于获取本页显示的记录条数%20*/        %20private%20function%20disnum(){            %20if($this->total%20>%200){                %20return%20$this->end()-$this->start()+1;            %20}else{                %20return%200;            %20}        %20}    %20}                 

<?php /**  file:%20page.class.php  完美分页类%20Page  @微凉%20QQ496928838 */ class%20Page%20{  private%20$total;  %20  %20    //数据表中总记录数  private%20$listRows;%20      //每页显示行数  private%20$limit;  %20  %20    //SQL语句使用limit从句,限制获取记录个数  private%20$uri;    %20  %20    //自动获取url的请求地址  private%20$pageNum;%20  %20    //总页数  private%20$page;       //当前页   private%20$config%20=%20array(       'head'%20=>%20"条记录",       'prev'%20=>%20"上一页",       'next'%20=>%20"下一页",       'first'=>%20"首页",       'last'%20=>%20"末页"      );%20      //在分页信息中显示内容,可以自己通过set()方法设置  private%20$listNum%20=%2010;%20     //默认分页列表显示的个数

  /**   构造方法,可以设置分页类的属性   @param int $total  计算分页的总记录数   @param int $listRows 可选的,设置每页需要显示的记录数,默认为25条   @param mixed $query 可选的,为向目标页面传递参数,可以是数组,也可以是查询字符串格式   @param%20 bool $ord 可选的,默认值为true,%20页面从第一页开始显示,false则为最后一页  %20*/  public%20function%20__construct($total,%20$listRows=25,%20$query="",%20$ord=true){   $this->total%20=%20$total;   $this->listRows%20=%20$listRows;   $this->uri%20=%20$this->getUri($query);   $this->pageNum%20=%20ceil($this->total%20/%20$this->listRows);   /*以下判断用来设置当前面*/   if(!empty($_GET["page"]))%20{    $page%20=%20$_GET["page"];   }else{    if($ord)     $page%20=%201;    else     $page%20=%20$this->pageNum;   }

   if($total%20>%200)%20{    if(preg_match('//D/',%20$page)%20){     $this->page%20=%201;    }else{     $this->page%20=%20$page;    }   }else{    $this->page%20=%200;   }      $this->limit%20=%20"LIMIT%20".$this->getLimit();  }

  /**   用于设置显示分页的信息,可以进行连贯操作   @param string $param 是成员属性数组config的下标   @param string $value 用于设置config下标对应的元素值   @return object   返回本对象自己$this,%20用于连惯操作  %20*/  function%20set($param,%20$value){   if(array_key_exists($param,%20$this->config)){    $this->config[$param]%20=%20$value;   }   return%20$this;  }    /*%20不是直接去调用,通过该方法,可以使用在对象外部直接获取私有成员属性limit和page的值%20*/  function%20__get($args){   if($args%20==%20"limit"%20||%20$args%20==%20"page")    return%20$this->$args;   else    return%20null;  }    /**   按指定的格式输出分页   @param int 0-7的数字分别作为参数,用于自定义输出分页结构和调整结构的顺序,默认输出全部结构   @return string 分页信息内容  %20*/  function%20fpage(){   $arr%20=%20func_get_args();

   $html[0]%20=%20"%20共<b>%20{$this->total}%20</b>{$this->config["head"]}%20";   $html[1]%20=%20"%20本页%20<b>".$this->disnum()."</b>%20条%20";   $html[2]%20=%20"%20本页从%20<b>{$this->start()}-{$this->end()}</b>%20条%20";   $html[3]%20=%20"%20<b>{$this->page}/{$this->pageNum}</b>页%20";   $html[4]%20=%20$this->firstprev();   $html[5]%20=%20$this->pageList();   $html[6]%20=%20$this->nextlast();   $html[7]%20=%20$this->goPage();

   $fpage%20=%20'<div%20style="font:12px%20/'/5B8B/4F53/',san-serif;">';   if(count($arr)%20<%201)    $arr%20=%20array(0,%201,2,3,4,5,6,7);       for($i%20=%200;%20$i%20<%20count($arr);%20$i++)    $fpage%20.=%20$html[$arr[$i]];     $fpage%20.=%20'</div>';   return%20$fpage;  }    /*%20格式为%201,5,*/  function%20getLimit(){   if($this->page%20>%200)    return%20($this->page-1)*$this->listRows.",{$this->listRows}";   else    return%200;  }

  //当前的页面  function%20getPage(){   return%20$this->page;  }  //分页的开始  function%20getStart(){   $num1=$this->page-4;

   return%20$num1>0?$num1:1;

  }  //分页的结束  function%20getEnd(){   $num1=$this->pageNum;   $num2=$this->page+5;

   return%20$num1<$num2?$num1:$num2;  }

  /*%20在对象内部使用的私有方法,用于自动获取访问的当前URL%20*/  private%20function%20getUri($query){    $request_uri%20=%20$_SERVER["REQUEST_URI"];    $url%20=%20strstr($request_uri,'?')%20?%20$request_uri%20: %20$request_uri.'?';      if(is_array($query))    $url%20.=%20http_build_query($query);   else%20if($query%20!=%20"")    $url%20.=%20"&".trim($query,%20"?&");     $arr%20=%20parse_url($url);

   if(isset($arr["query"])){    parse_str($arr["query"],%20$arrs);    unset($arrs["page"]);    $url%20=%20$arr["path"].'?'.http_build_query($arrs);   }      if(strstr($url,%20'?'))%20{    if(substr($url,%20-1)!='?')     $url%20=%20$url.'&';   }else{    $url%20=%20$url.'?';   }      return%20$url;  }

  /*%20在对象内部使用的私有方法,用于获取当前页开始的记录数%20*/  private%20function%20start(){   if($this->total%20==%200)    return%200;   else    return%20($this->page-1)%20*%20$this->listRows+1;  }

  /*%20在对象内部使用的私有方法,用于获取当前页结束的记录数%20*/  private%20function%20end(){   return%20min($this->page%20*%20$this->listRows,%20$this->total);  }

  /*%20在对象内部使用的私有方法,用于获取上一页和首页的操作信息%20*/  private%20function%20firstprev(){   if($this->page%20>%201)%20{    $str%20=%20"%20<a%20href='{$this->uri}page=1'>{$this->config["first"]}</a>%20";    $str%20.=%20"<a%20href='{$this->uri}page=".($this->page-1)."'>{$this->config["prev"]}</a>%20";      return%20$str;   }

  }   /*%20在对象内部使用的私有方法,用于获取页数列表信息%20*/  private%20function%20pageList(){   $linkPage%20=%20"%20<b>";      $inum%20=%20floor($this->listNum/2);   /*当前页前面的列表%20*/   for($i%20=%20$inum;%20$i%20>=%201;%20$i--){    $page%20=%20$this->page-$i;

    if($page%20>=%201)     $linkPage%20.=%20"<a%20href='{$this->uri}page={$page}'>{$page}</a>%20";   }   /*当前页的信息%20*/   if($this->pageNum%20>%201)    $linkPage%20.=%20"<span%20style='padding:1px%202px;background:#BBB;color:white'>{$this->page}</span>%20";      /*当前页后面的列表%20*/   for($i=1;%20$i%20<=%20$inum;%20$i++){    $page%20=%20$this->page+$i;    if($page%20<=%20$this->pageNum)     $linkPage%20.=%20"<a%20href='{$this->uri}page={$page}'>{$page}</a>%20";    else     break;   }   $linkPage%20.=%20'</b>';   return%20$linkPage;  }

  /*%20在对象内部使用的私有方法,获取下一页和尾页的操作信息%20*/  private%20function%20nextlast(){   if($this->page%20!=%20$this->pageNum)%20{    $str%20=%20"%20<a%20href='{$this->uri}page=".($this->page+1)."'>{$this->config["next"]}</a>%20";    $str%20.=%20"%20<a%20href='{$this->uri}page=".($this->pageNum)."'>{$this->config["last"]}</a>%20";    return%20$str;   }  }

  /*%20在对象内部使用的私有方法,用于显示和处理表单跳转页面%20*/  private%20function%20goPage(){   %20   if($this->pageNum%20>%201)%20{    return%20'%20<input%20style="width:20px;height:17px%20!important;height:18px;border:1px%20solid%20#CCCCCC;"%20type="text"%20onkeydown="javascript:if(event.keyCode==13){var%20page=(this.value>'.$this->pageNum.')?'.$this->pageNum.':this.value;location=/''.$this->uri.'page=/'+page+/'/'}"%20value="'.$this->page.'"><input%20style="cursor:pointer;width:25px;height:18px;border:1px%20solid%20#CCCCCC;"%20type="button"%20value="GO"%20onclick="javascript:var%20page=(this.previousSibling.value>'.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;location=/''.$this->uri.'page=/'+page+/'/'">%20';   }  }

  /*%20在对象内部使用的私有方法,用于获取本页显示的记录条数%20*/  private%20function%20disnum(){   if($this->total%20>%200){    return%20$this->end()-$this->start()+1;   }else{    return%200;   }  } }

   

 

(免责声明:文章内容如涉及作品内容、版权和其它问题,请及时与我们联系,我们将在第一时间删除内容,文章内容仅供参考)
收藏
  • 人气文章
  • 最新文章
  • 下载排行榜
  • 热门排行榜