PHP develops simple book background management system new book management paging function
After the new book management page is completed, the database data needs to be queried through SQL statements and displayed in the table.
The paging function is used here to display. After all, the number displayed on the first page is limited. The number of books in libraries is generally relatively large.
Set 8 pieces of book information to be displayed on each page
$pagesize=8;
Get the total query data and calculate the total number of pages $pagecount
<?php $pagesize = 8; //每页显示数 $SQL = "SELECT * FROM yx_books"; $rs = mysqli_query($link,$sql); $recordcount = mysqli_num_rows($rs); //mysql_num_rows() 返回结果集中行的数目。此命令仅对 SELECT 语句有效。 $pagecount = ($recordcount-1)/$pagesize+1; //计算总页数 $pagecount = (int)$pagecount; ?>
Get the current page$pageno
Judge when the current page is empty or smaller than the first page, display the first page.
When the current page number is greater than the total page number, the total page number is displayed as the last page.
Calculate which piece of data each page starts from
<?php $pageno = $_GET["pageno"]; //获取当前页 if($pageno == "") { $pageno=1; //当前页为空时显示第一页 } if($pageno<1) { $pageno=1; //当前页小于第一页时显示第一页 } if($pageno>$pagecount) //当前页数大于总页数时显示总页数 { $pageno=$pagecount; } $startno=($pageno-1)*$pagesize; //每页从第几条数据开始显示 $sql="select * from yx_books order by id desc limit $startno,$pagesize"; $rs=mysqli_query($link,$sql); ?>
Use the while statement to loop out and display the book information in the database in the HTML tag
<?php while($rows=mysqli_fetch_assoc($rs)) { ?> <tr align="center"> <td class="td_bg" width="6%"><?php echo $rows["id"]?></td> <td class="td_bg" width="25%" height="26"><?php echo $rows["name"]?></td> <td class="td_bg" width="11%" height="26"><?php echo $rows["price"]?></td> <td class="td_bg" width="16%" height="26"><?php echo $rows["uploadtime"]?></td> <td width="11%" height="26" class="td_bg"><?php echo $rows["type"]?></td> <td width="11%" height="26" class="td_bg"><?php echo $rows["total"]?></td> <td class="td_bg" width="20%"> <a href="update.php?id=<?php echo $rows['id'] ?>" class="trlink">修改</a> <a href="del.php?id=<?php echo $rows['id'] ?>" class="trlink">删除</a> </td> </tr> <?php } ?>
Finally, click on the homepage , the previous page, next page, and last page functions are displayed.
If the current page is the first page, the next page and last page links are displayed.
When the current page is the total number of pages, the homepage and previous page are displayed as links.
The rest are displayed as normal links.
<tr> <th height="25" colspan="7" align="center" class="bg_tr"> <?php if($pageno==1) { ?> 首页 | 上一页 | <a href="?pageno=<?php echo $pageno+1?>&id=<?php echo $id?>">下一页</a> | <a href="?pageno=<?php echo $pagecount?>&id=<?php echo $id?>">末页</a> <?php } else if($pageno==$pagecount) { ?> <a href="?pageno=1&id=<?php echo $id?>">首页</a> | <a href="?pageno=<?php echo $pageno-1?>&id=<?php echo $id?>">上一页</a> | 下一页 | 末页 <?php } else { ?> <a href="?pageno=1&id=<?php echo $id?>">首页</a> | <a href="?pageno=<?php echo $pageno-1?>&id=<?php echo $id?>">上一页</a> | <a href="?pageno=<?php echo $pageno+1?>&id=<?php echo $id?>" class="forumRowHighlight">下一页</a> | <a href="?pageno=<?php echo $pagecount?>&id=<?php echo $id?>">末页</a> <?php } ?> 页次:<?php echo $pageno ?>/<?php echo $pagecount ?>页 共有<?php echo $recordcount?>条信息 </th> </tr>