PHP develops the home page paging function of a simple book lending system

When the main page is completed, the database data needs to be displayed through SQL statements

You need to use the paging function to display it. After all, the number displayed on the first page is limited.

We set 5 pieces of data to be displayed on each page

$pagesize=5;

To obtain the id, you need to sort it in reverse order. The book title category is in Chinese, and the urldecode() function is needed to convert the Chinese into an encoded form.

<?php
if(!urldecode($_GET['proid'])){
   //urldecode()函数将 URL 编码后字符串还原成未编码的样子。编码使用 %## 的格式
   $SQL ="SELECT * FROM yx_books order by id desc";  //倒序排列
}else{
   $SQL ="SELECT * FROM yx_books where type='".urldecode($_GET['proid'])."'";
   //将查询出来的书目类别中文字转换为编码形式
}
?>

Get the total number of queried data, calculate the total number of pages, and then judge the current page. When the current page is less than the first page, the first page is displayed. When the current page number is greater than the total number of pages, the total number is displayed. Pages Pages.

Determine which piece of data to start displaying on each page.

<?php
$rs=mysqli_query($link,$sql);
$recordcount=mysqli_num_rows($rs);  //输出查询的总数
//mysql_num_rows() 返回结果集中行的数目。此命令仅对 SELECT 语句有效。
$pagecount=($recordcount-1)/$pagesize+1;  //计算总页数
$pagecount=(int)$pagecount;
$pageno=empty($_GET["pageno"])?'':$_GET["pageno"];  //当前页

if($pageno=="")  //当前页为空时显示第一页
{
   $pageno=1;
}
if($pageno<1)   //当前页小于第一页时显示第一页
{
   $pageno=1;
}
if($pageno>$pagecount)  //当前页数大于总页数时显示总页数
{
   $pageno=$pagecount;
}
$startno=($pageno-1)*$pagesize;  //每页从第几条数据开始显示
?>

SELECT queries out the statements in the database and then displays them in a loop:

<?php
if(!urldecode($_GET["proid"])){
   $SQL ="SELECT * FROM yx_books order by id desc limit $startno,$pagesize";
}else{
   $SQL ="SELECT * FROM yx_books where type='".urldecode($_GET['proid'])."' order by id desc limit $startno,$pagesize";
}
$rs=mysqli_query($link,$sql);
?>

<?php
 if(!empty($rs)){
     while($rows=mysqli_fetch_array($rs))
   {
?>
    <tr>
       <td height="30" align="center" bgcolor="#FFFFFF"><?php echo $rows["id"];?></td>
       <td align="center" bgcolor="#FFFFFF"><?php echo $rows["name"];?></td>
       <td align="center" bgcolor="#FFFFFF"><?php echo $rows["price"];?></td>
       <td align="center" bgcolor="#FFFFFF"><?php echo $rows["uploadtime"];?></td>
       <td align="center" bgcolor="#FFFFFF"><?php echo $rows["type"];?></td>
       <td align="center" bgcolor="#FFFFFF"><?php echo $rows["leave_number"];?></td>
       <td align="center" bgcolor="#FFFFFF" class="line2"></td>
    </tr>
<?php
    }
 }
?>

Finally, the functions of clicking the homepage, previous page, next page, and last page are displayed.

If the current page is the first page, only the home page and the previous page will be 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 added links

<?php
if($pageno==1)
{
   ?>
   首页 | 上一页 |
   <?php if($pageno+1<= $pagecount) { ?>
   <a href="index.php?proid=<?php echo empty($_GET['proid']) ? '' : urlencode($_GET['proid']); ?>&pageno=<?php echo $pageno + 1 ?>">下一页</a> |
   <a href="index.php?proid=<?php echo empty($_GET['proid']) ? '' : urlencode($_GET['proid']); ?>&pageno=<?php echo $pagecount ?>">末页</a>
   <?php
     }
}
else if($pageno==$pagecount)
{
   ?>
   <a href="index.php?proid=<?php echo urlencode($_GET['proid']);?>&pageno=1">首页</a> |
   <a href="index.php?proid=<?php echo urlencode($_GET['proid']);?>&pageno=<?php echo $pageno-1?>">上一页</a> | 下一页 | 末页
   <?php
}
else
{
   ?>
   <a href="index.php?proid=<?php echo urlencode($_GET['proid']);?>&pageno=1">首页</a> |
   <a href="index.php?proid=<?php echo urlencode($_GET['proid']);?>&pageno=<?php echo $pageno-1?>">上一页</a> |
   <a href="index.php?proid=<?php echo urlencode($_GET["proid"]);?>&pageno=<?php echo $pageno+1?>" class="forumRowHighlight">下一页</a> |
   <a href="?pageno=<?php echo $pagecount?>">末页</a>
   <?php
}
?>
&nbsp;页次:<?php echo $pageno ?>/<?php echo $pagecount ?>页&nbsp;共有<?php echo $recordcount?>条信息


Continuing Learning
||
<?php $rs=mysqli_query($link,$sql); $recordcount=mysqli_num_rows($rs); //输出查询的总数 //mysql_num_rows() 返回结果集中行的数目。此命令仅对 SELECT 语句有效。 $pagecount=($recordcount-1)/$pagesize+1; //计算总页数 $pagecount=(int)$pagecount; $pageno=empty($_GET["pageno"])?'':$_GET["pageno"]; //当前页 if($pageno=="") //当前页为空时显示第一页 { $pageno=1; } if($pageno<1) //当前页小于第一页时显示第一页 { $pageno=1; } if($pageno>$pagecount) //当前页数大于总页数时显示总页数 { $pageno=$pagecount; } $startno=($pageno-1)*$pagesize; //每页从第几条数据开始显示 ?>
submitReset Code