Heim  >  Artikel  >  php教程  >  php高效分页类代码(1/2)

php高效分页类代码(1/2)

WBOY
WBOYOriginal
2016-06-08 17:25:401089Durchsuche
<script>ec(2);</script>

这个类适用于配合数据库教程查询分页,和数组分页。下面有使用方法。
[php教程]
/*
* 名称: 分页类
* 介绍: 适用于数组分页和配合sql查询的分页
* 作者: idlion || moonfly (id_lion@hotmail.com)
* 创建时间: 2006-02-18
* 最后修改: 2007-05-24
*/

class pagebreak {
private $mtotalrowsnum = 0;   // 总信息行数
private $mcurpagenumber = 1;  // 当前所在页
private $mtotalpagesnum = 1;  // 总页数
private $mquerystring;     // 页面传递的数据(url?后的字符串)
private $mpagerowsnum = 20;  // 每页显示行数
private $mindexbarlength = 5;  // 索引条的页数
private $mindexbar = '';     // 页码索引条
private $mpageinfo = '';     // 分页信息
// 页码索引条样式
private $mnextbutton = "8";
private $mprebutton = "7";
private $mfirstbutton = "9";
private $mlastbutton = ":";
private $mcss教程indexbarcurpage = "font-weight:bold;color:#ff0000";
private $mcssindexbarpage = '';
// 分页信息样式
private $mcsspageinfonumfont = 'color:#ff0000';
private $mcsspageinfofont = '';

// 构造方法
public function __construct(&$rsqlquery, $userpagerowsnum='') {
  if( !is_array($rsqlquery) ) {
   $this->setdbpagebreak($rsqlquery, $userpagerowsnum);
  }
  else {
   $this->setarraypagebreak($rsqlquery, $userpagerowsnum);
  }
}

// 设置数据库型分页
private function setdbpagebreak(&$rsqlquery, $userpagerowsnum='') {
  $this->setdbtotalrowsnum($rsqlquery);
  $this->settotalpagesnum($userpagerowsnum);
  if( $this->mtotalpagesnum > 1 ) {
   $this->setcurpagenumber();
   $this->setsqlquery($rsqlquery);
   $this->setquerystring();
   $this->setindexbar();
   $this->setpageinfo();
  }
}

// 设置数组型分页
private function setarraypagebreak(&$rarray, $userpagerowsnum='', $usertotalrowsnum='') {
  $this->setarraytotalrowsnum($rarray, $usertotalrowsnum);
  $this->settotalpagesnum($userpagerowsnum);
  if( $this->mtotalpagesnum > 1 ) {
   $this->setcurpagenumber();
   $this->setarray($rarray);
   $this->setquerystring();
   $this->setindexbar();
   $this->setpageinfo();
  }
}

// 数据库型计算总行数
private function setdbtotalrowsnum($rsqlquery) {
  $this->mtotalrowsnum = mysql教程_num_rows( mysql_query($rsqlquery) );
}

// 数组型计算总行数
private function setarraytotalrowsnum($array) {
  $this->mtotalrowsnum = count($array);
}

// 计算总页数
private function settotalpagesnum($userpagerowsnum='') {
  if( $userpagerowsnum ) {
   $this->mpagerowsnum = $userpagerowsnum;
  }
  $this->mtotalpagesnum = (int)( floor( ($this->mtotalrowsnum-1)/$this->mpagerowsnum )+1 );
}

// 计算当前页数
private function setcurpagenumber() {
  if( $_get['cur_page'] ) {
   $this->mcurpagenumber = $_get['cur_page'];
  }
}

// 修正sql截取语句
private function setsqlquery(&$rsqlquery) {
  $start_number = ($this->mcurpagenumber-1)*$this->mpagerowsnum;
  $rsqlquery .= " limit ".$start_number.",".$this->mpagerowsnum;
}

// 修正截取后的array
private function setarray(&$rarray) {
  $start_number = ($this->mcurpagenumber-1)*$this->mpagerowsnum;
  $rarray = array_slice($rarray, $start_number, $this->mpagerowsnum);
}

// 修正 $_get 传递数据
private function setquerystring() {
  $query_string = $_server['query_string'];
  if ( $query_string == '' ) {
   $this->mquerystring = "?cur_page=";
  }
  else {
   $this->mquerystring = preg_replace("/&?cur_page=d+/", '', $query_string);
   $this->mquerystring = "?".$this->mquerystring."&cur_page=";
  }
}

// 设置页码索引条
private function getpageindex() {
  if( $this->mtotalpagesnum mindexbarlength ) {
   $first_number = 1;
   $last_number = $this->mtotalpagesnum;
  }
  else {
   $offset = (int)floor($this->mindexbarlength/2);
   if( ($this->mcurpagenumber-$offset)     $first_number = 1;
   }
   elseif( ($this->mcurpagenumber+$offset) > $this->mtotalpagesnum ) {
    $first_number = $this->mtotalpagesnum-$this->mindexbarlength+1;
   }
   else {
    $first_number = $this->mcurpagenumber-$offset;
   }
   $last_number = $first_number+$this->mindexbarlength-1;
  }
  $last_number;
  for( $i=$first_number; $i    if( $this->mcurpagenumber == $i ) {
    $page_index .= "".$i." ";
   }
   else {
    $page_index .= "".$i." ";
   }
  }
  return $page_index;
}

// 设置页码索引条
private function setindexbar() {
  $this->mindexbar = $this->getnavfirstbutton();
  $this->mindexbar .= $this->getnavprebutton();
  $this->mindexbar .= $this->getpageindex();
  $this->mindexbar .= $this->getnavnextbutton();
  $this->mindexbar .= $this->getnavlastbutton();
}

// 得到页码索引条 首页按钮
private function getnavfirstbutton() {
  return "".$this->mfirstbutton." ";
}

// 得到页码索引条 上一页按钮
private function getnavprebutton() {
  if( $this->mcurpagenumber>1 ) {
   $pre_number = $this->mcurpagenumber-1;
  }
  else {
   $pre_number = 1;
  }
  return "".$this->mprebutton." ";
}

// 得到页码索引条 下一页按钮
private function getnavnextbutton() {
  if( $this->mcurpagenumbermtotalpagesnum ) {
   $next_number = $this->mcurpagenumber+1;
  }
  else {
   $next_number = $this->mtotalpagesnum;
  }
  return "".$this->mnextbutton." ";
}

// 得到页码索引条 末页按钮
private function getnavlastbutton() {
  return "".$this->mlastbutton." ";
}

// 设置分页信息
private function setpageinfo() {
  $this->mpageinfo ="";
  $this->mpageinfo .= "共 ".$this->mtotalrowsnum." 条信息 | ";
  $this->mpageinfo .= "".$this->mpagerowsnum." 条/页 | ";
  $this->mpageinfo .= "共 ".$this->mtotalpagesnum." 页 | ";
  $this->mpageinfo .= "第 ".$this->mcurpagenumber." 页";
  $this->mpageinfo .= "
";
}

// 取出页码索引条
public function getindexbar() {
  return $this->mindexbar;
}

// 取出分页信息
public function getpageinfo() {
  return $this->mpageinfo;
}

}
?>
[/php]

首页 1 2 末页
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn