PHP基於limit子句的分頁詳細操作

2020-07-16 10:05:38
PHP使用MYSQL中的limit子句實現分頁的範例,原生分頁程式碼,演示了從連線MYSQL、選擇資料庫到讀取記錄、計算分頁、輸出分頁按鈕的整個過程,若對PHP分頁不太懂的朋友,相信這個例子對你會有很大幫助。

註:例子中的MYSQL使用者名稱和密碼以及表名和欄位名請以你的為準,請修改這些資訊後再測試。

<html>    
    <head>    
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">    
    <title>應用limit子句實現分頁顯示</title>    
    <script>    
    //全選/取消全選    
    function chek(){    
         var leng = this.form1.chk.length;    
         if(leng==undefined){    
           leng=1;    
           if(!form1.chk.checked)    
            document.form1.chk.checked=true;    
            else    
                document.form1.chk.checked=false;    
         }else{     
           for( var i = 0; i < leng; i++)    
            {    
                if(!form1.chk[i].checked)    
                    document.form1.chk[i].checked = true;    
                else    
                    document.form1.chk[i].checked = false;    
            }    
         }    
        return false;    
    }    
    </script>
<?php    
   $conn = mysqli_connect("localhost","root","pwd");//連線MYSQL    
   mysqli_query("set names gb2312");    
   mysqli_select_db("db_softinfo"); //指定資料表    
   ?> 
   </head>    
   <body>    
   <center>    
   <table width="798" border="0" cellpadding="0" cellspacing="0">    
       <tr>    
           <td>    
           <table width="100%" height="38" border="0" cellpadding="0" cellspacing="0">    
               <tr>    
                   <td width="193" align="center" valign="middle">    
                   <b><?php echo date("Y-m-d")." ".date(l);?></b></td>    
                   <td width="101" align="center" valign="middle"><a href="index.php?action=show" class="a">瀏覽目錄</a></td>    
                   <td width="102" align="center" valign="middle"><a href="#">新增內容</a></td>    
                   <td width="101" align="center" valign="middle"><a href="#">簡單查詢</a></td>    
                   <td width="101" align="center" valign="middle"><a href="#">高階查詢</a></td>    
                   <td width="101" align="center" valign="middle"><a href="#">分組統計</a></td>    
                   <td width="99" align="center" valign="middle"><a href="#">退出系統</a></td>    
               </tr>    
           </table>    
           </td>    
       </tr>    
   </table>    
   <table width="799" height="300" border="0" cellpadding="0" cellspacing="0">    
       <tr>    
         <td align="center" valign="middle">   
<?php    
  //瀏覽資料庫內容    
   if (($action == null) or ($_GET[action] == "show")){    
?>    
<form name="form1" id="form1" method="post">    
   <table width="90%"  border="0" cellpadding="0" cellspacing="0">    
     <tr>    
       <td height="25" width="5%" class="top">&nbsp;</td>    
       <td width="5%" class="top">id</td>    
       <td width="30%" class="top">名稱</td>    
       <td width="10%" class="top">價格</td>    
       <td width="20%" class="top">時間</td>    
       <td width="10%" class="top">類別</td>    
       <td width="10%" class="top">操作</td>    
     </tr>
<?php        
       $pagesize = 5 ; //每頁顯示記錄數    
       $sqlstr = "select * from tb_mrbook order by id";    
       $total = mysqli_query($sqlstr,$conn);    
       $totalNum = mysqli_num_rows($total); //總記錄數    
       $pagecount = (int)(($totalNum - 1) / $pagesize) + 1;//總頁數    
       (!$absolutepage)?($absolutepage = 1):$absolutepage; //當前顯示頁數    
       ($absolutepage <= $pagecount)?$absolutepage:($absolutepage = $pagecount);       
       $f_pageNum = $pagesize * ($absolutepage - 1);//當前頁的第一條記錄    
       $sqlstr1 = $sqlstr." limit ".$f_pageNum.",".$pagesize;    
       $result = mysqli_query($sqlstr1,$conn);    
       while ($rows = mysqli_fetch_row($result)){    
           echo "<tr><td height='25' align='center' class='m_td'>";    
           echo "<input type=checkbox name='chk[]' id='chk' value=".$rows[0].">";    
           echo "</td>";    
           for($i = 0; $i < count($rows); $i++){    
               echo "<td height='25' align='center' class='m_td'>".$rows[$i]."</td>";    
           }    
           echo "<td class='m_td'><a href='#'>修改</a>/<a href='#'>刪除</a></td>";    
           echo "</tr>";    
       }    
   ?>   
<tr>    
   <td height="25" colspan="7" class="m_td" align="left"><a href="" onClick="return chek();">全部選擇/取消</a>&nbsp;&nbsp;    
   <input type="hidden" name="action" value="delall"><input type="submit" value="刪除選擇" onclick = 'return false'>&nbsp;&nbsp;    
   共<?php echo $totalNum ?>本圖書,第<?php echo $absolutepage ?>頁/共<?php echo $pagecount ?>頁:<a href="?absolutepage=<?php echo ($absolutepage > 1)?($absolutepage - 1):1;s?>">上一頁</a>&nbsp;<a href="?absolutepage=<?php echo ($absolutepage < ($pagecount - 1))?($absolutepage+1):$pagecount;?>">下一頁</a></td>    
   </tr>    
   </table>    
   </form>    
   <?php } ?>    
   </td>    
   </tr>    
   </table>    
   </center>    
   </body>    
   </html>

以上內容不足之處還請指出,不勝感激!

更多相關問題請存取PHP中文網:https://www.php.cn/

以上就是PHP基於limit子句的分頁詳細操作的詳細內容,更多請關注TW511.COM其它相關文章!