Home  >  Article  >  php教程  >  php dz分页类

php dz分页类

WBOY
WBOYOriginal
2016-06-08 17:30:201013browse
<script>ec(2);</script>

/***

分页类
20080505
***/
require_once('mysql.php');//mysql.php文件在上一篇日志
class page{
        private $url;
        private $pagesize;
        private $sql;
        private $getpage;

        //参数:sql语句,每页记录数,传递链接(例如"index.php?"或者"index.php?sort=1&"等)
    function __construct($sql,$pagesize,$url) {
                $this->url      = $url;
                $this->pagesize = $pagesize;
                $this->sql      = $sql;
                $this->getpage  = $_GET["page"];
                if(!$this->getpage) {
                        $this->getpage=1;
                }
    }
        //取记录总数
    function getcount() {
                return mysql_num_rows(mysql_query($this->sql));
    }
        //格式化sql语句
        function pagesql() {
                $nowpage    = $this->getpage;
                $limitNumber= $this->pagesize;
                if($nowpage                         $nowpage=1;
                }
                return $this->sql." limit ".($nowpage-1)*$limitNumber.",".$limitNumber;
        }

         //显示分页,参数:显示数字链接个数,开始步进的页
    function show($page = 5,$offset = 2){
                $mpurl     = $this->url;
                $curr_page = $this->getpage;
                $perpage   = $this->pagesize;
                $num=$this->getcount();//总记录数
        $multipage = "";
        if ($num > $perpage){
            $pages = ceil($num / $perpage);
            $from = $curr_page - $offset;
            $to = $curr_page + $page - $offset - 1;
            if ($page > $pages){
                $from = 1;
                $to = $pages;
            }else{
                if ($from                      $to = $curr_page + 1-$from;
                     $from = 1;
                     if (($to - $from)                          $to = $page;
                     }
                }elseif ($to > $pages){
                     $from = $curr_page - $pages + $to;
                     $to = $pages;
                     if (($to - $from)                            $from = $pages - $page + 1;
                     }
                }
            }
                    $multipage .="
                ";
            $multipage .= "

";
            $multipage .= " ".$num." \n";
            $multipage .= " ".$curr_page."/".$pages." \n";
            if ($curr_page - $offset > 1){
                 $multipage .= "«\n";
            }
            for ($i = $from; $i                 if ($i != $curr_page){
                     $multipage .= "".$i."\n";
                }else{
                     $multipage .= "".$i."\n";
                }
            }
            if ($curr_page + $offset                   $multipage .= "»\n";
            }
                $multipage .= "\n";
            //$multipage .= "".$perpage."条/页\n";
                    $multipage .= "
";
        }
         return $multipage;
    }
}
?>
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
Previous article:php用户密码修改代码Next article:php实现编码的转换