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

首頁 > 開發 > PHP > 正文

PHP分頁顯示的方法分析【附PHP通用分頁類】

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

本文實例講述了PHP分頁顯示的方法。分享給大家供大家參考,具體如下:

  1. <?php 
  2. header("content-type:text/html;charset=utf-8"); 
  3. $currentpage = 1; 
  4. if(isset($_GET['page'])) 
  5.   $currentpage = $_GET['page']; 
  6. //連接數據庫 
  7. $link = mysql_connect("localhost","root",""or die('連接失敗'); 
  8. mysql_select_db('myschool'); 
  9. mysql_query('set names utf8'); 
  10. $sql ="SELECT count(*) as 'count' from student";//查詢記錄的sql語句 
  11. $result = mysql_query($sql); 
  12. $arr = mysql_fetch_array($result); 
  13. $count = $arr['count']; 
  14. $pagesize = 3; 
  15. $pages = ceil($count/$pagesize);//共多少頁 
  16. $prepage = $currentpage -1; 
  17. if($prepage<=0) 
  18.   $prepage=1; 
  19. $nextpage = $currentpage+1; 
  20. if($nextpage >= $pages){ 
  21.  $nextpage = $pages
  22. $start =($currentpage-1) * $pagesize;//起始位置 
  23. $sql = "SELECT * from student limit $start,$pagesize"
  24. echo $sql
  25. // $sql = "select * from student"; 
  26. $result = mysql_query($sql); 
  27. ?> 
  28. <!-- html部分 --> 
  29. <!DOCTYPE html> 
  30. <html lang="en"
  31. <head> 
  32.  <meta charset="UTF-8"
  33.  <title>Document</title> 
  34. </head> 
  35. <body> 
  36. <table border="1"
  37.  <tr> 
  38.  <td>學號</td> 
  39.  <td>姓名</td> 
  40.  <td>性別</td> 
  41.  <td>年齡</td> 
  42.  </tr> 
  43. <?php while($arr=mysql_fetch_array($result)){ ?> 
  44.  <td><?php echo $arr['number']; ?></td> 
  45.  <td><?php echo $arr['name']; ?></td> 
  46.  <td><?php echo $arr['sex']; ?></td> 
  47.  <td><?php echo $arr['age']; ?></td> 
  48.  </tr> //Vevb.com 
  49. <?php } ?> 
  50.  </table> 
  51.  <a href="<?php echo $_SERVER['PHP_SELF'].'?page='.$prepage; ?>" rel="external nofollow" >上一頁</a>  <a href="<?php echo $_SERVER['PHP_SELF'].'?page='.$nextpage; ?>" rel="external nofollow" >下一頁</a> 
  52. </body> 
  53. </html> 

注:當一個文件中有php和html兩種時,php文件必須有結束標記

附:php通用分頁類與用法:

Page.class.php文件:

  1. <?php 
  2. /** 
  3.  * 分頁類 
  4.  * 
  5.  * 調用方式: 
  6.  * $p=new Page(總條數,顯示頁數,當前頁碼,每頁顯示條數,[鏈接]); 
  7.  * print_r($p->getPages()); //生成一個頁碼數組(鍵為頁碼,值為鏈接) 
  8.  * echo $p->showPages(1);  //生成一個頁碼樣式(可添加自定義樣式) 
  9.  * 
  10.  */ 
  11. /* 
  12. 總條數,需要顯示的頁數,當前頁,每頁顯示的條數,連接 
  13. 生成一個一維數組,鍵為頁碼 值為連接 
  14. 返回一個生成好樣式的頁碼(并且可以根據自己需要添加樣式) 
  15. 默認樣式 共45條記錄,每頁顯示10條,當前第1/4頁 [首頁] [上頁] [1] [2] [3] .. [下頁] [尾頁] 
  16. */ 
  17. class Page{ 
  18.   protected $count;    //總條數 
  19.   protected $showPages;  //需要顯示的頁數 
  20.   protected $countPages//總頁數 
  21.   protected $currPage;  //當前頁 
  22.   protected $subPages;  //每頁顯示條數 
  23.   protected $href;    //連接 
  24.   protected $page_arr=array();  //保存生成的頁碼 鍵頁碼 值為連接 
  25.   /** 
  26.    * __construct 構造函數(獲取分頁所需參數) 
  27.    * @param int $count   總條數 
  28.    * @param int $showPages 顯示頁數 
  29.    * @param int $currPage 當前頁數 
  30.    * @param int $subPages 每頁顯示數量 
  31.    * @param string $href  連接(不設置則獲取當前URL) 
  32.    */ 
  33.   public function __construct($count,$showPages,$currPage,$subPages,$href=''){ 
  34.     $this->count=$count
  35.     $this->showPages=$showPages
  36.     $this->currPage=$currPage
  37.     $this->subPages=$subPages
  38.     //如果鏈接沒有設置則獲取當前連接 
  39.     if(emptyempty($href)){ 
  40.       $this->href=htmlentities($_SERVER['PHP_SELF']); 
  41.     }else
  42.       $this->href=$href
  43.     } 
  44.     $this->construct_Pages(); 
  45.   } 
  46.   /** 
  47.    * getPages 返回頁碼數組 
  48.    * @return array 一維數組 鍵為頁碼 值為鏈接 
  49.    */ 
  50.   public function getPages(){ 
  51.     return $this->page_arr; 
  52.   } 
  53.   /** 
  54.    * showPages 返回生成好的頁碼 
  55.    * @param int $style 樣式 
  56.    * @return string   生成好的頁碼 
  57.    */ 
  58.   public function showPages($style=1){ 
  59.     $func='pageStyle'.$style
  60.     return $this->$func(); 
  61.   } 
  62.   /** 
  63.    * pageStyle1 分頁樣式(可參照這個添加自定義樣式 例如pageStyle2()) 
  64.    * 樣式 共45條記錄,每頁顯示10條,當前第1/4頁 [首頁] [上頁] [1] [2] [3] .. [下頁] [尾頁] 
  65.    * @return string 
  66.    */ 
  67.   protected function pageStyle1(){ 
  68.     /* 構造普通模式的分頁 
  69.     共4523條記錄,每頁顯示10條,當前第1/453頁 [首頁] [上頁] [1] [2] [3] .. [下頁] [尾頁] 
  70.     */ 
  71.     $pageStr='共'.$this->count.'條記錄,每頁顯示'.$this->subPages.'條'
  72.     $pageStr.='當前第'.$this->currPage.'/'.$this->countPages.'頁 '
  73.     $_GET['page'] = 1; 
  74.     $pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</a>] </span>'
  75.     //如果當前頁不是第一頁就顯示上頁 
  76.     if($this->currPage>1){ 
  77.       $_GET['page'] = $this->currPage-1; 
  78.       $pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >上頁</a>] </span>'
  79.     } 
  80.     foreach ($this->page_arr as $k => $v) { 
  81.       $_GET['page'] = $k
  82.       $pageStr.='<span>[<a href="'.$v.'" rel="external nofollow" >'.$k.'</a>] </span>'
  83.     } 
  84.     //如果當前頁小于總頁數就顯示下一頁 
  85.     if($this->currPage<$this->countPages){ 
  86.       $_GET['page'] = $this->currPage+1; 
  87.       $pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下頁</a>] </span>'
  88.     } 
  89.     $_GET['page'] = $this->countPages; 
  90.     $pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾頁</a>] </span>'
  91.     return $pageStr
  92.   } 
  93.   /** 
  94.    * construct_Pages 生成頁碼數組 
  95.    * 鍵為頁碼,值為鏈接 
  96.    * $this->page_arr=Array( 
  97.    *         [1] => index.php?page=1 
  98.    *         [2] => index.php?page=2 
  99.    *         [3] => index.php?page=3 
  100.    *         ......) 
  101.    */ 
  102.   protected function construct_Pages(){ 
  103.     //計算總頁數 
  104.     $this->countPages=ceil($this->count/$this->subPages); 
  105.     //根據當前頁計算前后頁數 
  106.     $leftPage_num=floor($this->showPages/2); 
  107.     $rightPage_num=$this->showPages-$leftPage_num
  108.     //左邊顯示數為當前頁減左邊該顯示的數 例如總顯示7頁 當前頁是5 左邊最小為5-3 右邊為5+3 
  109.     $left=$this->currPage-$leftPage_num
  110.     $left=max($left,1); //左邊最小不能小于1 
  111.     $right=$left+$this->showPages-1; //左邊加顯示頁數減1就是右邊顯示數 
  112.     $right=min($right,$this->countPages); //右邊最大不能大于總頁數 
  113.     $left=max($right-$this->showPages+1,1); //確定右邊再計算左邊,必須二次計算 
  114.     for ($i=$left$i <= $right$i++) { 
  115.       $_GET['page'] = $i
  116.       $this->page_arr[$i]=$this->href.'?'.http_build_query($_GET); 
  117.     } 
  118.   } 
  119. ?> 

用法示例demo.php:

  1. /** 
  2.  * demo 
  3.  */ 
  4. header("content-type:text/html;charset=utf8"); 
  5. include('Page.class.php');  //引入類 
  6. //$p=new Page(總條數,顯示頁數,當前頁碼,每頁顯示條數,[鏈接]); 
  7. //連接不設置則為當前鏈接 
  8. $page=isset($_GET['page']) ? $_GET['page'] : 1; 
  9. $p=new Page(100,4,$page,8); 
  10. //生成一個頁碼數組(鍵為頁碼,值為鏈接) 
  11. echo "<pre>"
  12. print_r($p->getPages()); 
  13. //樣式 共45條記錄,每頁顯示10條,當前第1/4頁 [首頁] [上頁] [1] [2] [3] .. [下頁] [尾頁] 
  14. echo $p->showPages(1); 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 武汉市| 缙云县| 衡山县| 广宁县| 湖南省| 云霄县| 张北县| 日照市| 天水市| 宁南县| 华亭县| 琼结县| 兴仁县| 阜康市| 丹凤县| 浏阳市| 漾濞| 抚顺县| 黄浦区| 延安市| 金乡县| 新沂市| 洛宁县| 若尔盖县| 金坛市| 淮南市| 昭平县| 尼玛县| 托克托县| 买车| 宁夏| 上杭县| 松潘县| 绿春县| 贵州省| 萨嘎县| 莆田市| 托克逊县| 柏乡县| 杭锦后旗| 高邮市|