Home > Backend Development > PHP Tutorial > PHP's native paging code

PHP's native paging code

不言
Release: 2023-03-31 13:52:02
Original
2328 people have browsed it

This article mainly introduces the native paging code of PHP, which has certain reference value. Now I share it with everyone. Friends in need can refer to it.

<?php 
header("content-type:text/html;charset=utf-8");
$pdo = new PDO("mysql:host=127.0.0.1;dbname=demo;charset=utf8",&#39;root&#39;,&#39;&#39;);
$sql = "select * from cai";
$db  = $pdo->query($sql)->fetchAll();
$total = count($db);
$num = 5;
$cpage = isset($_GET[&#39;page&#39;])?$_GET[&#39;page&#39;]:1;
$pagenum = ceil($total/$num);
$offset = ($cpage-1)*$num;
$sql = "select * from cai limit {$offset},{$num}";
$result  = $pdo->query($sql)->fetchAll();
$start = $offset+1;
$end=($cpage==$pagenum)?$total : ($cpage*$num);//结束记录页  
$next=($cpage==$pagenum)? 0:($cpage+1);//下一页  
$prev=($cpage==1)? 0:($cpage-1);//前一页 
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>展示</title>
</head>
<body>
<center>
<form action="">
<table border="1">
<tr>
<td>ID</td>
<td>标题</td>
<td>作者</td>
<td>时间</td>
<td>分类</td>
<td>操作</td>
</tr>
<?php foreach ($result as $key => $value): ?>
<tr>
<td><?=$value[&#39;id&#39;]?></td>
<td><?=$value[&#39;title&#39;]?></td>
<td><?=$value[&#39;user&#39;]?></td>
<td><?=$value[&#39;time&#39;]?></td>
<td><?=$value[&#39;type&#39;]?></td>
<td><a href="javascript:viod(0)">编辑</a>|| <a href="javascript:viod(0)">删除</a></td>
</tr>
<?php endforeach ?>
</table>
</form>
<?php 
echo &#39;<table align="center" width="800" border="1">&#39;;   
echo "共<b>$total</b>条记录,本页显示<b>{$start}-{$end}</b> {$cpage}/{$pagenum}";  
    if($cpage==1)  
        echo "  首页  ";  
    else  
        echo "  <a href=&#39;?page=1&#39;>首页</a>  ";  
    if($prev)  
        echo "  <a href=&#39;?page={$prev}&#39;>上一页</a>  ";  
    else  
        echo "  上一页  ";  
    if($next)  
        echo "  <a href=&#39;?page={$next}&#39;>下一页</a>  ";  
    else  
        echo "  下一页  ";  
    if($cpage==$pagenum)  
        echo "  尾页  ";  
    else  
        echo "  <a href=&#39;?page={$pagenum}&#39;>尾页</a>  ";  
echo &#39;</td></tr>&#39;;  
echo &#39;</table>&#39;;  
?>
</center>
</body>
</html>
Copy after login

The above is the entire content of this article. I hope it will be helpful to you. Everyone’s learning is helpful. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

laravel manually creates array paging

thinkPHP3.2.3 combined with Laypage to implement paging function

thinkPHP’s statistical ranking and paging display functions

The above is the detailed content of PHP's native paging code. 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