您好,登錄后才能下訂單哦!
php如何分頁顯示數據庫中的數據,主要是建立連接,讀取數據行數,設置pagecount數,設置每頁顯示的數據行數,進行計算,用表格嵌套php來實現輸出。
代碼如下
<html> <head><title>分頁顯示</title> <meta http-equiv="Content-Type" content="text/html" ;charset=utf-8" /> </head> <body> <?php //$page=$_GET['page']; $page=$_GET['page']; if($page ==0) $page = 1; $PageSize=2; $conn= mysqli_connect("localhost","root","root","datevase"); if(empty($conn)) die("mysqli_connect failed: ".musqli_connect_error()); $sql="SELECT COUNT(1) FROM userInformation"; $results=$conn->query($sql); $row = $results->fetch_row(); $RecordCount=$row[0]; if($RecordCount) { if($RecordCount<$PageSize) $PageSize=1; if($RecordCount % $PageSize) { $PageCount = (int )($RecordCount % $PageSize +1); } else $PageCount=$RecordCount % $PageSize; } else $PageCount=0; //////// //set language /////// mysqli_query($conn,"SET NAMES gb2312"); echo("<br>當前頁數:". $page."/".$PageCount); ?> <table width ="500" border="1"> <tr> <td>UserName</td> <td>UserId</td> <td>UserSex</td> <td>座右銘</td> <td>E-Mail</td> <td>密碼</td> <td>注冊日期</td> <td>Other</td> </tr> <?php //循環顯示內容 $sql=("SELECT userName,userID,userSex,userMoto,userMail,userPassword,userRegisterData,userProMenuName FROM userInformation LIMIT ".($page-1)*$PageSize.",".$PageSize); $results=$conn->query($sql); while($row=$results->fetch_row()) { echo("<tr>"); echo ("<td>". $row[0]." </td>"); echo ("<td>". $row[1]." </td>"); echo ("<td>". $row[2]." </td>"); echo ("<td>". $row[3]." </td>"); echo ("<td>". $row[4]." </td>"); echo ("<td>". $row[5]." </td>"); echo ("<td>". $row[6]." </td>"); echo ("<td>". $row[7]." </td>"); echo ("</tr>"); } mysqli_close($conn); //顯示分頁連接 if( $page == 1) echo("第一頁"); else echo(" <a href=pageShow.php?page=1>第一頁</a>"); //設置"上一頁"連接 if($page==1) echo ("上一頁"); else echo (" <a href=pageShow.php?page=". ($page-1 ).">上一頁</a>"); if($page=$PageCount) echo ("下一頁"); else echo (" <a href=pageShow.php?page=" . ($page+1) .">下一頁</a>"); if($page=$PageCount) echo ("最后一頁"); else echo (" <a href=pageShow.php?page=" . $PageCount . ">最后一頁</a>"); ?> </table> </body> </html>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。