Aug 25

PHP多功能分页类 不指定

/**
* @descrition
* 此类为PHP+MYSQL的分页显示类,此类是通过自己先前写的分页函数修改而来,本类继承了当前
* 分页显示的绝大部分功能,并且对这么功能可以筛选,选择自己所需的功能,本类提供的功能有:
* 基本翻页按扭(可对按扭自行设计)、分页统计功能(对当前页的数据进行统计,使用户更加一目
* 了然)、跳转功能(这里我们提供了两种跳转方式,可以通过设置相关参数进而来选择自己所需要
* 的跳转方式,这两种跳转方式为:下拉式菜单跳转文本输入框跳转,可以通过设置id为JumpSele
* ct的元素的样式来改变跳转框的界面)、对跳转的页号,提供了加密功能,这也是可以根据相关的
* 参数设置来定制的.
* @Author:    hqlong
* @CreatTime:   2006-04-06
* @UpdateTime-1: 2006-07-26
* @UpdateTime-2: 2006-11-07
* @UpdateTime-3: 2007-1-21
* @LastUpdateTime: 2007-3-16
* @CopyRight: hqlong
* @Introduce:
* 1. 首先实例化这个类($sql:sql查询语句,如果规定了limit,则每页显示数以limit后的数目
*    为准,如果末给定,刚采用默认显示条数,$CycNum:循环显示的页号数,缺省显示$CycNum*2
*     条,此参数可以缺少,该参数的所有功能得要在$IsDisNum设为true时才有效。
*     $page = new ($sql,$CycNum);
* 2. 调用分页函数,并返回经格式化后的sql语句,$Sortid:排序ID,整个显示都是依$Sortid来
*    进行排序的,$IsEncode:是否对页号加密,该参数可以省略,缺省时不加密该,该参数值为
*     bool型,取值范围true,false。$IsNeedStat:是否显示统状况,可省略,缺省时不显示,
*     取值范围true、false,$SortMethod为排序方式,取值范围up or down,up为升序, down
*     为降序,如果不提供,只默认为down
*     $sql = $page->StartPage($Sortid,$IsEncode = false,$IsNeedStat = false,
*    $SortMethod = "down");
* 3. 接下来就是执行$sql,和平时查询数据的操作一样
*     $result = mysql_query($sql)
*     while($arr = mysql_fetch_array($result)){
*       echo $arr[0];//这里打印输入
×  }
* 4. 显示翻页按扭,和自己定制的一种功能 $ButtomArray:翻页按扭定置数组,定置普通按扭的
*     形式如 $ButtomArray = array("首页","上页","下页","末页"),这里是一个数组,而且
*     数组元素个素为四,如果不按此规定设置,一切设置将视为无效,显示将按程序默认方式显示
*     $JumpType:定制跳转框,取值范围为select、text、none,其中select为下拉菜单显示框
*     ,text文本输入框,none不定制任何跳转框,此处注意,当设置成text时,不能对页号进行加
*     密,此参数可省略,缺省状况下不显示任何跳转框,$IsDisNum:是否显示循环页号数,取值范
*     围为true,false,此参数可以省略,缺省状况下为false,即,不显示循环页号.
*     $page->EndPage($ButtonArray,$JumpType = "none",$IsDisNum = false)
* 5. 如果对程序提供的统计显示位置感到不满意,可以通过调用$page->PageStat()来得到统计
*     状态,此返值字符串,即可以把这些统计文字放在你想放的任何地方
* @Example:
*   $sql = "select iBookId,vBookName from xx_book limit 0,10";
* $page =   new Page($sql);
* //StartPage($Sortid,$IsEncode = false,$IsNeedStat = false,$SortMethod = "down")
* $sql =   $page->StartPage("iBookId",false,true,"down");
* $result = mysql_query($sql);
* while($arr = mysql_fetch_array($result)){
*   echo $arr[1]."
";
* }
* $ButtonArray = array("首页","上页","下页","末页");
*   //可单独显示
* //echo "
".$page->PageStat()."

";
* echo $page->EndPage($ButtonArray,'text',true);
*
*/
class Page{
/**
  * @description
  * 每页记录数,如果未给定,则默认显示数为 10 条
  * @var int
    */
private $PageSize = 10;
/**
  *
  * @description
  * 总页数
  * @var int
    */
private $TotalPage;
/**
  * @description
  * 总记录数
  * @var int
    */
private $RecordNum;
/**
  * @description
  * 记录总数
  * @var int
    */
private $NowPage;
/**
  * @description
  * 执行的sql语句
  * @var int
    */
private $QueryString;
/**
  * @description
  * 地址栏中的页数是否加密
  * 默认不加密
  * @var string
    */
private $IsEncode = false;
/**
  *
  * @description 是否需要显示当前显示状态
  * @var unknown_type
    */
private $IsNeedStat;
/**
  * ******************************************************
  * @description 循环显示页号数
  * 默认显示数 10 条
  * @var int
  */
private $CycNum = 5;
/**
  *
  * @description 析构函数,该分页类创建对象时,自动调用
  * 对sql语句进行判断,获取文章每页显示数
  * @param string $sql
    */
public function __construct($sql,$CycNum = 5){
  if(!@mysql_ping()){
   echo "Please check your database link";
   exit;  
  }
  if(is_numeric($CycNum)){
   $this->CycNum = $CycNum;
  }else{
   $this->CycNum = $this->CycNum;
  }
  if(trim($sql) != ""){
   /*if(preg_match("/limit/",$sql)){
    list($sql,$limit) = explode("limit",$sql);
   }else{
    list($sql,$limit) = explode("LIMIT",$sql);
   }
   $this->QueryString = $sql;
   list($cnt1,$cnt2) = explode(",",$limit);
   if(!empty($cnt2)){
    $this->PageSize = $cnt2;
   }elseif(!empty($cnt1)){
    $this->PageSize = $cnt1;
   }else{
    $this->PageSize = $this->PageSize;
   }*/
   /**修改于2006年11月23日,解决出现Notice: Undefined offset: 2***/
   if(preg_match("/limit/",$sql)){
    list($sql,$limit) = explode("limit",$sql);
   }else if(preg_match("/LIMIT/",$sql)){
    list($sql,$limit) = explode("LIMIT",$sql);
   }
   //$this->QueryString = $sql;
   if(isset($limit)){
    list($cnt1,$cnt2) = explode(",",$limit);
    if(!empty($cnt2)){
     $this->PageSize = $cnt2;
    }elseif(!empty($cnt1)){
     $this->PageSize = $cnt1;
    }else{
     $this->PageSize = $this->PageSize;
    }
   }
   $this->QueryString = $sql;
   unset($cnt1);
   unset($cnt2);
  }
}
/**
  *
  * @description 获取相应规定数目的记录
  * 并计算出总记录数,总页数等比较重要的参数
  * @param int $Sortid
  * 排序ID
  * @param bool $IsEncode
  * 页号是否加密,true为加密,false为不加密
  * @return string
  */
public function StartPage($Sortid,$IsEncode = false,$IsNeedStat = false,$SortMethod = "down"){
  $Result = mysql_query($this->QueryString);
  $this->RecordNum = @mysql_num_rows($Result);
  $this->TotalPage = ceil($this->RecordNum/$this->PageSize);
  /*******************************************************
   * 初始化类属性IsNeedStat
   *******************************************************/
  if($IsNeedStat === true or $IsNeedStat === false){
   $this->IsNeedStat = $IsNeedStat;
  }else{
   echo "Warning:方法StartPage中的参数IsNeedStat只能是bool";
  }
  /*******************************************************
   * 初始化类属性IsEncode
   *******************************************************/
  if($IsEncode === true or $IsEncode === false){
   $this->IsEncode = $IsEncode;
  }else{
   echo "Warning:方法StartPage中的参数IsEncode只能是bool";
  }
 
  /*******************************************************
   *接收从url中传过来的当前页数,如果非数字,刚获取其整数值
   *******************************************************/
  if(isset($_REQUEST['NowPage'])){
   $this->NowPage = intval($_REQUEST['NowPage']);
  }
 
  /*******************************************************
   *根据$IsEncode的值来判断页号是否解密,如果$IsEncode的值非bool,
   *则给出警告信息,但并不影响程序执行,且默认不加密.
   *******************************************************/
  if($this->IsEncode === true){
    $this->NowPage = intval($this->StrDecode($_REQUEST['NowPage']));
  }
  //排列顺序 up 升序 down 降序
/* if(isset($SortMethod) && $SortMethod == "up"){
   $SortMethod = "ASC";
  }elseif(isset($SortMethod) && $SortMethod == "down"){
   $SortMethod = "DESC";
  }else{
   echo "Warning:方法StartPage中的参数SortMethod只能是down或者up";
  }*/
  if(!isset($this->NowPage)){
   $this->NowPage = 1;
  }elseif($this->NowPage <= 0){
   $this->NowPage = 1;
  }elseif($this->NowPage > $this->TotalPage){
   $this->NowPage = $this->TotalPage;
  }else{
   $this->NowPage = $this->NowPage;
  }
  $OffSet = $this->PageSize * ($this->NowPage -1);
  $sql = $this->QueryString." ORDER BY ".$Sortid."   LIMIT ".$OffSet.",".$this->PageSize;
  return $sql;
}
/**
  *
  * @description
  * 翻页按扭的显示,如:首页 上页 下页 末页,可以定制自定义翻页按扭样式
  * 此函数也是 外面调用此类的入口.将返回经处理后的sql语
  * @param array $ButtonArray
  * 翻页按扭形式,用户可随意定制:如:
  * $ButtonArray = array("首页","上页","下页","末页");
  * @param string $JumpType
  * 附加选项,通过能参数,可定制跳转框,select 下拉跳转框,text 文
  * 本输入跳转框,none 不定制任何跳转框
  * @param bool $IsDisNum
  * 是否显示循环分页,true 显示 false 不显示;如:
  * 1 2 3 4 5 6 7 8 9 10
  * @return string
  */
public function EndPage($ButtonArray,$JumpType = "none",$IsDisNum = false){
  $FirstPage = 1;
  $PrePage     = $this->NowPage - 1;
  $NextPage     = $this->NowPage + 1;
  $LastPage     = $this->TotalPage;
  /**
   * 根据参数$ButtomArray来得到用户定制的按扭,如果参数给出类型
   * 不正确,则采用系统默认按扭
   */
  if(!is_array($ButtonArray) or count($ButtonArray) != 4){
   $ButtonArray = array("First","Precede","Next","Last");
  }
  if($this->IsNeedStat === true){
   $ReturnStr    = "

".$this->PageStat()."

";
  }else{
   $ReturnStr    = "";
  }  
  $ReturnStr   .= $this->ToPage($FirstPage,$ButtonArray[0],"First");
  $ReturnStr   .= " ";
  $ReturnStr   .= $this->ToPage($PrePage,$ButtonArray[1],"Pre");
  if($IsDisNum === true){
   $ReturnStr   .= " ".$this->DisPageNum()." ";
  }elseif($IsDisNum === false){
   $ReturnStr .= " ";
  }else{
   /**
    * 对参数的合法性,进行审核
    */
   echo ('Warning:方法EndPage()中参数$IsDisNum的类型是bool,只能是 true或者false
');
   $ReturnStr .= " ";
  }
  $ReturnStr   .= $this->ToPage($NextPage,$ButtonArray[2],"Next");
  $ReturnStr   .= " ";
  $ReturnStr   .= $this->ToPage($LastPage,$ButtonArray[3],"Last");
  $ReturnStr   .= " ";
  if($JumpType === 'select'){
   $ReturnStr .= $this->JumpSelect('select');
  }elseif($JumpType === 'text'){
   $ReturnStr .= $this->JumpSelect('text');
  }elseif($JumpType === 'none'){
   //待写入
  }else{
   echo ('Warning:方法EndPage()中参数$JumpType 的值只能是 select text none
');
  }
  return $ReturnStr;
}
/**
  *
  * @description
  * 创建翻页按扭,并根据$Flag 的值来设置按扭是否可用,即:按
  * 扭是否带有链接,此此函数外界不可访问,属于该类私有方法
  * @param int $Page 将要跳转的页数
  * @param string $Msg 跳转按扭名称
  * @param string $Flag 按扭显示类型的判断
  * @return string
  */
private function ToPage($Page,$Msg,$Flag = ''){
  /*
   * 对$this->IsEncode为真,则对页数进行解密
   */
  if($this->IsEncode === true){
   $Page = $this->StrEncode($Page);
  }  
  $Url = $this->GetUrl($Page);
  $UrlStr = "".$Msg."";
  /*
   *如果当前页是小于或者等于第1页,那么首页和上页不显示链接
   * 如果当前页大于或者等于最后一页,那么末页和下页不显示链接
   * 对于其它情况,四个跳转按扭都显示*******
   */
  if(($this->NowPage <= 1 and ($Flag == "First" or $Flag == "Pre"))
  or ($this->NowPage >= $this->TotalPage and($Flag == "Next" or $Flag == "Last"))){
   $UrlStr = "".$Msg."";
  }else{
   //待写入
  }
  return $UrlStr;
}
/**
  *
  * @description
  * 获取当前的URL地址,并对将要跳转的地址做出修改,此方法也属于私有
  * 方法,外界不可访问,只能被类内部调用
  * @param int   $Page 将要跳转的页数
  * @return string
  */
private function GetUrl($Page){
/*下面代码于2006年12月1日进行了修改,其中$_SERVER['REMOTE_ADDR']被修改成下面的
  *$_SERVER['SERVER_ADDR'];$_SERVER['REMOTE_ADDR']为远程客户端地址,而$_SERVE
  *R['SERVER_ADDR']为服务器端地址SERVER_ADDR*/
  if($_SERVER['SERVER_PORT'] == 80){
   $Url = "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
  }else{
   $Url = "http://".$_SERVER['SERVER_NAME'].":".$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];
  }
// $Url = preg_replace("/\?NowPage=[0-9]*&?/i","",$Url);
// $Url = preg_replace("/&NowPage = [0-9]*&?/i","",$Url);
  /*** *********************************************
   * 对url做出修改,将url中的?NowPage = "任何字符" 或者
   * $NowPage = "任何字符" 替换成 空,供下面对url的增加
   *************************************************/
  $Url = preg_replace("/\?NowPage=.*&?/i","",$Url);
  $Url = preg_replace("/&NowPage=.*&?/i","",$Url);
  /***************************************************
   *如果$Page = 0,则只取得当前URL,不再加向url全传值
   ***************************************************/
  if($Page === 0){
   return $Url;
  }
  /*判断先前是否已存在url传值,如果存在,
   *则加&NowPage=,否则加?/NowPage =
   */
  if(preg_match("/\?/",$Url)){
   $Url = $Url."&NowPage=".$Page;
  }else{
   $Url = $Url."?NowPage=".$Page;
  }
 
  return $Url;
}
/**
  *
  * @description
  * 对分页状况的一个统计,此方法可以定制,
  * 即:可要,也可不要,如需要显示当前状况
  * 在外部需显示的地方调用此方法即可.
  * @return string
  */
public function PageStat(){
  $StatString   = "共有 ".$this->RecordNum." 条记录   ";
  $StatString .= "当前第 ".$this->NowPage." 页/共有 ".$this->TotalPage." 页 ";
  $StatString .= "每页显示 ".$this->PageSize." 条";
  return $StatString;
}
/**
  *
  * @description
  * 创建两种跳转框,一种下拉跳转菜单,一种文本输入菜单
  * 下拉菜单跳转框,当鼠标选中需跳转的页后,程序自动
  * 进行跳转到所指定的页数,文本输入菜单则是用户在文本
  * 框内输入要跳转的页数,鼠标外部单击,刚自己跳转
  * @param string $JumpType
  * 跳转菜单类型 select 下拉跳转框 text 文章输入跳转框,
  * none 不定制任何跳转框
  */
private function JumpSelect($JumpType){
  $Url = $this->GetUrl(0);//获取当前url
  /**
   *判断先前是否被已经URL传值******
   */
  if(preg_match("/\?/",$Url)){
   $Url = $Url."&NowPage";
  }else{
   $Url = $Url."?NowPage";
  }
  $JumpString   = "转到";
  if($JumpType === 'select'){
   $JumpString .= " 页";
  }elseif($JumpType === 'text'){
   $Title = "鼠标外部单击,文章转向";
   $JumpString .= "    $JumpString .= "javascript:if(isNaN(parseInt(this.value)) || parseInt(this.value)";
   $JumpString .= " > ".$this->TotalPage."){alert('您输入的数字只能在 1 ~ ".$this->TotalPage;
   $JumpString .= "之间,请重新输入'); return false;}location.href='".$Url."='+parseInt(this.value)\" title='".$Title."'>";
   $JumpString .= " 页";
  }else{
 
  }
  return $JumpString;
}
/**
  *
  * @description 对页数进行循环显示,如 1 2 3 4 5
  * @return unknown
  */
private function DisPageNum(){
  //循环显示规定数目的页号
  if(!isset($PageNumString)){
   $PageNumString = "";
  }
  for($i = $this->NowPage - $this->CycNum;$i < $this->NowPage + $this->CycNum;$i++){
   if($i <= 0){
    $i = 0;
    continue;
   }elseif($i > $this->TotalPage){
    break;
   }elseif($i == $this->NowPage){
    $PageNum = "[".$i."]";
    $PageNumString .= $this->ToPage($i,$PageNum)." ";
   }else{
    $PageNumString .= $this->ToPage($i,$i)." ";
   }
  }
  return $PageNumString;
}
/**
  *
  * 对字符串进行加密
  * @param string $str
  * @return string
  */
private function StrEncode($str){
  $encodeArr = str_split($str);
  $encode = '';
  for($i = 0; $i < count($encodeArr); $i++){
   $encodeStr = ord($encodeArr[$i]) + $i;
   $encode .= ($encodeStr)."|<<&>>|";
  }
  return urlencode(base64_encode(($encode)));
}
/**
  *
  * @description 对字符加密的字符串进行解密
  * @param string $str
  * @return string
  */
private function StrDecode($str){
  $decode = base64_decode(urldecode($str));
  $decodeArr = explode("|<<&>>|",$decode);
  $decodeStr = '';
   for($i = 0;$i < count($decodeArr);$i++){
    $decode = $decodeArr[$i];
    $decode = $decode;
    $decodeStr .= chr($decode - $i);
   }
   return $decodeStr;
}
/**
  *
  * @description
  * 本分页类的析构函数
  */
public function __destruct(){
  //echo "destruct";
}
}
?>




<style type="text/css">
/*链接面号的样式*/
.pagelink {
margin:0px 0px 0px 0px;
padding:0px 3px 0px 3px;
border:1px solid #159BD0;
}
/*下拉菜单或文本输入框样式*/
#JumpSelect{
background:#159bd0;
border:1px solid #ffff66;
color:#ffff66;
}
</style>




<?php

require_once("page.class.php");
$conn = mysql_connect("localhost","root","qinglong");
mysql_select_db("xx_libb");
$sql = "select iBookId,vBookName from xx_book limit 0,10";
/**
* 首先实例化这个类($sql:sql查询语句,如果规定了limit,则每页显示数以limit后的数目
* 为准,如果末给定,刚采用默认显示条数,$CycNum:循环显示的页号数,缺省显示$CycNum*2
* 条,此参数可以缺少,该参数的所有功能得要在$IsDisNum设为true时才有效。
* 原型$page = new ($sql,$CycNum);
*/
$page = new Page($sql,5);

$sql = $page->StartPage("iBookId",false,false,'down');
$result = mysql_query($sql);
//按扭名称
$ButtonArray = array("<","<<",">>",">");
/*显示分页状况,此功能和IsNeedStat设置为ture里的功能一样,如果对系统系统统计状态的位置不
//echo "<br>".$page->PageStat()."<p>";
/*原型$page->EndPage($ButtonArray,$JumpType = "none",$IsDisNum = false)

echo $page->EndPage($ButtonArray,"select",true);

?>




/*********
* 方法原型
*调用分页函数,并返回经格式化后的sql语句,$Sortid:排序ID,整个显示都是依$Sortid来
  *进行排序的,$IsEncode:是否对页号加密,该参数可以省略,缺省时不加密该,该参数值为
  *bool型,取值范围true,false。$IsNeedStat:是否显示统状况,可省略,缺省时不显示,
  *取值范围true、false,$SortMethod为排序方式,取值范围up or down,up为升序, down
  *为降序,如果不提供,只默认为down
$sql = $page->StartPage($Sortid,$IsEncode = false,$IsNeedStat = false,$SortMethod = "down");
****************/
2323
April 16, 2008 23:31
你的网站用火狐浏览器打开好像有点变形啊
ouwsh 回复于 April 17, 2008 05:27
噢。。这个倒没有测试过。是风格问题。换个就好了。
分页: 1/1 第一页 1 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]