php封装的page分页类完整实例
本文实例讲述了php封装的page分页类。分享给大家供大家参考,具体如下:
类文件:
<?php
//分页工具类
classPage{
/*
*获取分页字符串
*@param1string$uri,分页要请求的脚本url
*@param3int$counts,总记录数
*@param4int$length,每页显示的记录数
*@param5int$page=1,当前页码
*@returnstring,带有a标签的,可以点击发起请求的字符串
*/
publicstaticfunctiongetPageStr($uri,$counts,$length,$page=1){
//构造一个能够点击的字符串
//得到数据显示的字符串
$pagecount=ceil($counts/$length);//总页数
$str_info="当前一共有{$counts}条记录,每页显示{$length}条记录,一共{$pagecount}页,当前是第{$page}页";
//生成可以操作的连接:首页上一页下一页末页
//求出上一页和下一页页码
$prev=($page<=1)?1:$page-1;
$next=($page>=$pagecount)?$pagecount:$page+1;
$str_click=<<<END
<ahref="{$uri}?page=1">首页</a>
<ahref="{$uri}?page={$prev}">上一页</a>
<ahref="{$uri}?page={$next}">下一页</a>
<ahref="{$uri}?page={$pagecount}">末页</a>
END;
//按照页码分页字符串
$str_number='';
for($i=1;$i<=$pagecount;$i++){
$str_number.="<ahref='{$uri}?page={$i}'>{$i}</a> ";
}
//下拉框分页字符串:利用js的onchang事件来改变当前脚本的href
$str_select="<selectonchange=\"location.href='{$uri}?page='+this.value\">";
//将所有的页码放入到option
for($i=1;$i<=$pagecount;$i++){
if($i==$page)
$str_select.="<optionvalue='{$i}'selected='selected'>{$i}</option>";
else
$str_select.="<optionvalue='{$i}'>{$i}</option>";
}
$str_select.="</select>";
//返回值
return$str_info.$str_click.$str_number.$str_select;
}
}
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP+ajax技巧与应用小结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。