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; //每頁顯示數(shù) $SQL = "SELECT * FROM yx_books"; $rs = mysqli_query($link,$sql); $recordcount = mysqli_num_rows($rs); //mysql_num_rows() 返回結(jié)果集中行的數(shù)目。此命令僅對(duì) SELECT 語句有效。 $pagecount = ($recordcount-1)/$pagesize+1; //計(jì)算總頁數(shù) $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"]; //獲取當(dāng)前頁 if($pageno == "") { $pageno=1; //當(dāng)前頁為空時(shí)顯示第一頁 } if($pageno<1) { $pageno=1; //當(dāng)前頁小于第一頁時(shí)顯示第一頁 } if($pageno>$pagecount) //當(dāng)前頁數(shù)大于總頁數(shù)時(shí)顯示總頁數(shù) { $pageno=$pagecount; } $startno=($pageno-1)*$pagesize; //每頁從第幾條數(shù)據(jù)開始顯示 $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>