Home > Backend Development > PHP Tutorial > PHP combined with MySQL to implement native paging

PHP combined with MySQL to implement native paging

韦小宝
Release: 2023-03-19 17:52:02
Original
2131 people have browsed it

In PHP developmentFrameworkIt can be seen that it is used more and more widely. If the framework is used too much, it will not be native PracticeIt will gradually become a thing of the past. I hope everyone will not give the basic things to In the past! Our article is about PHP combined with MySQL to implement native paging, and now the general frameworks encapsulate pagination class, so you don’t need to write it manually. But when you have nothing to do, you can try to write and exercise your coding skills.

PHP+MYSQL paging is mainly implemented based on the limit in the SQL statement.

limit statement:

select * from table limit `limit`,`offset`;
Copy after login

You can think of limit as the number of paging pages, and offset as the number of items displayed on each page.
The implementation effect is shown in the figure:

PHP combined with MySQL to implement native paging

This is just an example code, which mainly implements the paging function, page effects, etc. There is no special debugging related to it. .

The main functions are: home page, last page, previous page, next page, total page statistics, positioning the current page, digital display, up to four, and the rest are represented by....

Specific code implementation:
Here is the database operated by PDO

<?php
header("Content-type:text/html;charset=utf-8");
// 1、传入页码
 $page = $_GET[&#39;p&#39;];

// 2、根据页码取出数据
$pdo = new PDO(&#39;mysql:host=localhost;dbname=ceshi&#39;,&#39;root&#39;,&#39;root&#39;);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);

$sql = &#39;SELECT * FROM `goods` LIMIT :start,:offset&#39;;
$limit = ($page-1)*3;
$offset = 3;
$stmt = $pdo->prepare($sql);
$sqlnum = &#39;SELECT COUNT(*)FROM `goods`&#39;;
$stmtnum = $pdo->prepare($sqlnum);
$stmtnum->execute();
$num = $stmtnum->fetch(PDO::FETCH_ASSOC);
$stmt->bindParam(&#39;:start&#39;,$limit);
$stmt->bindParam(&#39;:offset&#39;,$offset);
if(!$stmt->execute()){
            throw new Exception(&#39;查找失败&#39;,1);
        }
 $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_dump($data);

// 3、显示数据+显示分页条
/**总页数**/
$end = floor($num[&#39;COUNT(*)&#39;]/$offset)+1;

$page_banner =&#39;<a href="&#39;.$_SERVER[&#39;PHP_SELF&#39;].&#39;?p=1">   首页</a>&#39;;
if($page <=1){
    $page_banner .= &#39;<a style="color:#B8BBB3;">   上一页</a>&#39;;
}else{
    $page_banner .=&#39;<a href="&#39;.$_SERVER[&#39;PHP_SELF&#39;].&#39;?p=&#39;.($page -1).&#39;">   上一页</a>&#39;;
}
if($page+3>4){
    $page_banner .= &#39;...&#39;;
}
if(($page+4)>=$end){
    $page_banner .=&#39;<a href="&#39;.$_SERVER[&#39;PHP_SELF&#39;].&#39;?p=&#39;.($end-3).&#39;">   &#39;.($end-3).&#39;</a>&#39;;
    $page_banner .=&#39;<a href="&#39;.$_SERVER[&#39;PHP_SELF&#39;].&#39;?p=&#39;.($end-2).&#39;">   &#39;.($end-2).&#39;</a>&#39;;
    $page_banner .=&#39;<a href="&#39;.$_SERVER[&#39;PHP_SELF&#39;].&#39;?p=&#39;.($end-1).&#39;">   &#39;.($end-1).&#39;</a>&#39;;
    $page_banner .=&#39;<a href="&#39;.$_SERVER[&#39;PHP_SELF&#39;].&#39;?p=&#39;.($end).&#39;">   &#39;.($end).&#39;</a>&#39;;
}else{
    for($i=0;$i<4;$i++){
    $page_banner .=&#39;<a href="&#39;.$_SERVER[&#39;PHP_SELF&#39;].&#39;?p=&#39;.($page+$i).&#39;">   &#39;.($page+$i).&#39;</a>&#39;;
    }
}

if(($page+4)<$end){
    $page_banner .= &#39;...&#39;;
}

if($page <=$num[&#39;COUNT(*)&#39;]/$offset){
    $page_banner .=&#39;<a href="&#39;.$_SERVER[&#39;PHP_SELF&#39;].&#39;?p=&#39;.($page +1).&#39;">    下一页</a>&#39;;
}else{
    $page_banner .=&#39;<a style="color:#B8BBB3;">   下一页</a>&#39;;
}

$page_banner .=&#39;<a href="&#39;.$_SERVER[&#39;PHP_SELF&#39;].&#39;?p=&#39;.$end.&#39;">     尾页</a>&#39;;

$page_banner .=&#39;<span>   总共&#39;.$end.&#39;页</span>   当前第<span>&#39;.$page."页</span>";
echo $page_banner;
?>
Copy after login

You can practice it locally! If you don’t understand, you should practice more! If you forget these basic basic things, it will be difficult to pick them up again! !

Related recommendations:

#Page paging class implementation

Today I will share with you a useful one Nice looking php pagination class!

The implementation principle of php paging

Today I will take you to analyze the principle of php paging.

A universal php paging class example code

This article mainly introduces the universal php paging class, which is especially easy to use and is suitable for friends who need to use php paging class do not miss it.

thinkphp paging to achieve the effect

The display of a large amount of data requires pagination of the content. This article is to introduce thinkphp paging for organization. Friends in need can learn about it together. one time.

The above is the detailed content of PHP combined with MySQL to implement native paging. 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