php ideas for implementing record paging: 1. Get the number of records in the result set; 2. Set the number of records displayed on each page; 3. Get the total number of pages; 4. Control the number of records displayed on each page; 5 , just notify the script program of the page number displayed by passing parameters.
#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.
What is the idea of implementing record paging in php?
PHP Mysql realizes data paging display:
$PageSize is used to save the number of records displayed on each page. Its value is set by the user according to needs, and can be implemented directly through an assignment statement.
$RecordCount and
$PageSize$PageCount
,
$Page , you can use the following statement:
SELECT * FROM table name LIMIT ($Page- 1) * $ PageSize, $ PageSize
http:// localhost/ viewPage. php? page= 2 The page parameter is used to specify the current page number. In viewPage.php, use the following statement to read the parameters:
<!doctype html> <html> <head> <meta charset="utf-8" /> <title>Document</title> </head> <body> <?php header("content-type:text/html;charset=utf-8"); //获取当前页码 $page=$_GET['page']; if($page==0){ $page=1; } //设置每页最大能显示的数量 $pagesize=3; //连接数据库 $conn=mysql_connect("localhost","root","root"); mysql_select_db("test"); mysql_query("set names utf-8"); if(!$conn){ die("mysql_connect_failed".mysql_connect_error()); } else echo("connected succeed"."<br />"); //获取结果集的记录数 $row=mysql_fetch_row(mysql_query("select count(1) from clerk")); $recordcount=$row[0]; //计算总页数 if($recordcount==0) $pagecount=0; else if($recordcount<$pagesize ||$recordcount==$pagesize){ $pagecount=1; //如果 记录 总数 量小 于 每页 显示 的 记录 数量, 则 只有 一页 } else if($recordcount%$pagesize==0){ $pagecount=$recordcount/$pagesize; //如果 没有 余数, 则 页数 等于 总 记录 数量 除以 每页 显示 记录 的 数量 } else $pagecount=(int)($recordcount/$pagesize)+1; //取 记录 总数 量 不能 整除 每页 显示 记录 的 数量, // 则 页数 等于 总 记录 数量 除以 每页 显示 记录 数量 的 结果 取整 再加 1 echo("当前页码:".$page."/".$pagecount."<br />"); ?> <table width="449" border="1"> <tr> <td>员工姓名</td> <td>职务</td> <td>薪水</td> </tr> <?php //循环显示当前页面的记录 header("content-type:text/html;charset=utf-8"); echo $page; //$sql="select * from clerk limit" .($page-1)*$pagesize.",".$pagesize; //$page为当前页码 $sql=($page-1)*$pagesize; $result=mysql_query("select * from clerk limit {$sql},{$pagesize}"); while($row=mysql_fetch_row($result)) { echo("<tr />"); echo("<td>$row[0]</td>"); echo("<td>$row[2]</td>"); echo("<td>$row[3]</td>"); echo("<tr />"); } mysql_close($conn); //显示分页链接 if($page==1){ echo("第一页"); } else echo("<a href=viewpage.php?page=1>第一页</a>"); //设置上一页连接 if($page==1){ echo("上一页"); } else echo("<a href=viewpage.php?page=".($page-1).">上一页</a>"); //设置下一页链接 if($page==$pagecount){ echo("下一页"); } else echo("<a href=viewpage.php?page=".($page+1).">下一页</a>"); //设置最后一页 if($page==$pagecount){ echo("最后一页"); } else echo("<a href=viewpage.php?page=".$pagecount.">最后一页</a>"); ?> </table> </body> </html>
PHP Video Tutorial"
The above is the detailed content of What is the idea of implementing record paging in php?. For more information, please follow other related articles on the PHP Chinese website!