Home > Daily Programming > PHP Knowledge > How to implement paging function in PHP? (Picture + video tutorial)

How to implement paging function in PHP? (Picture + video tutorial)

藏色散人
Release: 2020-02-22 17:49:48
Original
12943 people have browsed it

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 = &#39;127.0.0.1&#39;;
$user = &#39;root&#39;;
$password = &#39;root&#39;;
$dbName = &#39;qiye&#39;;

$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[&#39;page&#39;]) ? $_GET[&#39;page&#39;] : 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[&#39;username&#39;]; ?></td>
                <td><?php echo $v[&#39;email&#39;]; ?></td>
                <td><?php echo $v[&#39;phone&#39;]; ?></td>
                <td><?php echo date("Y-m-d", $v[&#39;time&#39;]); ?></td>
            </tr>
        <?php endforeach; ?>
 </table>
    <?php
    if ($page==1){
 echo "首页";
    }else{
 echo "<a href=&#39;?page=1&#39;>首页</a>";
    }
 if ($p){
 echo "<a href=&#39;?page={$p}&#39;>上一页</a>";
    }else{
 echo "上一页";
    }
 if ($n){
 echo "<a href=&#39;?page={$n}&#39;>下一页</a>";
    }else{
 echo "下一页";
    }
 if($page== $w){
 echo "尾页";
    }else{
 echo "<a href=&#39;?page={$w}&#39;>尾页</a>";
    }
 ?>
</form>
</body>
</html>
Copy after login

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: How to implement paging function in PHP? (Picture + video tutorial)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:

How to implement paging function in PHP? (Picture + video tutorial)

How to implement paging function in PHP? (Picture + video tutorial)

##The above are the details about PHP implementation of paging function Introduction, I hope it will be helpful to friends in need! For more related tutorials, you can visit php Chinese website

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template