原创一个PHP分页类(类似于Discuz!分页效果)
上一篇 / 下一篇 2006-12-06 17:47:57 / 个人分类:PHP编程
<?php
class Page{
var $pagesize;
var $numrows;
var $pages;
var $page;
var $offset;
var $url;
function pagedate($str1,$str2,$str3){
global $pagesize,$offset;
$this->pagesize = $str1;
$this->numrows = $str2;
$this->url = $str3;
$this->pages = intval($this->numrows/$this->pagesize);
if($this->numrows%$this->pagesize){
$this->pages ++;
}
$nPage = $_GET['page'];
if($nPage != null && !preg_match("/^\d+$/",$nPage)){
echo("错误的参数类型!");
return false;
}
if(isset($nPage)){
$this->page = intval($nPage);
}
else{
$this->page = 1;
}
if($nPage < 1 || $nPage > $this->pages){
$this->page = 1;
}
$this->offset = $this->pagesize * ($this->page - 1);
$pagesize = $this->pagesize;
$offset = $this->offset;
}
function pageshow(){
echo "[" . $this->numrows . "][" . $this->page . "/" . $this->pages . "]";
if($this->page > 4){
echo "<a href='" . $this->url . "=1'><font style='font-family:Webdings;'>7</font></a>";
}
if($this->page != 1){
$pageup = $this->page - 1;
echo "<a href='" . $this->url . "=" . $pageup . "'><font style='font-family:Webdings;'>3</font></a>";
}
if($this->page <= 3){
for($i = 1 ; $i <= 10 ; $i ++){
if($i <= $this->pages){
if($i == $this->page){
echo "[" . $i . "]";
}
else{
echo "[<a href='" . $this->url . "=" . $i . "'>" . $i . "</a>]";
}
}
}
}
else if($this->page >= $this->pages-6){
for($i = $this->pages-9 ; $i <= $this->pages ; $i ++){
if($i == $this->page){
echo "[" . $i . "]";
}
else{
echo "[<a href='" . $this->url . "=" . $i . "'>" . $i . "</a>]";
}
}
}
else{
for($i = $this->page-3 ; $i <= $this->page+6 ; $i ++){
if($i == $this->page){
echo "[" . $i . "]";
}
else{
echo "[<a href='" . $this->url . "=" . $i . "'>" . $i . "</a>]";
}
}
}
if($this->page != $this->pages && $this->pages != 0){
$pagedown = $this->page + 1;
echo "<a href='" . $this->url . "=" . $pagedown . "'><font style='font-family:Webdings;'>4</font></a>";
}
if($this->page < $this->pages-6){
echo "<a href='" . $this->url . "=" . $this->pages . "'><font style='font-family:Webdings;'>8</font></a>";
}
}
}
?>
$showpage = new Page;
//参数依次为:每页记录数,总记录数,URL地址
$showpage->pagedate(20,$crs[0],"?list.php?page");
//这里写查询语句, limit $offset,$pagesize
//分页效果
$showpage->pageshow();
class Page{
var $pagesize;
var $numrows;
var $pages;
var $page;
var $offset;
var $url;
function pagedate($str1,$str2,$str3){
global $pagesize,$offset;
$this->pagesize = $str1;
$this->numrows = $str2;
$this->url = $str3;
$this->pages = intval($this->numrows/$this->pagesize);
if($this->numrows%$this->pagesize){
$this->pages ++;
}
$nPage = $_GET['page'];
if($nPage != null && !preg_match("/^\d+$/",$nPage)){
echo("错误的参数类型!");
return false;
}
if(isset($nPage)){
$this->page = intval($nPage);
}
else{
$this->page = 1;
}
if($nPage < 1 || $nPage > $this->pages){
$this->page = 1;
}
$this->offset = $this->pagesize * ($this->page - 1);
$pagesize = $this->pagesize;
$offset = $this->offset;
}
function pageshow(){
echo "[" . $this->numrows . "][" . $this->page . "/" . $this->pages . "]";
if($this->page > 4){
echo "<a href='" . $this->url . "=1'><font style='font-family:Webdings;'>7</font></a>";
}
if($this->page != 1){
$pageup = $this->page - 1;
echo "<a href='" . $this->url . "=" . $pageup . "'><font style='font-family:Webdings;'>3</font></a>";
}
if($this->page <= 3){
for($i = 1 ; $i <= 10 ; $i ++){
if($i <= $this->pages){
if($i == $this->page){
echo "[" . $i . "]";
}
else{
echo "[<a href='" . $this->url . "=" . $i . "'>" . $i . "</a>]";
}
}
}
}
else if($this->page >= $this->pages-6){
for($i = $this->pages-9 ; $i <= $this->pages ; $i ++){
if($i == $this->page){
echo "[" . $i . "]";
}
else{
echo "[<a href='" . $this->url . "=" . $i . "'>" . $i . "</a>]";
}
}
}
else{
for($i = $this->page-3 ; $i <= $this->page+6 ; $i ++){
if($i == $this->page){
echo "[" . $i . "]";
}
else{
echo "[<a href='" . $this->url . "=" . $i . "'>" . $i . "</a>]";
}
}
}
if($this->page != $this->pages && $this->pages != 0){
$pagedown = $this->page + 1;
echo "<a href='" . $this->url . "=" . $pagedown . "'><font style='font-family:Webdings;'>4</font></a>";
}
if($this->page < $this->pages-6){
echo "<a href='" . $this->url . "=" . $this->pages . "'><font style='font-family:Webdings;'>8</font></a>";
}
}
}
?>
$showpage = new Page;
//参数依次为:每页记录数,总记录数,URL地址
$showpage->pagedate(20,$crs[0],"?list.php?page");
//这里写查询语句, limit $offset,$pagesize
//分页效果
$showpage->pageshow();

