PHP paging detailed operation based on limit clause

王林
Release: 2023-04-07 07:56:01
forward
2837 people have browsed it

PHP uses the limit clause in MYSQL to implement paging. The native paging code demonstrates the entire process from connecting to MYSQL, selecting the database, reading records, calculating paging, and outputting paging buttons. If you don’t know much about PHP paging Friends, I believe this example will be of great help to you.

Note: The MYSQL user name and password as well as the table name and field name in the example are subject to yours. Please modify this information before testing.

<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"> </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=&#39;25&#39; align=&#39;center&#39; class=&#39;m_td&#39;>";    
           echo "<input type=checkbox name=&#39;chk[]&#39; id=&#39;chk&#39; value=".$rows[0].">";    
           echo "</td>";    
           for($i = 0; $i < count($rows); $i++){    
               echo "<td height=&#39;25&#39; align=&#39;center&#39; class=&#39;m_td&#39;>".$rows[$i]."</td>";    
           }    
           echo "<td class=&#39;m_td&#39;><a href=&#39;#&#39;>修改</a>/<a href=&#39;#&#39;>删除</a></td>";    
           echo "</tr>";    
       }    
   ?>   
<tr>    
   <td height="25" colspan="7" class="m_td" align="left"><a href="" onClick="return chek();">全部选择/取消</a>      
   <input type="hidden" name="action" value="delall"><input type="submit" value="删除选择" onclick = &#39;return false&#39;>      
   共<?php echo $totalNum ?>本图书,第<?php echo $absolutepage ?>页/共<?php echo $pagecount ?>页:<a href="?absolutepage=<?php echo ($absolutepage > 1)?($absolutepage - 1):1;s?>">上一页</a> <a href="?absolutepage=<?php echo ($absolutepage < ($pagecount - 1))?($absolutepage+1):$pagecount;?>">下一页</a></td>    
   </tr>    
   </table>    
   </form>    
   <?php } ?>    
   </td>    
   </tr>    
   </table>    
   </center>    
   </body>    
   </html>
Copy after login

Please point out any deficiencies in the above content, thank you very much!

For more related questions, please visit the PHP Chinese website: //m.sbmmt.com/

The above is the detailed content of PHP paging detailed operation based on limit clause. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:codefans.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!