PHP分页类

实例演示简易通用的PHP分页类

关于php分页类,网上有无数多个例子。水平参差不齐,孰优孰劣也没有一一考量。但可以肯定的是,一定有很多优秀的代码。开源最大的好处就是能使功能不断的完善,分享也是一样的道理,众人拾柴火焰高嘛!

笔者在这里把代码贴出来,抛砖引玉。

先来看一下最终的显示效果

1 .简易php分页类.png

这里写图片描述

2 .[PHP]代码

<?php
/***********************************************
 * @类名:   page
 * @参数:   $myde_total - 总记录数
 *          $myde_size - 一页显示的记录数
 *          $myde_page - 当前页
 *          $myde_url - 获取当前的url
 * @功能:   分页实现
 * @作者:   源码布道者
 */
class page {
    private $myde_total;          //总记录数
    private $myde_size;           //一页显示的记录数
    private $myde_page;           //当前页
    private $myde_page_count;     //总页数
    private $myde_i;              //起头页数
    private $myde_en;             //结尾页数
    private $myde_url;            //获取当前的url
    /*
     * $show_pages
     * 页面显示的格式,显示链接的页数为2*$show_pages+1。
     * 如$show_pages=2那么页面上显示就是[首页] [上页] 1 2 3 4 5 [下页] [尾页] 
     */
    private $show_pages;

    public function __construct($myde_total=1,$myde_size=1,$myde_page=1,$myde_url,$show_pages=2){
        $this->myde_total = $this->numeric($myde_total);
        $this->myde_size = $this->numeric($myde_size);
        $this->myde_page = $this->numeric($myde_page);
        $this->myde_page_count = ceil($this->myde_total/$this->myde_size);
        $this->myde_url = $myde_url;
        if($this->myde_total<0) $this->myde_total=0;
        if($this->myde_page<1)  $this->myde_page=1;
        if($this->myde_page_count<1) $this->myde_page_count=1;
        if($this->myde_page>$this->myde_page_count) $this->myde_page=$this->myde_page_count;
        $this->limit = ($this->myde_page-1)*$this->myde_size;
        $this->myde_i=$this->myde_page-$show_pages;
        $this->myde_en=$this->myde_page+$show_pages;
        if($this->myde_i<1){
          $this->myde_en=$this->myde_en+(1-$this->myde_i);
          $this->myde_i=1;
        }
        if($this->myde_en>$this->myde_page_count){
          $this->myde_i = $this->myde_i-($this->myde_en-$this->myde_page_count);
          $this->myde_en=$this->myde_page_count;
        }
        if($this->myde_i<1)$this->myde_i=1;
    }
    //检测是否为数字
    private function numeric($num){
      if(strlen($num)){
         if(!preg_match("/^[0-9]+$/",$num)){
             $num=1;
           }else{
             $num = substr($num,0,11);
         }
      }else{
               $num=1;
      }
      return $num;
    }
    //地址替换
    private function page_replace($page){
        return str_replace("{page}",$page,$this->myde_url);
    }
    //首页
    private function myde_home(){
        if($this->myde_page!=1){
            return "<a href="".$this->page_replace(1)."" title="首页">首页</a>";
        }else{
            return "<p>首页</p>";
        }
    }
    //上一页
    private function myde_prev(){
       if($this->myde_page!=1){
           return "<a href="".$this->page_replace($this->myde_page-1)."" title="上一页">上一页</a>";
       }else{
              return "<p>上一页</p>";
       }
    }
    //下一页
    private function myde_next(){
        if($this->myde_page!=$this->myde_page_count){
            return "<a href="".$this->page_replace($this->myde_page+1)."" title="下一页">下一页</a>";
        }else{
            return"<p>下一页</p>";
        }
    }
    //尾页
    private function myde_last(){
        if($this->myde_page!=$this->myde_page_count){
            return "<a href="".$this->page_replace($this->myde_page_count)."" title="尾页">尾页</a>";
        }else{
            return "<p>尾页</p>";
        }
    }
    //输出
    public function myde_write($id='page'){
       $str ="<div id="".$id."">";
       $str.=$this->myde_home();
       $str.=$this->myde_prev();
       if($this->myde_i>1){
            $str.="<p class="pageEllipsis">...</p>";
       }
       for($i=$this->myde_i;$i<=$this->myde_en;$i++){
            if($i==$this->myde_page){
                $str.="<a href="".$this->page_replace($i)."" title="".$i."" class="cur">$i</a>";
            }else{
          $str.="<a href="".$this->page_replace($i)."" title="".$i."">$i</a>";
            }
       }
       if( $this->myde_en<$this->myde_page_count ){
            $str.="<p class="pageEllipsis">...</p>";
       }
       $str.=$this->myde_next();
       $str.=$this->myde_last();
       $str.="<p class="pageRemark">共<b>".$this->myde_page_count.
             "</b>页<b>".$this->myde_total."</b>条数据</p>";
       $str.="</div>";
       return $str;
    }
}
?>

3 .页面代码

<?php
require_once('./page.class.php'); //分页类
$showrow = 3;//一页显示的行数
$curpage = empty($_GET['page'])?1:$_GET['page'];//当前的页,还应该处理非数字的情况
$url = "?page={page}";//分页地址,如果有检索条件 ="?page={page}&q=".$_GET['q']
//省略了链接mysql的代码,测试时自行添加
$sql = "SELECT * FROM table";
$query = mysql_query($sql);
$total = mysql_num_rows($query);//记录总条数
if(!empty($_GET['page']) && $total !=0 && $curpage > ceil($total/$showrow))
$curpage = ceil($total_rows/$showrow);//当前页数大于最后页数,取最后一页
//获取数据
$get_data = "select * from table limit ".($curpage-1)*$showrow.",$showrow;";
...
?>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>实例演示简易通用的PHP分页类</title>
<style type="text/css">
#page{
    height:40px;
    padding:20px 0px;
}
#page a{
    display:block;
    float:left;
    margin-right:10px;
    padding:2px 12px;
    height:24px;
    border:1px #cccccc solid;
    background:#fff;
    text-decoration:none;
    color:#808080;
    font-size:12px;
    line-height:24px;
}
#page a:hover{
    color:#077ee3;
    border:1px #077ee3 solid;
}
#page a.cur{
    border:none;
    background:#077ee3;
    color:#fff;
}
#page p{
    float:left;
    padding:2px 12px;
    font-size:12px;
    height:24px;
    line-height:24px;
    color:#bbb;
    border:1px #ccc solid;
    background:#fcfcfc;
    margin-right:8px;

}
#page p.pageRemark{
    border-style:none;
    background:none;
    margin-right:0px;
    padding:4px 0px;
    color:#666;
}
#page p.pageRemark b{
    color:red;
}
#page p.pageEllipsis{
    border-style:none;
    background:none;
    padding:4px 0px;
    color:#808080;
}
</style>
</head>

<body>
    <div class="main">
        <div class="showData">
            <!--显示数据区-->
        </div>
        <div class="showPage">
          <?php
             if($total>$showrow){//总记录数大于每页显示数,显示分页
             $page = new page($total,$showrow,$curpage,$url,2);
             echo $page->myde_write();
            }
          ?>
         </div>
    </div>
</body>
</html>

简单分页

/**
 * 自定义分页方法
 * @param unknown_type $url     #分页url,页码采用'%s'表示,例如:http://test.ebers.com/tags/xxx/%s/
 * @param unknown_type $cur_page        #当前页码
 * @param unknown_type $page_fix        #当前页码前后需要显示多少个页码
 * @param intval $total_rows            #数据总数
 * @param intval $pagesize      #每页显示多少数据
 * @param string $cur_page_calss        #当前页css样式名称
 */
public function pagenation($url, $cur_page=1, $page_fix=2, $total_rows=0, $pagesize=10, $cur_page_calss='disable'){
    #计算总页数
    $pagesize = $pagesize>0?$pagesize:10;
    $total_page = ceil($total_rows / $pagesize);

    $code = '';
    if($total_page>1){
        $pager = array();
        #首页
        $pager[] = sprintf( '<ul><li><a href="%s">首页</a></li>', sprintf($url, 1) );

        #页码列表
        for($page_num=$cur_page-$page_fix; $page_num<$cur_page+$page_fix; $page_num++){
            if($page_num<1 || $page_num>$total_page){
                continue;
            }
            $pager[] = sprintf( '<li class="%s"><a href="%s">%s</a></li>', ( $page_num==$cur_page?$cur_page_calss:'' ), sprintf($url, $page_num), $page_num );
        }

        #末页
        $pager[] = sprintf( '<li><a href="%s">末页</a></li><li>共%s条,第%s/%s页</li></ul>', sprintf($url, $total_page), $total_rows, $cur_page, $total_page );

        $code = implode("\n", $pager);
        unset($pager);
    }

    return $code;
}

PHP分页类,支持自定义样式,中间5页等

<?php

//namespace Component;
/**
 * 2016-3-27
 * @author ankang
 */
class Page {
    private $ShowPage;
    private $CountPage;
    private $Floorp;
    private $PageUrl;
    private $PageClass;
    private $CurClass;

    /**
     * @author ankang
     * @param number $CountNum          数据总数
     * @param string $PageUrl           跳转链接
     * @param string $PageClass         <a>标签 总体样式    
     * @param string $PageUrl           当前页样式
     * @param number $PageSize          每页显示的数据条数
     * @param number $ShowPage          每次显示的页数 
     */
    public function __construct($CountNum, $PageUrl = NULL, $PageClass = NULL,$CurClass = NULL, $PageSize = 20, $ShowPage = 5) {
        $this->ShowPage      = $ShowPage;
        $this->CountPage         = ceil ( $CountNum / $PageSize );
        $this->Floorp            = floor ( $ShowPage / 2 ); // 偏移量       
        $this->PageClass         = is_null ( $PageClass ) ? '' : $PageClass;
        $this->CurClass      = is_null ( $CurClass ) ? '' : $CurClass;

        // $ServerURL               = ( preg_match('/\?/i', $_SERVER['REQUEST_URI']))?preg_replace('/\&p\=[0-9]+/i', "", $_SERVER['REQUEST_URI']) : $_SERVER['REQUEST_URI']."?";
        // if( substr($ButURL,0,2)=='//' ){
            // $ServerURL          = substr($ServerURL,1);
        // }
        // $url                 = preg_replace('/p=[\d]*/i', '', $ServerURL);
           $url                 = '';
        //推荐自己传url,不传也可以打开上面的代码自动获取
        $this->PageUrl           = is_null ( $PageUrl ) ? $url : $PageUrl;
    }

    /**
     *
     * @param number $Page          
     * @param string $ShowToPage
     *          首页,上下页,尾页
     * @param string $Html  标签元素,li,p      
     * @return string
     */
    public function getPage($Page = 1, $ShowToPage = true, $Html = null) {
        $StartPage          = ($Page - $this->Floorp); // 开始页码
        $EndPage            = ($Page + $this->Floorp); // 结束页码

        if ($this->CountPage < $this->ShowPage) {
            $StartPage      = 1;
            $EndPage        = $this->CountPage;
        }

        if ($StartPage < 1) {
            $StartPage      = 1;
            $EndPage        = $this->ShowPage;
        }

        if ($EndPage > $this->CountPage) {
            $StartPage      = $this->CountPage - $this->ShowPage + 1;
            $EndPage        = $this->CountPage;
        }

        $PageHtml = '';

        if (! is_null ( $Html )) {
            if ($Html == 'li') {
                $Shtml = '<li>';
                $Ehtml = '</li>';
            } else {
                $Shtml = '<p>';
                $Ehtml = '</p>';
            }
        }

        if (true == $ShowToPage) {
            $PageHtml               .= "$Shtml<a href='{$this->PageUrl}p=1'>&laquo; 首页</a>$Ehtml";
            $PrveUrl                 = $this->getPrve($Page);
            $PageHtml               .= "$Shtml<a href='{$PrveUrl}'>&laquo; 上一页</a>$Ehtml";
        }

        for($i = $StartPage; $i <= $EndPage; $i ++) {
            if ($Page == $i) {
                $PageHtml           .= "$Shtml<a href='{$this->PageUrl}p={$i}' class='{$this->CurClass}'>{$i}</a>$Ehtml";
            } else {
                $PageHtml           .= "$Shtml<a href='{$this->PageUrl}p={$i}' class='{$this->PageClass}'>{$i}</a>$Ehtml";
            }
        }

        if (true == $ShowToPage) {
            $NextUrl                 = $this->getNext($Page);
            $PageHtml               .= "$Shtml<a href='{$NextUrl}'>下一页 &raquo;</a>$Ehtml";
            $PageHtml               .= "$Shtml<a href='{$this->PageUrl}p={$this->CountPage}' >尾页 &raquo;</a>$Ehtml";
        }

        return $PageHtml;
    }

    public function getPrve($Page){
        if ($Page != 1) {
            $Prve                = $Page - 1;
            $PrveUrl             = "{$this->PageUrl}p={$Prve}";
        } else {
            $PrveUrl             = "{$this->PageUrl}p=1";
        }

        return $PrveUrl;
    }

    public function getNext($Page){
        if ($Page != $this->CountPage) {
            $Next                = $Page + 1;
            $NextUrl             = "{$this->PageUrl}p={$Next}";
        } else {
            $NextUrl             = "{$this->PageUrl}p={$this->CountPage}";
        }

        return $NextUrl;
    }



}

万能的分页类

<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * 分页类
 * 使用方式:
 * $page = new Page();
 * $page->init(1000, 20);
 * $page->setNotActiveTemplate('<span>&nbsp;{a}&nbsp;</span>');
 * $page->setActiveTemplate('{a}');
 * echo $page->show();
 * 
 * 
 * @author 风居住的地方
 */
class Page {
    /**
     * 总条数
     */
    private $total;
    /**
     * 每页大小
     */
    private $pageSize;
    /**
     * 总页数
     */
    private $pageNum;
    /**
     * 当前页
     */
    private $page;
    /**
     * 地址
     */
    private $uri;
    /**
     * 分页变量
     */
    private $pageParam;
    /**
     * LIMIT XX,XX
     */
    private $limit;
    /**
     * 数字分页显示
     */
    private $listnum = 8;
    /**
     * 分页显示模板
     * 可用变量参数
     * {total}      总数据条数
     * {pagesize}   每页显示条数
     * {start}      本页开始条数
     * {end}        本页结束条数
     * {pagenum}    共有多少页
     * {frist}      首页
     * {pre}        上一页
     * {next}       下一页
     * {last}       尾页
     * {list}       数字分页
     * {goto}       跳转按钮
     */
    private $template = '<div><span>共有{total}条数据</span><span>每页显示{pagesize}条数据</span>,<span>本页{start}-{end}条数据</span><span>共有{pagenum}页</span><ul>{frist}{pre}{list}{next}{last}{goto}</ul></div>';
    /**
     * 当前选中的分页链接模板
     */
    private $activeTemplate = '<li class="active"><a href="javascript:;">{text}</a></li>';
    /**
     * 未选中的分页链接模板
     */
    private $notActiveTemplate = '<li><a href="{url}">{text}</a></li>';
    /**
     * 显示文本设置
     */
    private $config = array('frist' => '首页', 'pre' => '上一页', 'next' => '下一页', 'last' => '尾页');
    /**
     * 初始化
     * @param type $total       总条数
     * @param type $pageSize    每页大小
     * @param type $param       url附加参数
     * @param type $pageParam   分页变量
     */
    public function init($total, $pageSize, $param = '', $pageParam = 'page') {
        $this->total = intval($total);
        $this->pageSize = intval($pageSize);
        $this->pageParam = $pageParam;
        $this->uri = $this->geturi($param);
        $this->pageNum = ceil($this->total / $this->pageSize);
        $this->page = $this->setPage();
        $this->limit = $this->setlimit();
    }

    /**
     * 设置分页模板
     * @param type $template    模板配置
     */
    public function setTemplate($template) {
        $this->template = $template;
    }

    /**
     * 设置选中分页模板
     * @param type $activeTemplate      模板配置
     */
    public function setActiveTemplate($activeTemplate) {
        $this->activeTemplate = $activeTemplate;
    }

    /**
     * 设置未选中分页模板
     * @param type $notActiveTemplate   模板配置
     */
    public function setNotActiveTemplate($notActiveTemplate) {
        $this->notActiveTemplate = $notActiveTemplate;
    }

    /**
     * 返回分页
     * @return type
     */
    public function show() {
        return str_ireplace(array(
            '{total}',
            '{pagesize}',
            '{start}',
            '{end}',
            '{pagenum}',
            '{frist}',
            '{pre}',
            '{next}',
            '{last}',
            '{list}',
            '{goto}',
        ), array(
            $this->total,
            $this->setPageSize(),
            $this->star(),
            $this->end(),
            $this->pageNum,
            $this->frist(),
            $this->prev(),
            $this->next(),
            $this->last(),
            $this->pagelist(),
            $this->gopage(),
        ), $this->template);
    }

    /**
     * 获取limit起始数
     * @return type
     */
    public function getOffset() {
        return ($this->page - 1) * $this->pageSize;
    }

    /**
     * 设置LIMIT
     * @return type
     */
    private function setlimit() {
        return "limit " . ($this->page - 1) * $this->pageSize . ",{$this->pageSize}";
    }

    /**
     * 获取limit
     * @param type $args
     * @return type
     */
    public function __get($args) {
        if ($args == "limit") {
            return $this->limit;
        } else {
            return null;
        }
    }

    /**
     * 初始化当前页
     * @return int
     */
    private function setPage() {
        if (!empty($_GET[$this->pageParam])) {
            if ($_GET[$this->pageParam] > 0) {
                if ($_GET[$this->pageParam] > $this->pageNum)
                    return $this->pageNum;
                else
                    return $_GET[$this->pageParam];
            }
        }
        return 1;
    }

    /**
     * 初始化url
     * @param type $param
     * @return string
     */
    private function geturi($param) {
        $url = $_SERVER['REQUEST_URI'] . (strpos($_SERVER['REQUEST_URI'], "?") ? "" : "?") . $param;
        $parse = parse_url($url);
        if (isset($parse["query"])) {
            parse_str($parse["query"], $params);
            unset($params["page"]);
            $url = $parse["path"] . "?" . http_build_query($params);
            return $url;
        } else {
            return $url;
        }
    }

    /**
     * 本页开始条数
     * @return int
     */
    private function star() {
        if ($this->total == 0) {
            return 0;
        } else {
            return ($this->page - 1) * $this->pageSize + 1;
        }
    }

    /**
     * 本页结束条数
     * @return type
     */
    private function end() {
        return min($this->page * $this->pageSize, $this->total);
    }

    /**
     * 设置当前页大小
     * @return type
     */
    private function setPageSize() {
        return $this->end() - $this->star() + 1;
    }

    /**
     * 首页
     * @return type
     */
    private function frist() {
        $html = '';
        if ($this->page == 1) {
            $html .= $this->replace("{$this->uri}&page=1", $this->config['frist'], true);
        } else {
            $html .= $this->replace("{$this->uri}&page=1", $this->config['frist'], false);
        }
        return $html;
    }

    /**
     * 上一页
     * @return type
     */
    private function prev() {
        $html = '';
        if ($this->page > 1) {
            $html .= $this->replace($this->uri.'&page='.($this->page - 1), $this->config['pre'], false);
        } else {
            $html .= $this->replace($this->uri.'&page='.($this->page - 1), $this->config['pre'], true);
        }
        return $html;
    }

    /**
     * 分页数字列表
     * @return type
     */
    private function pagelist() {
        $linkpage = "";
        $lastlist = floor($this->listnum / 2);
        for ($i = $lastlist; $i >= 1; $i--) {
            $page = $this->page - $i;
            if ($page >= 1) {
                $linkpage .= $this->replace("{$this->uri}&page={$page}", $page, false);
            } else {
                continue;
            }
        }
        $linkpage .= $this->replace("{$this->uri}&page={$this->page}", $this->page, true);
        for ($i = 1; $i <= $lastlist; $i++) {
            $page = $this->page + $i;
            if ($page <= $this->pageNum) {
                $linkpage .= $this->replace("{$this->uri}&page={$page}", $page, false);
            } else {
                break;
            }
        }
        return $linkpage;
    }

    /**
     * 下一页
     * @return type
     */
    private function next() {
        $html = '';
        if ($this->page < $this->pageNum) {
            $html .= $this->replace($this->uri.'&page='.($this->page + 1), $this->config['next'], false);
        } else {
            $html .= $this->replace($this->uri.'&page='.($this->page + 1), $this->config['next'], true);
        }
        return $html;
    }

    /**
     * 最后一页
     * @return type
     */
    private function last() {
        $html = '';
        if ($this->page == $this->pageNum) {
            $html .= $this->replace($this->uri.'&page='.($this->pageNum), $this->config['last'], true);
        } else {
            $html .= $this->replace($this->uri.'&page='.($this->pageNum), $this->config['last'], false);
        }
        return $html;
    }

    /**
     * 跳转按钮
     * @return string
     */
    private function gopage() {
        $html = '';
        $html.='&nbsp;<input type="text" value="' . $this->page . '" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>' . $this->pageNum . ')?' . $this->pageNum . ':this.value;location=\'' . $this->uri . '&page=\'+page+\'\'}" style="width:25px;"/><input type="button" onclick="javascript:var page=(this.previousSibling.value>' . $this->pageNum . ')?' . $this->pageNum . ':this.previousSibling.value;location=\'' . $this->uri . '&page=\'+page+\'\'" value="GO"/>';
        return $html;
    }

    /**
     * 模板替换
     * @param type $replace     替换内容
     * @param type $result      条件
     * @return type
     */
    private function replace($url, $text, $result = true) {
        $template = ($result ? $this->activeTemplate : $this->notActiveTemplate);

         $html = str_replace('{url}', $url, $template);
         $html = str_replace('{text}', $text, $html);
        return $html;
    }
}

很随意的写出来的

<?php
/**
 * Created by PhpStorm.
 * User: zerodeng
 * Date: 15-5-12
 * Time: 下午4:09
 */

class pagination {

    private $total;
    private $per_page;
    private $link;
    private $page;
    private $page_offset;
    private static $instance;

    public function __construct(){
        $this->per_page = 40;
    }

    public static function get_instance(){
        if(!isset(self::$instance)){
            self::$instance = new self;
        }
        return self::$instance;
    }

    public function set_total($num){
        $this->total = $num;
    }

    public function set_link($link=''){
        if($link){
            $this->link = $link.'?page=';
        }else{
            $this->link = $_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'].'?page=';
        }
    }

    public function set_page(){
        if(!empty($_GET['page'])){
            $this->page = $_GET['page'];
        }else{
            $this->page = 1;
        }
    }

    public function set_page_offset(){
        if(isset($this->page)){
            $this->page_offset = ($this->page-1)*$this->per_page;
        }
    }

    public function page_limit($mode=''){
        $this->set_page();
        $this->set_page_offset();
        if($mode == 'str'){
            $limit = $this->page_offset.','.$this->per_page;
            return $limit;
        }
        $limit = array($this->page_offset,$this->per_page);
        return $limit;
    }

    public function show_page($page_num=5){
        if(!isset($this->page)){
            $this->set_page();
        }
        $first_page = 1;
        $last_page = ceil($this->total/$this->per_page);
        if(!isset($this->link)){
            $this->set_link();
        }
        $html = '';
        $html .= '<div id="show_page">总共'.$this->total.'条记录 '.$this->page.'/'.$last_page;
        if($this->page != $first_page){
            $html .= '<a href="http://'.$this->link.$first_page.'">首页</a>&nbsp';
        }
        if($page_num>=$last_page){
            for($i = 1;$i<=$last_page;$i++){
                $html .= '|&nbsp<a href="http://'.$this->link.$i.'">'.$i.'</a>&nbsp';
            }
        }else{
            $page_offset = $this->page+$page_num;
            if($page_offset<$last_page){
                for($i=$this->page;$i<=($this->page+$page_num);$i++){
                    $html .= '|&nbsp<a href="http://'.$this->link.$i.'">'.$i.'</a>&nbsp';
                }
            }else{
                for($i=$this->page;$i<=$last_page;$i++){
                    $html .= '|&nbsp<a href="http://'.$this->link.$i.'">'.$i.'</a>&nbsp';
                }
            }

        }
        if($this->page != $last_page){
            $html .= '<a href="http://'.$this->link.$last_page.'">最后一页</a>&nbsp';
        }
        $html .= '</div>';
        return $html;
    }

}

又是一个随便写的分页类!看原理,很强大

<?php
    class page
    {
        private $pagesize;
        private $lastpage;
        private $totalpages;
        private $nums;
        private $numPage=1;

        function __construct($page_size,$total_nums)
        {
            $this->pagesize=$page_size;      //每页显示的数据条数
            $this->nums=$total_nums;     //总的数据条数
            $this->lastpage=ceil($this->nums/$this->pagesize);     //最后一页
            $this->totalpages=ceil($this->nums/$this->pagesize);   //总得分页数
            if(!empty($_GET[page]))
            {
                $this->numPage=$_GET[page];
                if(!is_int($this->numPage))  $this->numPage=(int)$this->numPage;
                if($this->numPage<1)  $this->numPage=1;
                if($this->numPage>$this->lastpage) $this->numPage=$this->lastpage;
            }
        }

        function show_page_result()
        {
            $row_num=(($this->numPage)-1) * $this->pagesize; //表示每一页从第几条数据开始显示
            $row_num=$row_num.",";
            $SQL="SELECT * FROM `test` LIMIT $row_num $this->pagesize";
            $db=new database();
            $query=$db->execute($SQL);
            while($row=mysql_fetch_array($query))
            {
                echo "<b>".$row[name]." | ".$row[sex]."<hr>";
            }
            $db=null;
        }

        function show_page_way_1()  //以"首页 上一页 下一页 尾页"形式显示
        {
            $url=$_SERVER["REQUEST_URI"];
            $url=parse_url($url);   //parse_url -- 解析 URL,返回其组成部分,注: 此函数对相对路径的 URL 不起作用。
            $url=$url[path];
            if($this->nums > $this->pagesize)      //判断是否满足分页条件
            {
                echo " 共 $this->totalpages 页 当前为第<font color=red><b>$this->numPage</b></font>页 共 $this->nums 条 每页显示 $this->pagesize 条";
                if($this->numPage==1)
                {
                    echo " 首页 ";
                    echo "上一页 ";
                }
                if($this->numPage >= 2 && $this->numPage <= $this->lastpage)
                {
                    echo " <a href=$url?page=1>首页</a> " ;
                    echo "<a href=$url?page=".($this->numPage-1).">上一页</a> " ;
                }

                if($this->numPage==$this->lastpage)
                {
                    echo "下一页 ";
                    echo "尾页<br>";
                }
                if($this->numPage >= 1 && $this->numPage < $this->lastpage)
                {
                    echo "<a href=$url?page=".($this->numPage+1).">下一页</a> ";
                    echo "<a href=$url?page=$this->lastpage>尾页</a><br> ";
                }
            }
            else    return;
        }

        function show_page_way_2()      //以数字形式显示"首页 1 2 3 4 尾页"
        {
            $url=$_SERVER["REQUEST_URI"];
            $url=parse_url($url);   //parse_url -- 解析 URL,返回其组成部分,注: 此函数对相对路径的 URL 不起作用。
            $url=$url[path];
            if($this->nums > $this->pagesize)
            {
                if($this->numPage==1)    echo "首页";
                else    echo "<a href=$url?page=1>首页</a>";
                for($i=1;$i<=$this->totalpages;$i++)
                {
                    if($this->numPage==$i)
                    {
                        echo " ".$i." ";
                    }
                    else
                    {
                        echo " <a href=$url?page=$i>$i</a> ";
                    }

                }
                if($this->numPage==$this->lastpage)       echo "尾页";
                else    echo "<a href=$url?page=$this->lastpage>尾页</a>";
            }
        }

        function show_page_way_3()
        {
            global $c_id;
            $url=$_SERVER["REQUEST_URI"];
            $url=parse_url($url);   //parse_url -- 解析 URL,返回其组成部分,注: 此函数对相对路径的 URL 不起作用。
            $url=$url[path];
            if($this->nums > $this->pagesize)      //判断是否满足分页条件
            {
                if($c_id)
                {
                    echo "到第<select name='select1' onChange=\"location.href='$url?c_id=$c_id&page='+this.value+'&pagesize=$this->pagesize'\">";
                }
                else    echo "到第<select name='select1' onChange=\"location.href='$url?page='+this.value+'&pagesize=$this->pagesize'\">";
                for($i = 1;$i <= $this->totalpages;$i++)
                echo "<option value='" . $i . "'" . (($this->numPage == $i) ? 'selected' : '') . ">" . $i . "</option>";
                echo "</select>页, 每页显示";
                if($c_id)
                {
                    echo "<select name=select2 onChange=\"location.href='$url?c_id=$c_id&page=$this->numPage&pagesize='+this.value+''\">";
                }
                else    echo "<select name=select2 onChange=\"location.href='$url?page=$this->numPage&pagesize='+this.value+''\">";
                for($i = 0;$i < 5;$i++) // 将个数定义为五种选择
                {
                    $choice= ($i+1)*4;
                    echo "<option value='" . $choice . "'" . (($this->pagesize == $choice) ? 'selected' : '') . ">" . $choice . "</option>";
                }
                echo "</select>个";
            }
            else    return;     //echo "没有下页了";

        }





    }


?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值