This article mainly introduces in detail how to use PHP to realize the paging functionThat is, to achieve the paging effect on the database. Before introducing the content, you can first understand the specific method of connecting to the database in PHP. So in this article [How does PHP connect to Mysql database? ] has introduced specific methods for everyone, which can be used as a reference for friends in need.
The operation of PHP to implement the paging function may be difficult for PHP novices. Below we will explain it one by one through detailed code examples. I hope it will be of some help to friends in need.
PHP implements paging function The complete code example is as follows:
<?php $host = '127.0.0.1'; $user = 'root'; $password = 'root'; $dbName = 'qiye'; $pdo = new PDO("mysql:host=$host;dbname=$dbName", $user, $password); $sql = "select * from user"; $data = $pdo->query($sql)->fetchAll(); $num = 5; $count = count($data); $w = ceil($count / $num); $page = isset($_GET['page']) ? $_GET['page'] : 1; //var_dump($page); $offset = ($page - 1) * $num; //var_dump($offset); $sql = "select * from user limit $offset,{$num}"; $data = $pdo->query($sql)->fetchAll(); $p = ($page == 1) ? 0 : ($page - 1); $n = ($page == $w) ? 0 : ($page + 1); ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <form action=""> <table border="1"> <tr> <th>姓名</th> <th>邮箱</th> <th>号码</th> <th>时间</th> </tr> <?php foreach ($data as $v): ?> <tr> <td><?php echo $v['username']; ?></td> <td><?php echo $v['email']; ?></td> <td><?php echo $v['phone']; ?></td> <td><?php echo date("Y-m-d", $v['time']); ?></td> </tr> <?php endforeach; ?> </table> <?php if ($page==1){ echo "首页"; }else{ echo "<a href='?page=1'>首页</a>"; } if ($p){ echo "<a href='?page={$p}'>上一页</a>"; }else{ echo "上一页"; } if ($n){ echo "<a href='?page={$n}'>下一页</a>"; }else{ echo "下一页"; } if($page== $w){ echo "尾页"; }else{ echo "<a href='?page={$w}'>尾页</a>"; } ?> </form> </body> </html>
In this code, we first connect through PDO method "qiye" is the database, and its data is queried and traversed through sql statements. Then create a form locally to display the total information obtained from the database. Here we use the count function to calculate the total number and assign it to the $count variable.
When there is no paging operation, the result of the data form accessed through the browser is as shown below: What if we want to display only five pieces of data in the form form on each page? Everyone should know the pagination effect, which basically has four parts, home page, previous page, next page and last page. Here we use the ceil function to calculate the total number of pages and assign it to the $w variable. In fact, as long as you can master the PHP jump paging principle, you can easily complete the paging function in PHP.
So in the above complete code example, the specific method of paging has been written for everyone. The previous page and next page are assigned to variables $p and $n respectively, and then the four parts of the output paging are judged through conditional statements.
PHP jump paging principle: If $p, that is, the current page number is not on the homepage, that is, is not equal to 1, we will subtract 1 from the current page number to achieve click-up Jump operation to one page; if $n, that is, the current page number is not on the last page and is not equal to the total number of pages, we will add 1 to the current page number to achieve the jump operation of clicking the next page; if To achieve a direct jump operation when clicking on the homepage or last page, just set the value of $p or $n to 1 or the total number of pages.
The final effect of PHP paging implementation is as follows:
mysql video tutorial or php video tutorial, welcome to refer to and learn!
The above is the detailed content of How to implement paging function in PHP? (Picture + video tutorial). For more information, please follow other related articles on the PHP Chinese website!