一个简单的PHP分页类

<?php
/**
 * $pager =  new cls_pager($total,$pernum);
 * $limit = $pager->getlimit();//获得limit 语句
 * $pagerhtml = $pager->gethtml();//获得分页效果
 *
 */
class module_pager2
{
    private $total;//总条数
    private $pagetotal;//总页数
    private $curpage;//当前页
    private $pagenum;//每页数
    private $limit;//limit 语句
    private $nextpage;
    private $prepage;
    private $url='';
    public function module_pager2($total,$pernum=20)
    {
        $this->total = $total;
        $this->pagenum = $pernum;
        $this->pagetotal = ($total>0)? ceil($this->total / $this->pagenum): 1;
        $this->curpage = (isset($_GET['p']) && ($_GET['p'] > 0)) ? intval($_GET['p']) : 1;
        $this->curpage = ($this->curpage > $this->pagetotal) ? $this->pagetotal : $this->curpage;
        $this->nextpage = ($this->curpage == $this->pagetotal) ? '' : ($this->curpage + 1);
        $this->prepage = ($this->curpage == 1) ? '' : ($this->curpage - 1);
    }
    public function getlimit()
    {
        $start = ($this->curpage - 1) * $this->pagenum;
        $str = " limit $start,$this->pagenum";
        return $str;
    }
    /**
     * 计算limit值
     */
    public function get_limit()
    {
        $start = ($this->curpage - 1) * $this->pagenum;
        return " $start,$this->pagenum";
    }

    public function geturl()
    {
        if($this->url==''){
            $this->url = 'index.php?'. http_build_query( $_GET );
        }
        $this->url = preg_replace('/[?,&]p=(\w)+/','',$this->url);
        $this->url .= (strpos($this->url,"?") === false) ? '?' : '&';
        return $this->url;
    }
    public function seturl($url){
        $this->url=$url;
    }
    public function gethtml1()
    {
        $s = $this->curpage - 5;
        $i = ($s > 0) ? $s : 1;
        $e = $this->curpage + 5;
        $end = ($e < $this->pagetotal) ? $e : $this->pagetotal;
        $html = '<div class="pageItem clearfix fr"><div class="fl mr10">';
        $url = $this->geturl();

        if($this->curpage > 5) {
            $html .= "<a href=".$url."p=1>首页</a> ";
        }
        if($this->prepage) {
            $html .= "<a href=".$url."p=".$this->prepage.">上一页</a> ";
        }

        for($i;$i <= $end; $i++) {
            if($i == $this->curpage) {
                $html .= "<em>".$i . "</em> ";
                //$html .= "<a href='javascript:void(0)' class='cur'>".$i . "</a>";
            } else {
                $html .= "<a href=".$url."p=$i>$i</a> ";
            }
        }

        if($this->nextpage && ($this->nextpage <= $this->pagetotal)) {
            $html .= "<a href=".$url."p=".$this->nextpage.">下一页</a> ";
        }

        if(($this->curpage + 5) < $this->pagetotal) {
            $html .= "<a href=".$url."p=".$this->pagetotal.">末页</a> ";
        }
        $html .= '</div><div class="fl mr10">到第<input type="text" id="changepage" οnkeydοwn="if(event.keyCode==13) {window.location=\''.$url.'p=\'+this.value; return false;}">页</div><a href="javascript:;" οnclick="window.location=\''.$url.'p=\'+document.getElementById(\'changepage\').value; return false;" class="fl btn02">确定</a></div>';
        //$html .= " 共 $this->total 个 $this->pagetotal 页";
        return $html;
    }

    /**
     *
     * @return type
     */
    public function gethtml()
    {
        $s = $this->curpage - 5;
        $i = ($s > 0) ? $s : 1;
        $e = $this->curpage + 5;
        $end = ($e < $this->pagetotal) ? $e : $this->pagetotal;
        $html = '';
        $url = $this->geturl();

        if($this->curpage > 5) {
            $html .= "<a class=\"up\" href=".$url."p=1>首页</a> ";
        }
        if($this->prepage) {
            $html .= "<a class=\"up\" href=".$url."p=".$this->prepage.">上一页</a> ";
        }

        for($i;$i <= $end; $i++) {
            if($i == $this->curpage) {
                $html .= '<span>' . $i . '</span>';
            } else {
                $html .= "<a class=\"\" href=".$url."p=$i>$i</a> ";
            }
        }

        if($this->nextpage && ($this->nextpage <= $this->pagetotal)) {
            $html .= "<a class=\"down\" href=".$url."p=".$this->nextpage.">下一页</a> ";
        }

        if(($this->curpage + 5) < $this->pagetotal) {
            $html .= "<a class=\"down\" class=\"down\" href=".$url."p=".$this->pagetotal.">末页</a> ";
        }

        //$html .= "共 $this->total 个 $this->pagetotal 页";
        return $html;
    }


    /**
     * 用于当分页需要ajax时传递javascript函数,获得交易记录时用到
     * author:Riven<jiangtao1@leju.sina.com.cn>
     * date:2011-07-09
     * @return type
     */
    public function get_pagination_html($jump_js = null, $cut_line = '', $current_class ='current')
    {
        $s = $this->curpage - 5;
        $i = ($s > 0) ? $s : 1;
        $e = $this->curpage + 5;
        $end = ($e < $this->pagetotal) ? $e : $this->pagetotal;
        $html = '';
        $url = $this->geturl();
        if($this->pagetotal)
        {
            $html .= '<span class="gray">共' . $this->pagetotal . '页</span>';
        }
        if(isset($_SERVER['HTTP_HOST']))
        {
            $base_url = 'http://' . $_SERVER['HTTP_HOST'] . '/';
        }
        else
        {
            $base_url = 'http://www.eju.com/';
        }
        $url = $base_url . $url;
        if($jump_js)
        {
            $url = 'javascript:' . $jump_js . '(\'' . $url . 'p=' . '%s' . '\');';
        }
        else
        {
            $url .= 'p=' . '%s';
        }
        if($this->prepage)
        {
            $html .= '<a href="' . sprintf($url, $this->prepage) . '" class="prev"><</a>' . $cut_line;
        }
        if($end != 1)
        {
            if($i != 1)
            {
                if($i == 2)
                {
                    $html .= '<a  href="'.sprintf($url, 1) . '" οnclick="return false" >' . 1 . '</a>';
                }
                else
                {
                    $html .= '<a  href="'.sprintf($url, 1) . '" οnclick="return false" >' . 1 . '</a>…';
                }
            }
            for($i;$i <= $end; $i++)
            {
                if($i == $this->curpage) {
                    $html .= '<a  href="#" οnclick="return false" class="'.$current_class.'" >' . $i . '</a>' . $cut_line;
                } else {
                    $html .= '<a href="'.sprintf($url, $i) . '">'.$i.'</a>' . $cut_line;
                }
            }
            if($this->pagetotal != $end)
            {
                if($this->pagetotal -1 == $end)
                {
                    $html .= '<a  href="'.sprintf($url, $this->pagetotal) . '" οnclick="return false" >' . $this->pagetotal . '</a>';
                }
                else
                {
                     $html .= '<a  href="'.sprintf($url, $this->pagetotal) . '" οnclick="return false" >' . $this->pagetotal . '</a>…';
                }
            }
        }

        if($this->nextpage && ($this->nextpage <= $this->pagetotal))
        {
            $html .= '<a href="'.sprintf($url, $this->nextpage) . '" class="next">></a>';
        }
        $html = trim($html, $cut_line);
        return $html;
    }
}


调用方法:<pre name="code" class="php">	$pager = new module_pager2( $total, $page_size);
		$page_html = $pager->gethtml();
		$page_url = $pager->geturl();

 
并将对应变量传递给前端页面:
<pre name="code" class="php">		$this->_view->assign('pager', 		 $page);
		$this->_view->assign('page_html', 	$page_html);
		$this->_view->assign('page_url', 	$page_url);
		$this->_view->assign('total', 	$total);


 


前端简单调用,这里是smarty模板的写法

<div class="pageLine clearfix">
					<div class="fl pt5 pl5">共计<!--{$list_count}-->条记录</div>
					<div class="pageItem clearfix fr">
						<div class="fl mr10">
							<!--{$page_html}-->
						</div>
					</div>
	</div>

然后写个最简单的样式:

<style>
.pageItem a{
	padding: 9px;
    border: 1px solid #E4DDDD;
}
.pageItem span{
	padding: 9px;
    border: 1px solid #E4DDDD;
    margin-right: 15px;
    background-color: rgb(107, 206, 115);
}
.pageItem > div{
	float:none;
}
</style>
最后效果图:



整体都很简单,在此做下笔记


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值