先貼張圖看看效果
在貼一下代碼吧
<?php $localhost = "localhost"; $username = "root"; $password = "root"; $db = "test"; //信息 $pagesize = 5; $conn = mysql_connect($localhost,$username,$password); //鏈接數據庫 if(!$conn){ echo "數據庫鏈接失敗".mysql_error(); } mysql_query("SET NAMES 'UTF8'"); //編碼轉化 $db_select = mysql_select_db($db); //選擇表 //查詢記錄總數 $total_sql = "select COUNT(*) from page"; $total_result = mysql_query($total_sql); $total_row_arr = mysql_fetch_row($total_result); $total_row = $total_row_arr[0]; //總條數 //總頁數 $total = ceil($total_row / $pagesize); //當前頁數 $page = @$_GET['p'] ? $_GET['p'] : 1; //limit 下限 $offset = ($page - 1)*$pagesize; $sql = "select * from page order by id limit {$offset},{$pagesize}"; $result = mysql_query($sql); echo "<p>PHP分頁代碼的小模塊</p>"; echo "<table border=1 cellspacing=0 WIDTH=60% align=center>"; echo "<tr><td>ID</td><td>NAME</td></tr>"; while($row = mysql_fetch_assoc($result)){ $id = $row['id']; $name = $row['name']; echo "<tr><td>".$id."</td><td>".$name."</td></tr>"; } echo "</table>"; //上一頁 、下一頁 $pageprev = $page -1 ; if($page > $total){ $pagenext = $total; } else{ $pagenext = $page +1; } // 做鏈接 跳轉; echo "<h3> <a href='page.php?p={$pageprev}'>上一頁</a> 丨<a href='page.php?p={$pagenext}'>下一頁</a></h3>"; mysql_free_result($result); mysql_close($conn); ?>
關鍵點: 1、$sql = "select * from page order by id limit {$offset},{$pagesize}";
2、echo "<h3> <a href='page.php?p={$pageprev}'>上一頁</a> 丨<a href='page.php?p={$pagenext}'>下一頁</a></h3>";
這兩點是關鍵能體現 分頁技術 和php 代碼藝術的地方~
和大家一起學習吧~