php分页类

Eddy 发布于2013-3-11 23:14:48 分类: 技术心得 已浏览loading 网友评论1条 我要评论


自己写了个分页类,表现层基于bootstrap。

pagination.class.php


<?php

/**
 * 分页类,表现层基于bootstrap
 *
 * @author Eddy
 * */
class pagination {

    private $pageSize; // 每页显示记录数
    private $totalCount; // 总记录数
    private $pageUrl; // 页面链接
    private $pageNavNum; // 导航条条目数
    public $pageNum; // 总页数
    public $curPageNum; // 当前页面
    public $showGoto; // 是否显示跳转连接
    public $position; // 分页导航位置 right center left
    public $showTotal; // 是否显示总页数

    // 构造函数

    public function __construct($totalCount, $pageSize, $pageUrl, $pageNavNum, $showGoto = false, $showTotal = false, $position = 'right') {
        $this->totalCount = $totalCount;
        $this->pageSize = $pageSize;
        $this->pageUrl = $pageUrl;
        $this->pageNavNum = $pageNavNum > 3 ? $pageNavNum : 3;
        $this->pageNum = ceil($totalCount / $pageSize);
        $this->position = $position;
        $this->showGoto = $showGoto;
        $this->showTotal = $showTotal;
    }

    // 获取分页导航链接数组
    private function getNavUrlArr() {
        $navUrl = array();
        if ($this->pageNum > $this->pageNavNum) {
            if ($this->pageNum - $this->curPageNum < $this->pageNavNum) {
                $start = $this->pageNum - $this->pageNavNum + 1;
            } else {
                $start = $this->curPageNum;
            }

            for ($i = $start; $i < min($this->pageNavNum + $this->curPageNum, $this->pageNum + 1); $i++) {
                if ($this->curPageNum == $i) {
                    $navUrl [] = '<li class="active"><a href="' . $this->pageUrl . $i . '">' . $i . '</a></li>';
                } else {
                    $navUrl [] = '<li><a href="' . $this->pageUrl . $i . '">' . $i . '</a></li>';
                }
            }
        } else {
            for ($i = 0; $i < $this->pageNum; $i++) {
                if ($this->curPageNum == $i + 1) {
                    $navUrl [] = '<li class="active"><a href="' . $this->pageUrl . ($i + 1) . '">' . ($i + 1) . '</a></li>';
                } else {
                    $navUrl [] = '<li><a href="' . $this->pageUrl . ($i + 1) . '">' . ($i + 1) . '</a></li>';
                }
            }
        }

        return $navUrl;
    }

    // 生成分页导航条
    public function generatePageNav() {
        $navStr = '';
        $midStr = '';
        if ($this->position == 'right') {
            $navStr .= '<div class="pagination pagination-right"><ul>';
        } else if ($this->position == 'center') {
            $navStr .= '<div class="pagination pagination-centered"><ul>';
        } else {
            $navStr .= '<div class="pagination"><ul>';
        }
        if ($this->totalCount <= 0) {
            return null;
        }
        $navUrl = $this->getNavUrlArr();
        foreach ($navUrl as $v) {
            $midStr .= $v;
        }
        if ($this->curPageNum == 1) {
            if ($this->pageNum > 1) {
                $navStr .= $midStr . '<li><a href="' . $this->pageUrl . ($this->curPageNum + 1) . '">下一页&gt;</a></li>' . '<li><a href="' . $this->pageUrl . $this->pageNum . '">末页</a></li>';
            } else {
                $navStr .= $midStr;
            }
        } else if ($this->curPageNum == $this->pageNum) {
            $navStr .= '<li><a href="' . $this->pageUrl . '1">首页</a></li>' . '<li><a href="' . $this->pageUrl . ($this->curPageNum - 1) . '">&lt;上一页</a></li>' . $midStr;
        } else {
            $navStr .= '<li><a href="' . $this->pageUrl . '1">首页</a></li>' . '<li><a href="' . $this->pageUrl . ($this->curPageNum - 1) . '">&lt;上一页</a></li>' . $midStr . '<li><a href="' . $this->pageUrl . ($this->curPageNum + 1) . '">下一页&gt;</a></li>' . '<li><a href="' . $this->pageUrl . $this->pageNum . '">末页</a></li>';
        }

        if ($this->showTotal) {
            $navStr .= "<li><a>当前第 $this->curPageNum 页|共 $this->pageNum 页</a></li>";
        }

        if ($this->showGoto) {
            $navStr .= '<li><a>跳转至:<select onchange="goto(this)">';
            for ($i = 1; $i < $this->pageNum + 1; $i++) {
                if ($i == $this->curPageNum) {
                    $navStr .= "<option selected='selected' value='" . $this->pageUrl . "$i'>第 $i 页</option>";
                } else {
                    $navStr .= "<option value='" . $this->pageUrl . "$i'>第 $i 页</option>";
                }
            }
        }
        $navStr .= '</select></a></li></ul></div>';

        $script = '<script type="text/javascript">
	function goto(th){
		window.location.href = th.value;
	}
</script>';
        $navStr .= $script;
        return $navStr;
    }

}

// END class pagination
?>

<?php
//使用方法
require 'pagination.class.php';

$totalCount = 700;//总记录数查询数据库获取
$pageSize = 20;
$pageUrl = 'http://www.rrgod.com/list.php?page=';
$pg = new pagination($totalCount, $pageSize, $pageUrl, 10, true, true, 'right');
$pg->curPageNum = (($_GET['page'] > $pg->pageNum) or (intval($_GET['page']) <= 0)) ? 1 : $_GET['page'];
echo $pg->generatePageNav();
?>
效果:

已经有(1)位网友发表了评论,你也评一评吧!
原创文章如转载,请注明:转载自Eddy Blog
原文地址:http://www.rrgod.com/technique/873.html     欢迎订阅Eddy Blog

关于 php分页  的相关文章

  1. 发表于2013-3-24 18:57:48

    刚好新建了织梦网站,碰到了这个问题,想PHP分页,找了很久都没找到方法,想不到在这里可以找到了

记住我的信息,下次不用再输入 欢迎给Eddy Blog留言