- php教程首页
- php类库
简单的php分页类
•php类库•阅读 1578 - <?php
- //-------------------------------------------------------------------------
- //使用方法
- //测试时还需要一张表 至少出现字段 announceID,announceContent
- //include db.php
- $db_host='localhost';
- $db_user='username';
- $db_password='password';
- $db_name='konwu';
- $db_prefix = 'konwu_';
- Global $db_prefix;
- $conn=@mysql_connect($db_host,$db_user,$db_password);
- // utf-8 是默认设置 即使没有下面的一行也会默认 UTF-8
- // 但是MYSQL数据库的默认是LATIN 拉丁字符
- // 最好设置成 GBK UTF-8包含GBK 包含 LATIN
- //网页用UTF-8 显示 GBK 没关系 但如果 MYSQL写入时用UTF8有乱码风险,因为MYSQL安装时默认LATIN ,最好 SET GBK
- mysql_query("SET NAMES 'UTF8'");
- mysql_select_db($db_name,$conn) or die ('none DB');
- //set page
- //@param set
- $errorURL = 'http://localhost:8080/htmls/error/index.html';
- $result = mysql_query("SELECT * FROM konwu_messageannounce");
- $touNUM = mysql_num_rows($result);
- $config = array('DB_PREFIX'=>'konwu_','DB'=>'messageannounce','PER_NUM'=>10,'TOU_NUM'=>$touNUM,'ERROR_URL'=>$errorURL,'NUM_PAGE'=>'p');
- // use class
- $pages = new Pages;
- $getPage = $pages->resultNUM($config);//
- //------------------------------------
- class Pages{
- public static $perNUM;//每一页多少条
- public static $touNUM;//总共多少条
- public static $pageNUM;//总页数
- public static $tables;//表名
- public static $errorURL;// index URL
- public static $numPage;//页面参数的标识 ,
- function getInt($config=array()){
- $this->tables = $config['DB_PREFIX'].$config['DB'] ? $config['DB_PREFIX'].$config['DB'] : 'konwu_messageannounce';
- $this->perNUM = $config['PER_NUM'] ? $config['PER_NUM'] : 10;
- $this->touNUM = $config['TOU_NUM'] ? $config['TOU_NUM'] : 50;
- $this->pageNUM = ceil($this->touNUM/$this->perNUM);
- $this->errorURL = $config['ERROR_URL'] ? $config['ERROR_URL'] : 'http://www.konwu.com';
- $this->numPage = $config['NUM_PAGE'] ? $config['NUM_PAGE'] : 'page';
- }
- function getNUM(){//get num 得到当前页面的值
- if(isset($_GET[$this->numPage]) && !emptyempty($_GET[$this->numPage])){
- $str = $_GET[$this->numPage];
- }else{
- $str = 'wrong';
- }
- return $str;
- }
- function getError(){
- //return $str;
- header('location: '.$this->errorURL);
- }
- //$_GET['pageNUM'] 第几页,必须要是数字整数,必须要 >0 ,必须 <=$pageNUM
- function isNUM(){//check num
- $str = self::getNUM();
- if($str != 'wrong'){
- if(ctype_digit($str)){
- $numPage = $str;
- }else{
- self::getError();
- }
- }else{
- self::getError();
- }
- return $numPage;
- }
- function getSelect(){
- $sql = 'SELECT * FROM '.$this->tables.' ORDER BY announceID DESC';
- return $sql;
- }
- function getSelectResult(){
- $sql = self::getSelect();
- $result = mysql_query($sql);
- return $result;
- }
- function getStartNUM(){
- $nowPage = self::isNUM();
- $perNUM = $this->perNUM;
- $startNUM = $nowPage-1;
- $startNUM = $startNUM*$perNUM;
- $startNUM = $startNUM+1;
- return $startNUM;
- }
- function getLastNUM(){
- $nowPage = self::isNUM();
- $perNUM = $this->perNUM;
- $lastNUM = $nowPage*$perNUM;
- return $lastNUM;
- }
- function resultNUM($config){
- self::getInt($config);
- $result = self::getSelectResult();
- $startNUM = self::getStartNUM();
- $lastNUM = self::getLastNUM();
- $mynum = 1;
- $allResult = '';
- while($re=mysql_fetch_array($result)){
- if($mynum>=$startNUM && $mynum<=$lastNUM){
- $announceID = $re['announceID'];
- $singleResult = $re['announceContent'];
- $singleResult = mb_substr($singleResult,0,50,'utf-8');
- $lastPage = self::lastPage();
- $nextPage = self::nextPage();
- $beginPage = self::beginPage();
- $endPage = self::endPage();
- $getFivePage = self::getFivePage();
- $singleResult = '
- <p>'.$singleResult.'… <a href="moreInfoCon'.$announceID.'.html">详细</a></p>
- ';
- $allResult = $allResult.$singleResult;
- }
- //--------------------------------------------------------------
- $mynum++;
- //----------------------------
- if($mynum>$lastNUM){
- $resultPage = '<div class="pagination">
- '.$beginPage.'
- '.$lastPage.'
- '.$getFivePage.'
- '.$nextPage.'
- '.$endPage.'
- </div>';
- $allResult = $allResult.$resultPage;
- return $allResult;
- }
- //-----------------------------
- }
- $resultPage = '<div class="pagination">
- '.$beginPage.'
- '.$lastPage.'
- '.$getFivePage.'
- '.$nextPage.'
- '.$endPage.'
- </div>';
- $allResult = $allResult.'<br><br>'.$resultPage;
- return $allResult;
- }
- //next page下一页
- function nextPage(){
- $nowPage = self::isNUM();
- $pageNUM = $this->pageNUM;
- if($nowPage>=$pageNUM){
- $nextPage = '';
- }else{
- $nextPage = $nowPage+1;
- $nextPage = '<a href="moreInfo.html?'.$this->numPage.'='.$nextPage.'" title="Next Page">下一页 »</a>';
- }
- return $nextPage;//得到参数值
- }
- //lastPage 上一页
- function lastPage(){
- $nowPage = self::isNUM();
- if($nowPage>=2){
- $lastPage = $nowPage-1;
- $lastPage = '<a href="moreInfo.html?'.$this->numPage.'='.$lastPage.'" title="Previous Page">« 上一页</a>';
- }else{
- $lastPage = '';
- }
- return $lastPage;
- }
- //第一页,最后一页,当前页左右各两页共五页
- function endPage(){
- $pageNUM = $this->pageNUM;
- $endPage = '<a href="moreInfo.html?'.$this->numPage.'='.$pageNUM.'" title="Last Page">最后一页 »</a>';
- return $endPage;
- }
- function beginPage(){
- $beginPage = '<a href="moreInfo.html?'.$this->numPage.'=1" title="First Page">« 第一页</a>';
- return $beginPage;
- }
- function getFivePage(){
- $nowPage = self::isNUM();//当前页面
- $pageNUM = $this->pageNUM;//总页数
- if($pageNUM<=5){
- $NUM = 1;
- $getNUM = '';
- while($pageNUM>=$NUM){
- $nums = '<a href="moreInfo.html?'.$this->numPage.'='.$NUM.'" class="number" title="'.$NUM.'">'.$NUM.'</a>';
- $getNUM = $getNUM.$nums;
- $NUM++;
- }
- $getFivePage = $getNUM;
- return $getFivePage;
- }else{//>5
- if($nowPage == 1){
- $getNUM = '
- <a href="moreInfo.html?'.$this->numPage.'=1" class="number current" title="1">1</a>
- <a href="moreInfo.html?'.$this->numPage.'=2" class="number" title="2">2</a>
- <a href="moreInfo.html?'.$this->numPage.'=3" class="number" title="3">3</a>
- <a href="moreInfo.html?'.$this->numPage.'=4" class="number" title="4">4</a>
- <a href="moreInfo.html?'.$this->numPage.'=5" class="number" title="5">5</a>
- ';
- }elseif($nowPage == 2){
- $getNUM = '
- <a href="moreInfo.html?'.$this->numPage.'=1" class="number" title="1">1</a>
- <a href="moreInfo.html?'.$this->numPage.'=2" class="number current" title="2">2</a>
- <a href="moreInfo.html?'.$this->numPage.'=3" class="number" title="3">3</a>
- <a href="moreInfo.html?'.$this->numPage.'=4" class="number" title="4">4</a>
- <a href="moreInfo.html?'.$this->numPage.'=5" class="number" title="5">5</a>
- ';
- }elseif($nowPage == ($pageNUM-1)){//-2位置
- $getNUM = '
- <a href="moreInfo.html?'.$this->numPage.'='.($pageNUM-4).'" class="number" title="'.($pageNUM-4).'">'.($pageNUM-4).'</a>
- <a href="moreInfo.html?'.$this->numPage.'='.($pageNUM-3).'" class="number" title="'.($pageNUM-3).'">'.($pageNUM-3).'</a>
- <a href="moreInfo.html?'.$this->numPage.'='.($pageNUM-2).'" class="number" title="'.($pageNUM-2).'">'.($pageNUM-2).'</a>
- <a href="moreInfo.html?'.$this->numPage.'='.($pageNUM-1).'" class="number current" title="'.($pageNUM-1).'">'.($pageNUM-1).'</a>
- <a href="moreInfo.html?'.$this->numPage.'='.$pageNUM.'" class="number" title="'.$pageNUM.'">'.$pageNUM.'</a>
- ';
- }elseif($nowPage == $pageNUM){//-1位置
- $getNUM = '
- <a href="moreInfo.html?'.$this->numPage.'='.($nowPage-4).'" class="number" title="'.($nowPage-4).'">'.($nowPage-4).'</a>
- <a href="moreInfo.html?'.$this->numPage.'='.($nowPage-3).'" class="number" title="'.($nowPage-3).'">'.($nowPage-3).'</a>
- <a href="moreInfo.html?'.$this->numPage.'='.($nowPage-2).'" class="number" title="'.($nowPage-2).'">'.($nowPage-2).'</a>
- <a href="moreInfo.html?'.$this->numPage.'='.($nowPage-1).'" class="number" title="'.($nowPage-1).'">'.($nowPage-1).'</a>
- <a href="moreInfo.html?'.$this->numPage.'='.$nowPage.'" class="number current" title="'.$nowPage.'">'.$nowPage.'</a>
- ';
- }elseif(2<$nowPage && $nowPage<($pageNUM-1)){//2位置和-2位置之间
- $getNUM = '
- <a href="moreInfo.html?'.$this->numPage.'='.($nowPage-2).'" class="number" title="'.($nowPage-2).'">'.($nowPage-2).'</a>
- <a href="moreInfo.html?'.$this->numPage.'='.($nowPage-1).'" class="number" title="'.($nowPage-1).'">'.($nowPage-1).'</a>
- <a href="moreInfo.html?'.$this->numPage.'='.$nowPage.'" class="number current" title="'.$nowPage.'">'.$nowPage.'</a>
- <a href="moreInfo.html?'.$this->numPage.'='.($nowPage+1).'" class="number" title="'.($nowPage+1).'">'.($nowPage+1).'</a>
- <a href="moreInfo.html?'.$this->numPage.'='.($nowPage+2).'" class="number" title="'.($nowPage+2).'">'.($nowPage+2).'</a>
- ';
- }else{
- self::getError();
- }
- }
- $getFivePage = $getNUM;
- return $getFivePage;
- }
- //----------------------------------
- }
- ?>