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`;
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:
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['p']; // 2、根据页码取出数据 $pdo = new PDO('mysql:host=localhost;dbname=ceshi','root','root'); $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false); $sql = 'SELECT * FROM `goods` LIMIT :start,:offset'; $limit = ($page-1)*3; $offset = 3; $stmt = $pdo->prepare($sql); $sqlnum = 'SELECT COUNT(*)FROM `goods`'; $stmtnum = $pdo->prepare($sqlnum); $stmtnum->execute(); $num = $stmtnum->fetch(PDO::FETCH_ASSOC); $stmt->bindParam(':start',$limit); $stmt->bindParam(':offset',$offset); if(!$stmt->execute()){ throw new Exception('查找失败',1); } $data = $stmt->fetchAll(PDO::FETCH_ASSOC); var_dump($data); // 3、显示数据+显示分页条 /**总页数**/ $end = floor($num['COUNT(*)']/$offset)+1; $page_banner ='<a href="'.$_SERVER['PHP_SELF'].'?p=1"> 首页</a>'; if($page <=1){ $page_banner .= '<a style="color:#B8BBB3;"> 上一页</a>'; }else{ $page_banner .='<a href="'.$_SERVER['PHP_SELF'].'?p='.($page -1).'"> 上一页</a>'; } if($page+3>4){ $page_banner .= '...'; } if(($page+4)>=$end){ $page_banner .='<a href="'.$_SERVER['PHP_SELF'].'?p='.($end-3).'"> '.($end-3).'</a>'; $page_banner .='<a href="'.$_SERVER['PHP_SELF'].'?p='.($end-2).'"> '.($end-2).'</a>'; $page_banner .='<a href="'.$_SERVER['PHP_SELF'].'?p='.($end-1).'"> '.($end-1).'</a>'; $page_banner .='<a href="'.$_SERVER['PHP_SELF'].'?p='.($end).'"> '.($end).'</a>'; }else{ for($i=0;$i<4;$i++){ $page_banner .='<a href="'.$_SERVER['PHP_SELF'].'?p='.($page+$i).'"> '.($page+$i).'</a>'; } } if(($page+4)<$end){ $page_banner .= '...'; } if($page <=$num['COUNT(*)']/$offset){ $page_banner .='<a href="'.$_SERVER['PHP_SELF'].'?p='.($page +1).'"> 下一页</a>'; }else{ $page_banner .='<a style="color:#B8BBB3;"> 下一页</a>'; } $page_banner .='<a href="'.$_SERVER['PHP_SELF'].'?p='.$end.'"> 尾页</a>'; $page_banner .='<span> 总共'.$end.'页</span> 当前第<span>'.$page."页</span>"; echo $page_banner; ?>
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!