這篇文章主要介紹了PHP分頁顯示的方法,結合執行個體形式分析了php資料庫查詢及內容結合HTML分頁顯示的簡單操作技巧,並附帶一個PHP通用分頁類供大家參考使用,需要的朋友可以參考下
本文執行個體講述了PHP分頁顯示的方法。分享給大家供大家參考,具體如下:
<?phpheader("content-type:text/html;charset=utf-8");$currentpage = 1;if(isset($_GET['page'])) $currentpage = $_GET['page'];//串連資料庫$link = mysql_connect("localhost","root","") or die('串連失敗');mysql_select_db('myschool');mysql_query('set names utf8');$sql ="SELECT count(*) as 'count' from student";//查詢記錄的sql語句$result = mysql_query($sql);$arr = mysql_fetch_array($result);$count = $arr['count'];$pagesize = 3;$pages = ceil($count/$pagesize);//共多少頁$prepage = $currentpage -1;if($prepage<=0) $prepage=1;$nextpage = $currentpage+1;if($nextpage >= $pages){ $nextpage = $pages;}$start =($currentpage-1) * $pagesize;//起始位置$sql = "SELECT * from student limit $start,$pagesize";echo $sql;// $sql = "select * from student";$result = mysql_query($sql);?><!-- html部分 --><!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title></head><body><table border="1"> <tr> <td>學號</td> <td>姓名</td> <td>性別</td> <td>年齡</td> </tr><?php while($arr=mysql_fetch_array($result)){ ?> <td><?php echo $arr['number']; ?></td> <td><?php echo $arr['name']; ?></td> <td><?php echo $arr['sex']; ?></td> <td><?php echo $arr['age']; ?></td> </tr><?php } ?> </table> <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></body></html>
註:當一個檔案中有php和html兩種時,php檔案必須有結束標記
附:php通用分頁類與用法:
Page.class.php檔案:
<?php/** * 分頁類 * * 調用方式: * $p=new Page(總條數,顯示頁數,當前頁碼,每頁顯示條數,[連結]); * print_r($p->getPages()); //產生一個頁碼數組(鍵為頁碼,值為連結) * echo $p->showPages(1); //產生一個頁碼樣式(可添加自訂樣式) * *//*總條數,需要顯示的頁數,當前頁,每頁顯示的條數,串連產生一個一維數組,鍵為頁碼 值為串連返回一個產生好樣式的頁碼(並且可以根據自己需要添加樣式)預設樣式 共45條記錄,每頁顯示10條,當前第1/4頁 [首頁] [上頁] [1] [2] [3] .. [下頁] [尾頁]*/class Page{ protected $count; //總條數 protected $showPages; //需要顯示的頁數 protected $countPages; //總頁數 protected $currPage; //當前頁 protected $subPages; //每頁顯示條數 protected $href; //串連 protected $page_arr=array(); //儲存產生的頁碼 鍵頁碼 值為串連 /** * __construct 建構函式(擷取分頁所需參數) * @param int $count 總條數 * @param int $showPages 顯示頁數 * @param int $currPage 當前頁數 * @param int $subPages 每頁顯示數量 * @param string $href 串連(不設定則擷取當前URL) */ public function __construct($count,$showPages,$currPage,$subPages,$href=''){ $this->count=$count; $this->showPages=$showPages; $this->currPage=$currPage; $this->subPages=$subPages; //如果連結沒有設定則擷取當前串連 if(empty($href)){ $this->href=htmlentities($_SERVER['PHP_SELF']); }else{ $this->href=$href; } $this->construct_Pages(); } /** * getPages 返回頁碼數組 * @return array 一維數組 鍵為頁碼 值為連結 */ public function getPages(){ return $this->page_arr; } /** * showPages 返回產生好的頁碼 * @param int $style 樣式 * @return string 產生好的頁碼 */ public function showPages($style=1){ $func='pageStyle'.$style; return $this->$func(); } /** * pageStyle1 分頁樣式(可參照這個添加自訂樣式 例如pageStyle2()) * 樣式 共45條記錄,每頁顯示10條,當前第1/4頁 [首頁] [上頁] [1] [2] [3] .. [下頁] [尾頁] * @return string */ protected function pageStyle1(){ /* 構造普通模式的分頁 共4523條記錄,每頁顯示10條,當前第1/453頁 [首頁] [上頁] [1] [2] [3] .. [下頁] [尾頁] */ $pageStr='共'.$this->count.'條記錄,每頁顯示'.$this->subPages.'條'; $pageStr.='當前第'.$this->currPage.'/'.$this->countPages.'頁 '; $_GET['page'] = 1; $pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首頁</a>] </span>'; //如果當前頁不是第一頁就顯示上頁 if($this->currPage>1){ $_GET['page'] = $this->currPage-1; $pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >上頁</a>] </span>'; } foreach ($this->page_arr as $k => $v) { $_GET['page'] = $k; $pageStr.='<span>[<a href="'.$v.'" rel="external nofollow" >'.$k.'</a>] </span>'; } //如果當前頁小於總頁數就顯示下一頁 if($this->currPage<$this->countPages){ $_GET['page'] = $this->currPage+1; $pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下頁</a>] </span>'; } $_GET['page'] = $this->countPages; $pageStr.='<span>[<a href="'.$this->href.'?'.http_build_query($_GET).'" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >尾頁</a>] </span>'; return $pageStr; } /** * construct_Pages 產生頁碼數組 * 鍵為頁碼,值為連結 * $this->page_arr=Array( * [1] => index.php?page=1 * [2] => index.php?page=2 * [3] => index.php?page=3 * ......) */ protected function construct_Pages(){ //計算總頁數 $this->countPages=ceil($this->count/$this->subPages); //根據當前頁計算前後頁數 $leftPage_num=floor($this->showPages/2); $rightPage_num=$this->showPages-$leftPage_num; //左邊顯示數為當前頁減左邊該顯示的數 例如總顯示7頁 當前頁是5 左邊最小為5-3 右邊為5+3 $left=$this->currPage-$leftPage_num; $left=max($left,1); //左邊最小不能小於1 $right=$left+$this->showPages-1; //左邊加顯示頁數減1就是右邊顯示數 $right=min($right,$this->countPages); //右邊最大不能大於總頁數 $left=max($right-$this->showPages+1,1); //確定右邊再計算左邊,必須二次計算 for ($i=$left; $i <= $right; $i++) { $_GET['page'] = $i; $this->page_arr[$i]=$this->href.'?'.http_build_query($_GET); } }}?>
用法樣本demo.php:
<?php/** * demo */header("content-type:text/html;charset=utf8");include('Page.class.php'); //引入類//$p=new Page(總條數,顯示頁數,當前頁碼,每頁顯示條數,[連結]);//串連不設定則為當前連結$page=isset($_GET['page']) ? $_GET['page'] : 1;$p=new Page(100,4,$page,8);//產生一個頁碼數組(鍵為頁碼,值為連結)echo "<pre>";print_r($p->getPages());//樣式 共45條記錄,每頁顯示10條,當前第1/4頁 [首頁] [上頁] [1] [2] [3] .. [下頁] [尾頁]echo $p->showPages(1);