本类能够实现像百度、谷歌一样的分页效果
**********
*
* 分页的pojo
*
* @author wangyalei
*
*/
class PageInfo{
// 下一页
private $nextpage;
//上一页
private $uppage;
//当前页
private $curpage;
//总页数
private $allpage;
//数据总数量
private $allnumber;
// 每页显示的个数
public static $page_record = 2;
//最多显示的分页数
public static $liststep = 10;
//用来初步初始化会一些信息
public function __construct($allnumber,$curpage){
$this->allpage = intval(($allnumber+self::$page_record-1)/self::$page_record);
$this->curpage = $curpage ;
$this->allnumber = $allnumber;
}
//分页信息显示到第几页
public function getListend(){
//如果总页数没有 预定页数多的 显示总页数
if($this->allpage < self::$liststep){
return $this->allpage;
}else{
$listend = $this->curpage + self::$liststep/2;
if($listend > $this->allpage){
$listend = $this->allpage+1;
}
return $listend;
}
}
//分页的开始显示
public function getlistbegin(){
$listbegin=(intval($this->curpage)-intval(ceil((double)self::$liststep/2)));
if ($listbegin < 1)
$listbegin = 1;
return $listbegin;
}
//得到下一页
public function getNextpage(){
return $this->curpage + 1 ;
}
//得到上一页
public function getUppage(){
return $this->curpage - 1;
}
//得的当前显示的页数
public function getCurpage(){
return $this->curpage;
}
//得到总页数
public function getAllpage(){
return $this->allpage ;
}
//得到数据的总数量
public function getAllnumber(){
return $this->allnumber ;
}
//每页显示的个数
public static function getPage_record(){
return $this->page_record ;
}
}
测试一下
<?php
/*
* Created on 2010-8-17
*
* @author wangyalei
*
*/
header("Content-type: text/html; charset=gbk");
require("mysql.php");
require("PageInfo.php");
//判断 有page 请求么
//取得url中的page从参数
$url = $_SERVER["REQUEST_URI"];
// 如果有
if(strpos($url,"page=")!==false){
//从浏览器请求冲查看 当前是第几页
$curpage = $_GET["page"];
}else{
$curpage = 1;
}
//得到数据库连接
$conn = MySql::getInstatnce();
//数据总数量
$sql = "select * from test";
$query = $conn->query($sql);
$num = $conn->row_num($query);
// 计算当前页数据显示从第几个开始
//($nowpage = ($curpage -1)*每页显示的个数)
$nowpage =($curpage - 1)*PageInfo::$page_record.",";
/*******
* @param $num 数据总数量
* @param $curpage 当前页
*/
$pageInfo = new PageInfo($num,$curpage);
// 进行分页处理
$sql = "select * from test limit $nowpage ".PageInfo::$page_record;
//执行sql 语句
$result = $conn->query($sql);
while( $row =$conn->fetch_array($result)){
echo "<hr><b>".$row["name"]." | ".$row["sex"];
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<style type="text/css">
<!--
.STYLE3 {font-size: 12px}
.STYLE1 {color: #00FFCC}
-->
.bi {background-color:#D9E1F7;height:20px;margin-bottom:12px}
A:link {COLOR: blue; TEXT-DECORATION: none}
A:active {COLOR: blue; TEXT-DECORATION: none}
A:visited {COLOR: #660066; TEXT-DECORATION: none}
A:hover {COLOR: blue; TEXT-DECORATION: none;position:relative;left:2px;top:2px}
</style>
</head>
<script>
function Num(){
var num=document.form1.num.value;
if(num=="页/数"){
document.form1.num.value="";
}
}
function SelectNum(){
var num=document.form1.num.value;
if(!isNumeric(num)){
alert("请输入数字");
return false;
}
form1.action="test.php?page="+num+"";
form1.submit();
}
function isNumeric(value){
if( value != null && value.length>0 && isNaN(value) == false){
return true;
}
else{
return false;
}
}
</script>
<body>
<form id="form1" name="form1" method="post" action="test.php">
<p>
<?php if(intval($pageInfo->getCurpage())>1){ ?>
<a href=test.php?page=1><span class="tt3">首页</span></a>
<a href=test.php?page=<?php echo $pageInfo->getUppage();?>><span class="tt3">上一页</span></a>
<?php } ?>
<?php
for($i=$pageInfo->getlistbegin(); $i<$pageInfo->getListend(); $i++){
if($i!=$pageInfo->getCurpage()){
?>
<?php echo "<a href=test.php?page=".$i .">[".$i ."]</a>";?>
<?php } else{ ?>
<?php echo "[".$i ."]";?>
<?php }
}
?>
<?php if(intval($pageInfo->getCurpage())!=intval($pageInfo->getAllpage())){ ?>
<a href=test.php?page=<?php echo $pageInfo->getNextpage();?>><span class="tt3">下一页</span></a>
<a href=test.php?page=<?php echo $pageInfo->getAllpage(); ?>><span class="tt3">尾页</span></a>
<?php }?>
<label>
<input name="num" type="text" id="num" value="页/数" size="6" ></input>
</label>
<label>
<input type="button" name="Submit2" value="GO" >
</label>
</p>
</form>
</body>
</html>
效果如下
如果用smarty模板或是其他模板来做就更简单了
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com