简单的php分页类 - php类库
来源:自学PHP网
时间:2014-11-30 12:53 作者:
阅读:次
[导读] ?php ------------------------------------------------------------------------- 使用方法 测试时还需要一张表至少出现字段an...
简单的php分页类
- <?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);
-
-
-
-
- mysql_query("SET NAMES 'UTF8'");
- mysql_select_db($db_name,$conn) or die ('none DB');
-
-
- $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');
-
- $pages = new Pages;
- $getPage = $pages->resultNUM($config);
-
-
- class Pages{
- public static $perNUM;
- public static $touNUM;
- public static $pageNUM;
- public static $tables;
- public static $errorURL;
- 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(){
- if(isset($_GET[$this->numPage]) && !emptyempty($_GET[$this->numPage])){
- $str = $_GET[$this->numPage];
- }else{
- $str = 'wrong';
- }
- return $str;
- }
-
- function getError(){
-
- header('location: '.$this->errorURL);
- }
-
-
- function isNUM(){
- $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;
- }
-
-
- 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;
- }
-
-
- 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{
- 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)){
- $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){
- $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)){
- $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;
- }
-
-
- }
- ?>
|