设为首页 收藏本站
查看: 841|回复: 0

[经验分享] PHP实现翻页处理的类

[复制链接]

尚未签到

发表于 2017-4-3 14:11:00 | 显示全部楼层 |阅读模式
  PHP实例源代码:PHP实现翻页处理的类
  <?php
class Page{
  var $CountAll; //共有纪录数
var $CountPage; //每页显示记录数
var $Link; //显示 完整的分页信息
var $ForPage; //上一页
var $NextPage; //下一页
var $FirstPage; //第一页
var $LastPage; //最后一页
var $CurrPage; //第几页
var $PageNum; //共有多少页
var $Parameter; //参数
var $LimitNum; //不是统计全部记录,而是显示部分记录,例如共有100条记录,但是只统计显示前50条
  function Page($sql, $num=30){
  //初始化,统计记录数
$this->CountPage = $num;
global $mysql;
$sql = base64_decode($sql);
$result = $mysql->Query($sql);
if (0 != $mysql->AffectedRows()){
$row = $mysql->FetchArray($result);
$this->CountAll = $row[0];
}
else{
$this->CountAll = 0;
}
//print "共有 $this->CountAll <br>";
}
  function ListPage($sql, $page=0,$sql_all,$other){
//查询,定义变量,获取数据
  global $mysql;
//print "sql sql<br>";
if (isset($this->LimitNum) && $this->CountAll > $this->LimitNum){
$this->CountAll = $this->LimitNum;
}//更新总浏览记录数
  $sql_src = $sql;
//if ($page > 0){
$sql = base64_decode($sql);
$sql_all = base64_decode($sql_all);
$sql_src = $sql;
//}
//echo $sql;
if (($this->CountAll % $this->CountPage) == 0)//统计共有多少页
$pagecount = (integer)($this->CountAll/$this->CountPage);
else
$pagecount = (integer)($this->CountAll/$this->CountPage)+1;
$this->ageNum = $pagecount;
if ($page > $this->ageNum)//如果页码超过页码总数则设为最大页码
$page = $this->ageNum;
if ($page <= 0)//如果页码小于等于零则将页码设置为1
$page = 1;
  if ($this->CountAll == 0)
{
$this->CurrPage = 0;
}else{
$this->CurrPage = $page;
}
$first_start = ($page-1)*$this->CountPage;
$sql = $sql." limit ".$first_start.", ".$this->CountPage;
//print "2sql<br>";
$result = $mysql->Query($sql);
if (0 != $mysql->AffectedRows()){
$i = 0;
while($row = $mysql->FetchArray($result)){
$array[$i] = $row;
//print "name:".$array[$i][Name]."<br>";
$i++;
}
}
  $sql = base64_encode($sql_src);
$sql_all = base64_encode($sql_all);
if ($pagecount >1){
if($page == 1){
$nextpage = $page+1;
$forpage = 1;
$this->Link = "<button .$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/next.gif" align="absmiddle"> </button><button .$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/prev_end.gif" align="absmiddle"> </button>";
$this->NextPage = "<button .$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/next.gif" align="absmiddle"> </button>";
$this->LastPage = "<button .$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/prev_end.gif" align="absmiddle"> </button>";
}
else if(($page > 1)&&($page < $pagecount)) {
$forpage = $page-1;
$nextpage = $page+1;
$this->Link = "<button .$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/prev_top.gif" align="absmiddle"> </button><button .$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/prev.gif" align="absmiddle"> </button><button .$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/next.gif" align="absmiddle"> </button><button .$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/prev_end.gif" align="absmiddle"> </button>";
$this->ForPage = "<button .$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/prev.gif" align="absmiddle"> </button>";
$this->NextPage = "<button .$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/next.gif" align="absmiddle"> </button>";
$this->FirstPage = "<button .$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/prev_top.gif" align="absmiddle"> </button>";
$this->LastPage = "<button .$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/prev_end.gif" align="absmiddle"> </button>";
}
else if ($page = $pagecount){
$forpage = $page-1;
$nextpage = 1;
$this->Link = "<button .$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/prev_top.gif" align="absmiddle"> </button><button .$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/prev.gif" align="absmiddle"> </button>";
$this->FirstPage = "<button .$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/prev_top.gif" align="absmiddle"> </button>";
$this->ForPage = "<button .$this->Parameter."&sql_all=".$sql_all."".$other."'" class="button_a" style="width:30;height:22";><img src="images/prev.gif" align="absmiddle"> </button>";
}
}
else{
$this->Link = ' ';
}
  return $array;//$array;
  }
  }
/***************
使用例子
  include("class.config.php");
include("class.mysql.php");
include("class.page.php");
global $mysql;
$config = new Config;
$mysql = new TDatabase($config);
  $query_all = "select count(*) from user";
$page_object = new Page($query_all,20);
//new Page('统计记录个数语句',每页记录个数)
if(empty($query_page))
$query_sql = "select * from user";
//注意这里的变量名必须为 $query_sql $query_page ,因为下一页的连接参数默认为 query_sql query_page
  $list = $page_object->ListPage($query_sql,$query_page);
//ListPage('没有limit的前一部分,系统自动根据补齐',察看的页数)
$page_object->Parameter = '&action=view';
//这是传送的Url 所带的其它参数,如果有就修改变量 Parameter ,系统自动将她补在后面
//显示数据
for ($i=0;$i< $page_object->CountPage;$i++)
print $list[$i][ID]."->".$list[$i][UserName]."<br>";
//返回的数据为二维哈西(关联)数组,一维为纪录的标识ID号,二维为哈西(关联)数组,取值标识建议采取用数据库中字段名的方法,例如list[0][UserName]。
  //显示其他相关数据
echo $page_object->CountAll;//纪录总数
echo $page_object->CountPage;//每页显示数据个数
echo $page_object->Link;//显示完整的分页信息
echo $page_object->FirstPage;//第一页
echo $page_object->NextPage;//下一页
echo $page_object->ForPage;//上一页
echo $page_object->LastPage;//最后一页
echo $page_object->CurrPage;//第几页
echo $page_object->PageNum;//共有多少页
$mysql->DatabaseClose();
****************/
  ?>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-359682-1-1.html 上篇帖子: how to use qmail for php 下篇帖子: PHP企业级应用之WebService篇
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表