php+mysql 公共分页类 思想是通过mysql 的 LIMIT 来实现

$pagesize = 5; //每页条数
$page = ($page < 1) ? 1 : $page;
 $start = ($page - 1) * $pagesize; //起始条数

 //信息列表
$menulist = $this->CONN->getList("tb_admin_manager", "*", $where,'ORDER BY Fid', "LIMIT $start,$pagesize");

分页类:

<?php

class Pager2 {

    /**
     * 分页时获取参数
     * @param type $page
     * @return string
     */
    function urlAnalyze($page) {
        $url = $_SERVER['REQUEST_URI'];
        $parse_url = parse_url($url);
        $url_query = isset($parse_url['query']) ? $parse_url['query'] : '';
        if ($url_query) {
            if (strpos($url_query, 'page=') === false) {
                $url .= '&page=PAGE';
            } else {
                $url = str_replace('page=' . trim($page), 'page=PAGE', $url);
            }
        } else {
            $url .= '?page=PAGE';
        }
        return $url;
    }

    /**
     * 分页函数
     * @param type $total 总记录数
     * @param type $page  当前页
     * @param type $pagesize 每页条数
     * @param type $showlvtao 中间页面个数 为奇数
     * @return type
     */
    function getPager2($total, $page, $pagesize, $showlvtao) {

        $pageData = array();
        //分页总数 总记录数/每页条数
        $pageData['pageCount'] = ceil($total / $pagesize);
        $pageData['pageSize'] = $pagesize;
        //$page=min($pageData['pageCount'],$page);

        if (0 < $page - 1) {
            $pageData['prevPage'] = $page - 1;
        } else {
            $pageData['prevPage'] = 1;
        }
        if ($pageData['pageCount'] >= $page + 1) {
            $pageData['nextPage'] = $page + 1;
        } else {
            $pageData['nextPage'] = $page;
        }
        $pageData['lastPage'] = $pageData['pageCount'];

        if ($page > $pageData['pageCount']) {
            $page = $pageData['pageCount'];
        }
        $nextPageUrl = $this->urlAnalyze($page);

        $html = '<div class="pagerInfo">';
        $html .= '<span>总记录:' . $total . '</span>';
        $html .= '<span>当前页:' . $page . '/共' . $pageData['pageCount'] . '页</span>';
        $html .= '<a href="' . str_replace('PAGE', 1, $nextPageUrl) . '" title="首页">首页</a>';
        $html .= '<a href="' . str_replace('PAGE', $pageData['prevPage'], $nextPageUrl) . '" title="上一页">上一页</a>';

        $o = $showlvtao; //中间页码表总长度,为奇数
        $u = ceil($o / 2); //根据$o计算单侧页码宽度$u
        $f = $page - $u; //根据当前页$currentPage和单侧宽度$u计算出第一页的起始数字
        if ($f < 0) {
            $f = 0;
        }//当第一页小于0时,赋值为0
        $n = $pageData['pageCount']; //总页数,

        if ($n < 1) {
            $n = 1;
        }//当总数小于1时,赋值为1
        if ($page == 1) {
            $html.='<strong>1</strong>';
        } else {
            $html.='<a href="' . str_replace('PAGE', 1, $nextPageUrl) . '">1</a>';
        }
        ///////////////////////////////////////
        for ($i = 1; $i <= $o; $i++) {
            if ($n <= 1) {
                break;
            }//当总页数为1时
            $c = $f + $i; //从第$c开始累加计算
            if ($i == 1 && $c > 2) {
                $html.='<a>...</a>';
            }
            if ($c == 1) {
                continue;
            }
            if ($c == $n) {
                break;
            }
            if ($c == $page) {
                $html.='<strong>' . $page . '</strong>';
            } else {
                $html.='<a href="' . str_replace('PAGE', $c, $nextPageUrl) . '">' . $c . '</a>';
            }
            if ($i == $o && $c < $n - 1) {
                $html.='<a>...</a>';
            }
            if ($i > $n) {
                break;
            }//当总页数小于页码表长度时 
        }
        if ($page == $n && $n != 1) {
            $html.='<strong>' . $n . '</strong>';
        } else {
            $html.='<a href="' . str_replace('PAGE', $n, $nextPageUrl) . '">' . $n . '</a>';
        }

        $html .= '<a href="' . str_replace('PAGE', $pageData['nextPage'], $nextPageUrl) . '" title="下一页">下一页</a>';
        $html .= '<a href="' . str_replace('PAGE', $pageData['lastPage'], $nextPageUrl) . '" title="末页">末页</a>';
        $html.='<span id=loc>';
        $html.="跳至<select name='topage' size='1' onchange='gotoUrl(\"$nextPageUrl\" , this.value)'>\n";
        for($i=1;$i<=$pageData['pageCount'];$i++){
        if($i==$page) $html.="<option value='$i' selected>$i</option>\n";
        else $html.="<option value='$i'>$i</option>\n";
        }
        $html.="</select>页";
        $html.='</span>';
        $html .= <<<JS
        <script language="javascript" type="text/javascript">
            function gotoUrl(url,pageNum) {
                var gotoUrl = url.replace("PAGE", pageNum);
                window.location= gotoUrl;
            }
        </script>
JS;
        return $html;
    }

}

posted @ 2013-01-23 14:39  /折磨  阅读(374)  评论(0编辑  收藏  举报