国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

二個php分頁程序代碼

2024-05-04 23:06:48
字體:
來源:轉載
供稿:網友

PHP分頁器制作,自動生成分面頁碼,JS調用函數,分頁的原理大致如下,分頁程序有兩個非常重要的參數:每頁顯示幾條記錄($pagesize)和當前是第幾頁($page).

有了這兩個參數就可以很方便的寫出分頁程序,我們以MySql數據庫作為數據源,在mysql里如果要想取出表內某段特定內容可以使用的 T-SQL語句:select * from table limit offset,rows來實現。這里的offset是記錄偏移量,它的計算方法是offset=$pagesize*($page-1),rows是要顯示的記錄條數,這里就是$page,也就是說select * from table limit 10,10這條語句的意思是取出表里從第11條記錄開始的20條記錄.

PHP實例代碼如下:

  1. <?php 
  2. class PageView{ 
  3.     /**頁碼**/ 
  4.     public $pageNo = 1; 
  5.     /**頁大小**/ 
  6.     public $pageSize = 20; 
  7.     /**共多少頁**/ 
  8.     public $pageCount = 0; 
  9.     /**總記錄數**/ 
  10.     public $totalNum = 0; 
  11.     /**偏移量,當前頁起始行**/ 
  12.     public $offSet = 0; 
  13.     /**每頁數據**/ 
  14.     public $pageData = array(); 
  15.      
  16.     /**是否有上一頁**/ 
  17.     public $hasPrePage = true; 
  18.     /**是否有下一頁**/ 
  19.     public $hasNextPage = true; 
  20.      
  21.     public $pageNoList = array(); 
  22.      
  23.     public $jsFunction ='jsFunction'
  24.     /** 
  25.      *  
  26.      * @param unknown_type $count 總行數 
  27.      * @param unknown_type $size 分頁大小 
  28.      * @param unknown_type $string 
  29.      */ 
  30.     public function __construct($count=0, $size=20,$pageNo=1,$pageData =array(),$jsFunction='jsFunction'){ 
  31.  
  32.         $this->totalNum = $count;//總記錄數 
  33.         $this->pageSize = $size;//每頁大小 
  34.         $this->pageNo = $pageNo
  35.         //計算總頁數 
  36.         $this->pageCount = ceil($this->totalNum/$this->pageSize); 
  37.         $this->pageCount = ($this->pageCount<=0)?1:$this->pageCount; 
  38.         //檢查pageNo 
  39.         $this->pageNo = $this->pageNo == 0 ? 1 : $this->pageNo; 
  40.         $this->pageNo = $this->pageNo > $this->pageCount? $this->pageCount : $this->pageNo; 
  41.          
  42.         //計算偏移 
  43.         $this->offset = ( $this->pageNo - 1 ) * $this->pageSize; 
  44.         //計算是否有上一頁下一頁 
  45.         $this->hasPrePage = $this->pageNo == 1 ?false:true; 
  46.  
  47.         $this->hasNextPage = $this->pageNo >= $this->pageCount ?false:true; 
  48.          
  49.         $this->pageData = $pageData
  50.         $this->jsFunction = $jsFunction
  51.          
  52.     } 
  53.     /** 
  54.      * 分頁算法 
  55.      * @return 
  56.      */ 
  57.     private function generatePageList(){ 
  58.         $pageList = array(); 
  59.         if($this->pageCount <= 9){ 
  60.             for($i=0;$i<$this->pageCount;$i++){ 
  61.                 array_push($pageList,$i+1); 
  62.             } 
  63.         }else
  64.             if($this->pageNo <= 4){ 
  65.                 for($i=0;$i<5;$i++){ 
  66.                     array_push($pageList,$i+1); 
  67.                 } 
  68.                 array_push($pageList,-1); 
  69.                 array_push($pageList,$this->pageCount); 
  70.  
  71.             }else if($this->pageNo > $this->pageCount - 4){ 
  72.                 array_push($pageList,1); 
  73.                  
  74.                 array_push($pageList,-1); 
  75.                 for($i=5;$i>0;$i--){ 
  76.                     array_push($pageList,$this->pageCount - $i+1); 
  77.                 } 
  78.             }else if($this->pageNo > 4 && $this->pageNo <= $this->pageCount - 4){ 
  79.                 array_push($pageList,1); 
  80.                 array_push($pageList,-1); 
  81.                  
  82.                 array_push($pageList,$this->pageNo -2); 
  83.                 array_push($pageList,$this->pageNo -1); 
  84.                 array_push($pageList,$this->pageNo); 
  85.                 array_push($pageList,$this->pageNo + 1); 
  86.                 array_push($pageList,$this->pageNo + 2); 
  87.                  
  88.                 array_push($pageList,-1); 
  89.                 array_push($pageList,$this->pageCount); 
  90.                  
  91.             } 
  92.         } 
  93.         return $pageList
  94.     } 
  95.  
  96.     /*** 
  97.      * 創建分頁控件 
  98.     * @param 
  99.     * @return String 
  100.     */ 
  101.     public function echoPageAsDiv(){ 
  102.         $pageList = $this->generatePageList(); 
  103.          
  104.         $pageString ="<div class='pagination'><div class='page-bottom'>"
  105.      
  106.         if(!emptyempty($pageList)){ 
  107.             if($this->pageCount >1){ 
  108.                 if($this->hasPrePage){ 
  109.                     $pageString = $pageString ."<a class='page-next' href="javascript:" .$this->jsFunction . "(" . ($this->pageNo-1) . ")">上一頁</a>"
  110.                 } 
  111.                 foreach ($pageList as $k=>$p){ 
  112.                     if($this->pageNo == $p){ 
  113.                         $pageString = $pageString ."<span class='page-cur'>" . $this->pageNo . "</span>"
  114.                         continue
  115.                     } 
  116.                     if($p == -1){ 
  117.                         $pageString = $pageString ."<span class='page-break'>...</span>"
  118.                         continue
  119.                     } 
  120.                     $pageString = $pageString ."<a href="javascript:" .$this->jsFunction . "(" . $p . ")">" . $p . "</a>"
  121.                 } 
  122.                  
  123.                 if($this->hasNextPage){ 
  124.                     $pageString = $pageString ."<a class='page-next' href="javascript:" .$this->jsFunction . "(" . ($this->pageNo+1) . ")">下一頁</a>"
  125.                 } 
  126.                  
  127.             } 
  128.         } 
  129.         $pageString = $pageString .("</div></div>"); 
  130.         return $pageString
  131.     } 
  132. ?> 

css,代碼如下:

  1. <style type="text/css"
  2. <!-- 
  3. .pagination {font-familyTahoma;overflowhiddenpadding-top12pxtext-aligncenter;} 
  4. .pagination-tab { margin-bottom20px;} 
  5. .pagination a, .pagination .page-cur, .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g, .pagination .page-break, .pagination .page-skip { 
  6.     display: inline-block;font-familyTahoma,SimSun,Arialheight22px;line-height:22pxmargin0min-width16px;padding0 5pxtext-aligncentervertical-aligntopwhite-spacenowrap;} 
  7. .pagination a, .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g, .pagination .page-cur, .pagination .page-break { 
  8.     border1px solid #ed3d83color:#e9357dfont-weight:bold;} 
  9. .pagination a:hover { border1px solid #ed3d83;text-decorationnonebackground-color:#f95f9dcolor:#fff;} 
  10. .pagination .page-prev_g, .pagination .page-prev, .pagination .page-next, .pagination .page-next_g { width36pxbackground-imageurl(../static/img/page.gif);} 
  11. .pagination .page-prev { background-position-0px -38pxpadding-left16px;} 
  12. .pagination .page-prev_g { background-position:0px -59pxpadding-left16pxcolor:#cbcbcbfont-weight:normal;} 
  13. .pagination .page-next { background-position0px 0pxpadding-right16pxfont-weight:normal;} 
  14. .pagination .page-next_g { background-position-0px -19pxpadding-right16pxcolor:#cbcbcb;} 
  15. .pagination .page-cur {background-color#f95f9dborder1px solid #ed3d83;color#fff;font-weightbold;} 
  16. .pagination .page-break {bordermedium nonebackground:none transparentcolor:#333;} 
  17.  
  18. --> 
  19. </style> 

使用方法,代碼如下:

  1. $pageNo = $_GET['pageNo']; 
  2.         if(emptyempty($pageNo)){ 
  3.             $pageNo = 1; 
  4.         } 
  5.         //分頁數據 
  6.         $pageData = News::getNewsPage($pageNo,$pageSize); 
  7.        //取得總行數 
  8.         $count = News::getNewsCount(); 
  9.         //創建分頁器 
  10.         $p = new PageView($count['0']['TOTAL'],$pageSize,$pageNo,$pageData); 
  11.      //生成頁碼 
  12. $pageViewString = $p->echoPageAsDiv(); 

下面再介紹一個分頁類,代碼如下:

  1. <?php    
  2. class SubPages{    
  3.        
  4.    private  $each_disNums;//每頁顯示的條目數    
  5.   private  $nums;//總條目數    
  6.   private  $current_page;//當前被選中的頁    
  7.   private  $sub_pages;//每次顯示的頁數    
  8.   private  $pageNums;//總頁數    
  9.   private  $page_array = array();//用來構造分頁的數組    
  10.   private  $subPage_link;//每個分頁的鏈接    
  11.   private  $subPage_type;//顯示分頁的類型    
  12.    /*   
  13.    __construct是SubPages的構造函數,用來在創建類的時候自動運行.   
  14.    @$each_disNums   每頁顯示的條目數   
  15.    @nums     總條目數   
  16.    @current_num     當前被選中的頁   
  17.    @sub_pages       每次顯示的頁數   
  18.    @subPage_link    每個分頁的鏈接   
  19.    @subPage_type    顯示分頁的類型   
  20.       
  21.    當@subPage_type=1的時候為普通分頁模式   
  22.          example:   共4523條記錄,每頁顯示10條,當前第1/453頁 [首頁] [上頁] [下頁] [尾頁]   
  23.          當@subPage_type=2的時候為經典分頁樣式   
  24.          example:   當前第1/453頁 [首頁] [上頁] 1 2 3 4 5 6 7 8 9 10 [下頁] [尾頁]   
  25.    */   
  26.   function __construct($each_disNums,$nums,$current_page,$sub_pages,$subPage_link,$subPage_type){    
  27.    $this->each_disNums=intval($each_disNums);    
  28.    $this->nums=intval($nums);    
  29.     if(!$current_page){    
  30.     $this->current_page=1;    
  31.     }else{    
  32.     $this->current_page=intval($current_page);    
  33.     }    
  34.    $this->sub_pages=intval($sub_pages);    
  35.    $this->pageNums=ceil($nums/$each_disNums);    
  36.    $this->subPage_link=$subPage_link;     
  37.    $this->show_SubPages($subPage_type);     
  38.    //echo $this->pageNums."--".$this->sub_pages;    
  39.   }    
  40.        
  41.        
  42.   /*   
  43.     __destruct析構函數,當類不在使用的時候調用,該函數用來釋放資源。   
  44.    */   
  45.   function __destruct(){    
  46.     unset($each_disNums);    
  47.     unset($nums);    
  48.     unset($current_page);    
  49.     unset($sub_pages);    
  50.     unset($pageNums);    
  51.     unset($page_array);    
  52.     unset($subPage_link);    
  53.     unset($subPage_type);    
  54.    }    
  55.        
  56.   /*   
  57.     show_SubPages函數用在構造函數里面。而且用來判斷顯示什么樣子的分頁     
  58.    */   
  59.   function show_SubPages($subPage_type){    
  60.     if($subPage_type == 1){    
  61.     $this->subPageCss1();    
  62.     }elseif ($subPage_type == 2){    
  63.     $this->subPageCss2();    
  64.     }    
  65.    }    
  66.        
  67.        
  68.   /*   
  69.     用來給建立分頁的數組初始化的函數。   
  70.    */   
  71.   function initArray(){    
  72.     for($i=0;$i<$this->sub_pages;$i++){    
  73.     $this->page_array[$i]=$i;    
  74.     }    
  75.     return $this->page_array;    
  76.    }    
  77.        
  78.        
  79.   /*   
  80.     construct_num_Page該函數使用來構造顯示的條目   
  81.     即使:[1][2][3][4][5][6][7][8][9][10]   
  82.    */   
  83.   function construct_num_Page(){    
  84.     if($this->pageNums < $this->sub_pages){    
  85.     $current_array=array();    
  86.      for($i=0;$i<$this->pageNums;$i++){     
  87.      $current_array[$i]=$i+1;    
  88.      }    
  89.     }else{    
  90.     $current_array=$this->initArray();    
  91.      if($this->current_page <= 3){    
  92.       for($i=0;$i<count($current_array);$i++){    
  93.       $current_array[$i]=$i+1;    
  94.       }    
  95.      }elseif ($this->current_page <= $this->pageNums && $this->current_page > $this->pageNums - $this->sub_pages + 1 ){    
  96.       for($i=0;$i<count($current_array);$i++){    
  97.       $current_array[$i]=($this->pageNums)-($this->sub_pages)+1+$i;    
  98.       }    
  99.      }else{    
  100.       for($i=0;$i<count($current_array);$i++){    
  101.       $current_array[$i]=$this->current_page-2+$i;    
  102.       }    
  103.      }    
  104.     }    
  105.         
  106.     return $current_array;    
  107.    }    
  108.        
  109.   /*   
  110.    構造普通模式的分頁   
  111.    共4523條記錄,每頁顯示10條,當前第1/453頁 [首頁] [上頁] [下頁] [尾頁]   
  112.    */   
  113.   function subPageCss1(){    
  114.    $subPageCss1Str="";    
  115.    $subPageCss1Str.="共".$this->nums."條記錄,";    
  116.    $subPageCss1Str.="每頁顯示".$this->each_disNums."條,";    
  117.    $subPageCss1Str.="當前第".$this->current_page."/".$this->pageNums."頁 ";   
  118.     if($this->current_page > 1){    
  119.     $firstPageUrl=$this->subPage_link."1";    
  120.     $prewPageUrl=$this->subPage_link.($this->current_page-1);    
  121.     $subPageCss1Str.="[<a href='$firstPageUrl'>首頁</a>] ";    
  122.     $subPageCss1Str.="[<a href='$prewPageUrl'>上一頁</a>] ";    
  123.     }else {    
  124.     $subPageCss1Str.="[首頁] ";    
  125.     $subPageCss1Str.="[上一頁] ";    
  126.     }    
  127.         
  128.     if($this->current_page < $this->pageNums){    
  129.     $lastPageUrl=$this->subPage_link.$this->pageNums;    
  130.     $nextPageUrl=$this->subPage_link.($this->current_page+1);    
  131.     $subPageCss1Str.=" [<a href='$nextPageUrl'>下一頁</a>] ";    
  132.     $subPageCss1Str.="[<a href='$lastPageUrl'>尾頁</a>] ";    
  133.     }else {    
  134.     $subPageCss1Str.="[下一頁] ";    
  135.     $subPageCss1Str.="[尾頁] ";    
  136.     }    
  137.         
  138.     echo $subPageCss1Str;    
  139.         
  140.    }    
  141.        
  142.        
  143.   /*   
  144.    構造經典模式的分頁   
  145.    當前第1/453頁 [首頁] [上頁] 1 2 3 4 5 6 7 8 9 10 [下頁] [尾頁]   
  146.    */   
  147.   function subPageCss2(){    
  148.    $subPageCss2Str="";    
  149.    $subPageCss2Str.="當前第".$this->current_page."/".$this->pageNums."頁 ";   
  150.         
  151.         
  152.     if($this->current_page > 1){    
  153.     $firstPageUrl=$this->subPage_link."1";    
  154.     $prewPageUrl=$this->subPage_link.($this->current_page-1);    
  155.     $subPageCss2Str.="[<a href='$firstPageUrl'>首頁</a>] ";    
  156.     $subPageCss2Str.="[<a href='$prewPageUrl'>上一頁</a>] ";    
  157.     }else {    
  158.     $subPageCss2Str.="[首頁] ";    
  159.     $subPageCss2Str.="[上一頁] ";    
  160.     }    
  161.         
  162.    $a=$this->construct_num_Page();    
  163.     for($i=0;$i<count($a);$i++){    
  164.     $s=$a[$i];    
  165.      if($s == $this->current_page ){    
  166.      $subPageCss2Str.="[<span style='color:red;font-weight:bold;'>".$s."</span>]";    
  167.      }else{    
  168.      $url=$this->subPage_link.$s;    
  169.      $subPageCss2Str.="[<a href='$url'>".$s."</a>]";    
  170.      }    
  171.     } 
  172.     if($this->current_page < $this->pageNums){    
  173.     $lastPageUrl=$this->subPage_link.$this->pageNums;    
  174.     $nextPageUrl=$this->subPage_link.($this->current_page+1);    
  175.     $subPageCss2Str.=" [<a href='$nextPageUrl'>下一頁</a>] ";    
  176.     $subPageCss2Str.="[<a href='$lastPageUrl'>尾頁</a>] ";    
  177.     }else {    
  178.     $subPageCss2Str.="[下一頁] ";    
  179.     $subPageCss2Str.="[尾頁] ";    
  180.     }    
  181.     echo $subPageCss2Str;    
  182.    }    
  183. }    
  184. ?> 

調用方法,代碼如下:

  1. <?php    
  2. require_once("SubPages.php");    
  3. //每頁顯示的條數    
  4.   $page_size=20;    
  5. //總條目數    
  6.   $nums=1024;    
  7. //每次顯示的頁數    
  8.   $sub_pages=10;    
  9. //得到當前是第幾頁    
  10.   $pageCurrent=$_GET["p"];    
  11.   //if(!$pageCurrent) $pageCurrent=1;    
  12.        
  13.   $subPages=new SubPages($page_size,$nums,$pageCurrent,$sub_pages,"test.php?p=",2);    
  14. ?> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 仁怀市| 南昌县| 措勤县| 扎兰屯市| 于都县| 长泰县| 陵川县| 红安县| 乃东县| 清涧县| 乌兰县| 丹凤县| 北票市| 华阴市| 新邵县| 定州市| 江津市| 葫芦岛市| 台南市| 平邑县| 遵义市| 合川市| 和硕县| 汨罗市| 肥西县| 富锦市| 巢湖市| 龙南县| 军事| 华坪县| 大埔区| 咸阳市| 禹州市| 兴城市| 湘阴县| 勐海县| 勐海县| 钟祥市| 云阳县| 嘉祥县| 柳河县|