php如何分頁(yè)顯示數(shù)據(jù)庫(kù)中的數(shù)據(jù),主要是建立連接,讀取數(shù)據(jù)行數(shù),設(shè)置pagecount數(shù),設(shè)置每頁(yè)顯示的數(shù)據(jù)行數(shù),進(jìn)行計(jì)算,用表格嵌套php來實(shí)現(xiàn)輸出。
代碼如下
<html> <head><title>分頁(yè)顯示</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>當(dāng)前頁(yè)數(shù):". $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>注冊(cè)日期</td> <td>Other</td> </tr> <?php //循環(huán)顯示內(nèi)容 $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); //顯示分頁(yè)連接 if( $page == 1) echo("第一頁(yè)"); else echo(" <a href=pageShow.php?page=1>第一頁(yè)</a>"); //設(shè)置"上一頁(yè)"連接 if($page==1) echo ("上一頁(yè)"); else echo (" <a href=pageShow.php?page=". ($page-1 ).">上一頁(yè)</a>"); if($page=$PageCount) echo ("下一頁(yè)"); else echo (" <a href=pageShow.php?page=" . ($page+1) .">下一頁(yè)</a>"); if($page=$PageCount) echo ("最后一頁(yè)"); else echo (" <a href=pageShow.php?page=" . $PageCount . ">最后一頁(yè)</a>"); ?> </table> </body> </html>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。