php专区

 首页 > php专区 > PHP应用 > php类库 > 简单的php分页类 - php类库

简单的php分页类 - php类库

分享到:
【字体:
导读:
          ?php ------------------------------------------------------------------------- 使用方法 测试时还需要一张表至少出现字段an...

简单的php分页类

  1.  
  2. //-------------------------------------------------------------------------  
  3. //使用方法  
  4.   //测试时还需要一张表  至少出现字段 announceID,announceContent  
  5.   //include db.php  
  6.  $db_host='localhost';  
  7.  $db_user='username';  
  8.  $db_password='password';  
  9.  $db_name='konwu';  
  10.  $db_prefix = 'konwu_';  
  11.   Global $db_prefix;  
  12.   $conn=@mysql_connect($db_host,$db_user,$db_password);  
  13.   // utf-8 是默认设置 即使没有下面的一行也会默认 UTF-8  
  14.   // 但是MYSQL数据库的默认是LATIN 拉丁字符  
  15.   // 最好设置成 GBK UTF-8包含GBK 包含 LATIN  
  16.   //网页用UTF-8 显示 GBK 没关系 但如果 MYSQL写入时用UTF8有乱码风险,因为MYSQL安装时默认LATIN ,最好 SET GBK  
  17.   mysql_query("SET NAMES 'UTF8'");  
  18.   mysql_select_db($db_name,$connor die ('none DB');  
  19.   //set page  
  20.       //@param set  
  21.    $errorURL = 'http://localhost:8080/htmls/error/index.html';  
  22.    $result   = mysql_query("SELECT * FROM konwu_messageannounce");  
  23.    $touNUM   = mysql_num_rows($result);  
  24.    $config   = array('DB_PREFIX'=>'konwu_','DB'=>'messageannounce','PER_NUM'=>10,'TOU_NUM'=>$touNUM,'ERROR_URL'=>$errorURL,'NUM_PAGE'=>'p');  
  25.    // use class  
  26.    $pages    = new Pages;  
  27.    $getPage  = $pages->resultNUM($config);//  
  28. //------------------------------------   
  29.    
  30.   class Pages{  
  31.     public static $perNUM;//每一页多少条  
  32.     public static $touNUM;//总共多少条  
  33.     public static $pageNUM;//总页数  
  34.     public static $tables;//表名  
  35.     public static $errorURL;// index URL  
  36.     public static $numPage;//页面参数的标识 ,   
  37.     function getInt($config=array()){     
  38.       $this->tables    = $config['DB_PREFIX'].$config['DB'] ? $config['DB_PREFIX'].$config['DB'] : 'konwu_messageannounce';  
  39.       $this->perNUM    = $config['PER_NUM'] ? $config['PER_NUM'] : 10;  
  40.       $this->touNUM    = $config['TOU_NUM'] ? $config['TOU_NUM'] : 50;  
  41.       $this->pageNUM   = ceil($this->touNUM/$this->perNUM);  
  42.       $this->errorURL  = $config['ERROR_URL'] ? $config['ERROR_URL'] : 'http://www.konwu.com';  
  43.       $this->numPage   = $config['NUM_PAGE'] ? $config['NUM_PAGE'] : 'page';  
  44.     }  
  45.      
  46.     function getNUM(){//get num 得到当前页面的值    
  47.       if(isset($_GET[$this->numPage]) && !emptyempty($_GET[$this->numPage])){  
  48.         $str = $_GET[$this->numPage];  
  49.       }else{  
  50.         $str = 'wrong';  
  51.       }  
  52.       return $str;  
  53.     }  
  54.      
  55.     function getError(){  
  56.       //return $str;     
  57.       header('location: '.$this->errorURL);  
  58.     }  
  59.      
  60.     //$_GET['pageNUM'] 第几页,必须要是数字整数,必须要 >0 ,必须 <=$pageNUM  
  61.     function isNUM(){//check num   
  62.      $str = self::getNUM();      
  63.      if($str != 'wrong'){  
  64.        if(ctype_digit($str)){  
  65.          $numPage = $str;  
  66.        }else{  
  67.          self::getError();  
  68.        }  
  69.      }else{  
  70.        self::getError();  
  71.      }  
  72.      return $numPage;  
  73.     }    
  74.     function getSelect(){    
  75.       $sql = 'SELECT * FROM '.$this->tables.' ORDER BY announceID DESC';  
  76.       return $sql;  
  77.     }  
  78.      
  79.     function getSelectResult(){    
  80.      $sql = self::getSelect();  
  81.      $result = mysql_query($sql);  
  82.      return $result;  
  83.     }  
  84.      
  85.     function getStartNUM(){   
  86.       $nowPage = self::isNUM();  
  87.       $perNUM = $this->perNUM;  
  88.       $startNUM = $nowPage-1;  
  89.       $startNUM = $startNUM*$perNUM;  
  90.       $startNUM = $startNUM+1;  
  91.       return $startNUM;  
  92.     }  
  93.      
  94.     function getLastNUM(){   
  95.       $nowPage = self::isNUM();  
  96.       $perNUM = $this->perNUM;  
  97.       $lastNUM = $nowPage*$perNUM;  
  98.       return $lastNUM;  
  99.     }  
  100.      
  101.     function resultNUM($config){     
  102.       self::getInt($config);  
  103.       $result = self::getSelectResult();       
  104.       $startNUM = self::getStartNUM();  
  105.       $lastNUM = self::getLastNUM();  
  106.       $mynum = 1;  
  107.       $allResult = '';  
  108.       while($re=mysql_fetch_array($result)){    
  109.         if($mynum>=$startNUM && $mynum<=$lastNUM){  
  110.           $announceID   = $re['announceID'];  
  111.           $singleResult = $re['announceContent'];  
  112.           $singleResult = mb_substr($singleResult,0,50,'utf-8');  
  113.           $lastPage     = self::lastPage();  
  114.           $nextPage     = self::nextPage();  
  115.           $beginPage    = self::beginPage();  
  116.           $endPage      = self::endPage();  
  117.           $getFivePage  = self::getFivePage();  
  118.           $singleResult = '  
  119.                             

    '.$singleResult.'…  "moreInfoCon'.$announceID.'.html">详细

     
  120.                           ';  
  121.           $allResult = $allResult.$singleResult;                           
  122.         }  
  123.    //--------------------------------------------------------------      
  124.         $mynum++;  
  125.         //----------------------------  
  126.         if($mynum>$lastNUM){  
  127.           $resultPage = 'class="pagination">  
  128.       '.$beginPage.' 
  129.       '.$lastPage.' 
  130.       '.$getFivePage.' 
  131.       '.$nextPage.' 
  132.       '.$endPage.' 
  133.       
';  
  •           $allResult = $allResult.$resultPage;  
  •           return $allResult;  
  •       }  
  •       //-----------------------------   
  •       }  
  •       $resultPage = 'class="pagination">  
  •       '.$beginPage.' 
  •       '.$lastPage.' 
  •       '.$getFivePage.' 
  •       '.$nextPage.' 
  •       '.$endPage.' 
  •       
  • ';  
  •       $allResult  = $allResult.'

    '
    .$resultPage;  
  •       return $allResult;  
  •     }  
  •      
  •     //next page下一页  
  •     function nextPage(){  
  •      $nowPage = self::isNUM();  
  •      $pageNUM = $this->pageNUM;   
  •      if($nowPage>=$pageNUM){  
  •        $nextPage = '';  
  •      }else{  
  •        $nextPage = $nowPage+1;  
  •        $nextPage = '.$this->numPage.'='.$nextPage.'" title="Next Page">下一页 »';  
  •      }     
  •      return $nextPage;//得到参数值  
  •     }  
  •      
  •     //lastPage 上一页  
  •     function lastPage(){  
  •       $nowPage = self::isNUM();  
  •       if($nowPage>=2){  
  •         $lastPage = $nowPage-1;  
  •         $lastPage = '.$this->numPage.'='.$lastPage.'" title="Previous Page">« 上一页';  
  •       }else{  
  •         $lastPage = '';  
  •       }      
  •       return $lastPage;  
  •     }  
  •      
  •     //第一页,最后一页,当前页左右各两页共五页  
  •     function endPage(){  
  •       $pageNUM = $this->pageNUM;  
  •       $endPage = '.$this->numPage.'='.$pageNUM.'" title="Last Page">最后一页 »';  
  •       return $endPage;  
  •     }  
  •      
  •     function beginPage(){  
  •       $beginPage = '.$this->numPage.'=1" title="First Page">« 第一页';  
  •       return $beginPage;  
  •     }  
  •      
  •     function getFivePage(){  
  •       $nowPage = self::isNUM();//当前页面  
  •       $pageNUM = $this->pageNUM;//总页数  
  •       if($pageNUM<=5){  
  •         $NUM = 1;  
  •         $getNUM = '';  
  •         while($pageNUM>=$NUM){  
  •           $nums = '.$this->numPage.'='.$NUM.'" class="number" title="'.$NUM.'">'.$NUM.'';  
  •           $getNUM = $getNUM.$nums;  
  •           $NUM++;  
  •         }  
  •         $getFivePage = $getNUM;  
  •         return $getFivePage;           
  •       }else{//>5  
  •         if($nowPage == 1){  
  •           $getNUM = '  
  •                      "moreInfo.html?'.$this->numPage.'=1" class="number current" title="1">1  
  •                      "moreInfo.html?'.$this->numPage.'=2" class="number" title="2">2  
  •                      "moreInfo.html?'.$this->numPage.'=3" class="number" title="3">3  
  •                      "moreInfo.html?'.$this->numPage.'=4" class="number" title="4">4  
  •                      "moreInfo.html?'.$this->numPage.'=5" class="number" title="5">5  
  •                     ';  
  •         }elseif($nowPage == 2){  
  •           $getNUM = '  
  •                      "moreInfo.html?'.$this->numPage.'=1" class="number" title="1">1  
  •                      "moreInfo.html?'.$this->numPage.'=2" class="number current" title="2">2  
  •                      "moreInfo.html?'.$this->numPage.'=3" class="number" title="3">3  
  •                      "moreInfo.html?'.$this->numPage.'=4" class="number" title="4">4  
  •                      "moreInfo.html?'.$this->numPage.'=5" class="number" title="5">5  
  •                     ';  
  •         }elseif($nowPage == ($pageNUM-1)){//-2位置  
  •           $getNUM = '  
  •                      "moreInfo.html?'.$this->numPage.'='.($pageNUM-4).'" class="number" title="'.($pageNUM-4).'">'.($pageNUM-4).'  
  •                      "moreInfo.html?'.$this->numPage.'='.($pageNUM-3).'" class="number" title="'.($pageNUM-3).'">'.($pageNUM-3).'  
  •                      "moreInfo.html?'.$this->numPage.'='.($pageNUM-2).'" class="number" title="'.($pageNUM-2).'">'.($pageNUM-2).'  
  •                      "moreInfo.html?'.$this->numPage.'='.($pageNUM-1).'" class="number current" title="'.($pageNUM-1).'">'.($pageNUM-1).'  
  •                      "moreInfo.html?'.$this->numPage.'='.$pageNUM.'" class="number" title="'.$pageNUM.'">'.$pageNUM.'  
  •                     ';  
  •         }elseif($nowPage == $pageNUM){//-1位置  
  •           $getNUM = '  
  •                      "moreInfo.html?'.$this->numPage.'='.($nowPage-4).'" class="number" title="'.($nowPage-4).'">'.($nowPage-4).'  
  •                      "moreInfo.html?'.$this->numPage.'='.($nowPage-3).'" class="number" title="'.($nowPage-3).'">'.($nowPage-3).'  
  •                      "moreInfo.html?'.$this->numPage.'='.($nowPage-2).'" class="number" title="'.($nowPage-2).'">'.($nowPage-2).'  
  •                      "moreInfo.html?'.$this->numPage.'='.($nowPage-1).'" class="number" title="'.($nowPage-1).'">'.($nowPage-1).'  
  •                      "moreInfo.html?'.$this->numPage.'='.$nowPage.'" class="number current" title="'.$nowPage.'">'.$nowPage.'  
  •                     ';  
  •         }elseif(2<$nowPage && $nowPage<($pageNUM-1)){//2位置和-2位置之间  
  •           $getNUM = '  
  •                      "moreInfo.html?'.$this->numPage.'='.($nowPage-2).'" class="number" title="'.($nowPage-2).'">'.($nowPage-2).'  
  •                      "moreInfo.html?'.$this->numPage.'='.($nowPage-1).'" class="number" title="'.($nowPage-1).'">'.($nowPage-1).'  
  •                      "moreInfo.html?'.$this->numPage.'='.$nowPage.'" class="number current" title="'.$nowPage.'">'.$nowPage.'  
  •                      "moreInfo.html?'.$this->numPage.'='.($nowPage+1).'" class="number" title="'.($nowPage+1).'">'.($nowPage+1).'  
  •                      "moreInfo.html?'.$this->numPage.'='.($nowPage+2).'" class="number" title="'.($nowPage+2).'">'.($nowPage+2).'  
  •                     ';  
  •         }else{  
  •           self::getError();  
  •         }  
  •          
  •       }  
  •       $getFivePage = $getNUM;  
  •       return $getFivePage;  
  •     }  
  •      
  • //----------------------------------     
  •   }  
  • ?> 
  •  

    分享到:
    分享的一个分页类 - php类库
    分享的一个分页类  
    一个功能比较高的分页类(for PHP5.x) - p...
    一个功能比较高的分页类(for PHP5.x) 怕水平的不高,所以从来没有放过任何代码,这个类我已经用了很久,近来用面向对象方法重写,适用于PHP5.x,特地扔出,不怕见笑,希望抛砖引玉。 这个类适用于配合数据库查询分页,和数组分页,下面有使用方法。    /*    * 名称: ...
    •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
    • 在这里……