`
wangyalei
  • 浏览: 51969 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

php学习笔记(仿百度分页类)

    博客分类:
  • PHP
阅读更多

本类能够实现像百度、谷歌一样的分页效果

 

**********
 *
 * 分页的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" onfocus="Num()"></input>
     </label>
     <label>
     <input type="button" name="Submit2" value="GO" onClick="SelectNum()" >
     </label>
  </p>
</form>
</body>
</html>

 效果如下 




 如果用smarty模板或是其他模板来做就更简单了
  • 大小: 2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics