1. 程式人生 > >MySql+PHP實現分頁

MySql+PHP實現分頁

<?php header('Content-Type:text/html;charset=utf-8'); $link=mysql_connect('localhost','root','root'); //實現分頁的原理解析 mysql_select_db('cms2'); $sql="set names utf8"; mysql_query($sql); $query="select id,aname from cms_admin order by id asc limit 0,5 "; $result=mysql_query($query
); echo "<table border='1' width='500'>"; echo "<thead> <tr> <th>ID</th><th>姓名</th> </tr> </thead> <tbody> "; while($row=mysql_fetch_assoc($result)){ echo '<tr>'; echo '<td>'.$row['id'].'</td>'
; echo '<td>'.$row['aname'].'</td>'; echo '</tr>'; } echo "</tbody></table>"; ------------------------------------------------------------------------------------- <?php header('Content-Type:text/html;charset=utf-8'); //$_GET的測試程式 //http://127.0.0.1/project1/get.php?id=123&aname=tom
var_dump($_GET); ------------------------------------------------------------------------------------- <?php //這個是手動分頁 header('Content-Type:text/html;charset=utf-8'); $link=mysql_connect('localhost','root','root'); //實現分頁的原理解析 mysql_select_db('cms2'); $sql="set names utf8"; mysql_query($sql); //每次顯示的記錄數 $page_size=5; //page從瀏覽器的位址列給出 //判斷是否設定了變數$_GET['p'],就是是都在瀏覽器上面設定了p=多少 //設定了的話,就返回獲得當前的頁數,否則的話返回第一頁 $page=isset($_GET['p'])?$_GET['p']:1; //偏移量計算=(當前頁-1)*,每頁顯示記錄數 $offset=($page-1)*$page_size; echo $offset,$page_size; //每頁顯示的記錄 $query="select id,aname from cms_admin order by id asc limit $offset,$page_size "; $result=mysql_query($query); echo "<table border='1' width='500'>"; echo "<thead> <tr> <th>ID</th><th>姓名</th> </tr> </thead> <tbody> "; while($row=mysql_fetch_assoc($result)){ echo '<tr>'; echo '<td>'.$row['id'].'</td>'; echo '<td>'.$row['aname'].'</td>'; echo '</tr>'; } echo "</tbody></table>"; echo '共'.mysql_num_rows($result).'行'; -------------------------------------mysql分頁完整版----------------------------- <?php /* *5、應用mysql獲取結果的函式,將cms_admin表中 *的資料讀取出來放到table表格中。 */ header("Content-Type:text/html;charset=utf-8"); //1、連線資料庫 $link = mysql_connect("localhost", "root", "root"); //2、選擇預設資料庫 mysql_select_db("cms"); //3、操作-有返回結果集的sql語句 返回 資源 //每頁顯示記錄數 $page_size = 5; //獲取當前頁,當前頁從位址列獲取,若沒有當前頁,則預設為1 $page = isset($_GET['p'])?$_GET['p']:1; //獲取總頁數 $query = "select id from cms_admin"; $result = mysql_query($query); //獲取總記錄數 $total = mysql_num_rows($result); //總頁數 = ceil(總記錄數/每頁顯示記錄數) $total_page = ceil($total/$page_size); //對page進行限制 if($page<=0){ //page最小為1 $page=1; }else if($page>=$total_page&&$total_page!=0){ //page最大為total_page $page=$total_page; } //首頁 上一頁 //上一頁的連線地址 = 當前頁-1 $prev = $page-1; $flist = ""; if($prev>=1){ $flist = "<a href='?p=1'>首頁</a>&nbsp; <a href='?p=".$prev."'>上一頁</a>"; } ///中間的頁數列表 // 1 2 3 4 5 6 7 // 假設當前頁為4 //定義佇列長度 $num = 3; $lists = ""; //1 2 3 for($i=$num;$i>=1;$i--){ $n = $page-$i; if($n>0){ $lists.=" <a href='?p={$n}'>{$n}</a> "; } } // 4 將中間的頁數連線上 $lists.="<a href='?p={$page}'>{$page}</a>"; //5 6 7 for($i=1;$i<=$num;$i++){ $n=$page+$i; if($n<=$total_page){ $lists.=" <a href='?p={$n}'>{$n}</a> "; } } $end=""; $next=$page+1; if($next<=$total_page+1){ $end.="<a href='?p={$next}'>下一頁</a> <a href='?p={$total_page}'>尾頁</a> "; } //每頁顯示的記錄 $query="select id,aname from cms_admin order by id asc limit $offset,$page_size "; $result=mysql_query($query); //從mysql_query產生的資源中獲取結果 //mysql_fetch_assoc //輸出table表格的外邊框 echo "<table border='1' width='500'>"; //輸出table表格的表頭 echo "<tr><th>ID</th><th>姓名</th> <th>年齡</th></tr>"; // 迴圈從資料庫中讀取出資料 while($row = mysql_fetch_assoc($result)){ //迴圈輸出table表格的每一行 echo "<tr>"; echo "<td>".$row['id']."</td>"; echo "<td>".$row['aname']."</td>"; echo "<td>".$row['age']."</td>"; echo "</tr>"; } echo "</table>"; echo "共".$total_page."頁&nbsp;當前第".$page."頁". $flist.$lists.$llist; //4、關閉資料庫 釋放資源 mysql_close(); -------------//將cms_article表中的資料讀取出來顯示到頁面上-------------------- <?php header("Content-Type:text/html;charset=utf-8"); //引入資料庫配置檔案 include '../common/db.inc.php'; //將cms_article表中的資料讀取出來 $query="select a.id,title,tname,addtime from cms_article as a inner join cms_type as t on a.id=t.id "; $result=mysql_query($query); ?>