Home >php教程 >php手册 >实用的简单PHP分页集合包括使用方法

实用的简单PHP分页集合包括使用方法

WBOY
WBOYOriginal
2016-06-06 20:27:183097browse

收集了三个PHP分页方法,总会有一个适合你用的。

方法一:

复制代码 代码如下:


    /*
    分页类 用于实现对多条数据分页显示 
    version:1.0
    Date:2013-10-20
    */

    /*
        调用非常方便,先连接好数据库,直接传人查询的sql字符串即可,也可以指定每页显示的数据条数
        例如$pages = new Page('SELECT * FROM `zy_common_member`');
        或  $pages = new Page('SELECT * FROM `zy_common_member`', 10);
    */
    class Page{
        private $curPage;
        private $totalPages;//数据总共分多少页显示
        private $dispNum;//每页显示的数据条数
        private $queryStr;//查询的SQL语句
        private $limitStr;//查询语句后面的limit控制语句

        /*
        构造函数
        $queryStr 查询数据的SQL语句
        $dispNum  每页显示的数据条数
        */
        public function __construct($queryStr='',$dispNum=10){   
            $result = mysql_query($queryStr);
            $totalNum = mysql_num_rows($result);
            $this->dispNum = $dispNum;
            $this->totalPages = ceil($totalNum / $dispNum);
            $this->queryStr = $queryStr;

            $temp = (isset($_GET["curPage"]) ? $_GET["curPage"] : 1);
            $this->setCurPage($temp);

            $this->showCurPage();
            $this->showFoot();
        }       

        /*显示当前页的数据内容*/
        private function showCurPage(){
            $this->limitStr = ' LIMIT '.(($this->curPage - 1)* $this->dispNum).','.$this->dispNum;
            //echo $this->queryStr.$this->limitStr;
            $result = mysql_query($this->queryStr.$this->limitStr);

            if (!$result)
            {           
                if ($this->totalPages > 0)
                {
                    echo '查询出错'.'
';
                }
                else
                {
                    echo '无数据'.'
';
                }
                return;
            }
            $cols = mysql_num_fields($result);

            echo '

';
            echo '';
            for($i=0; $i            {
                echo '';
            }
            echo '';

            while($row = mysql_fetch_assoc($result))
            {
                echo '';
                foreach($row as $key=>$value)
                {
                    echo '';
                }
                echo '';
            }

            echo '
';
                echo mysql_field_name($result, $i);
                echo '
';
                    echo $value;
                    echo '
';
        }

        private function setCurPage($curPage){
                if($curPage                 {
                    $curPage = 1;
                }
                else if($curPage > $this->totalPages)
                {
                    $curPage = $this->totalPages;
                }
                    $this->curPage = $curPage;
        }

        /*
        显示分页页脚的信息
        如首页,上一页,下一页,尾页等信息
        */
        private function showFoot(){
            echo '首页';
            echo '上一页';
            echo '下一页';
            echo '尾页';
        }

  }

?>


方法二:

复制代码 代码如下:

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn