动态分页
获取数据,并预处理
// 获取页码 $_GET
$page = $_GET['p'] ?? 1;
// 越界检测
$page = $page < 1 ? 1 : $page;
// 每页显示条数
$pageSize = 10;
// $pageSize = $_GET['pageSize'];
// 偏移量
$offset = ($page - 1) * $pageSize;
$db = new PDO('mysql:dbname=laravel', 'root', 'root');
$stmt = $db->prepare('select * from cates limit ?,? ');
$stmt->bindParam(1, $offset, PDO::PARAM_INT);//参数序号,参数值,参数类型
$stmt->bindParam(2, $pageSize, PDO::PARAM_INT);
$stmt->execute();
$lists = $stmt->fetchAll(PDO::FETCH_ASSOC);
// var_dump($lists);
// 计算总记录数
$sql = 'SELECT COUNT(id) AS TOTAL FROM cates';
$stmt = $db->prepare($sql);
$stmt->execute();
// print_r($stmt->fetch(PDO::FETCH_ASSOC));
$total = $stmt->fetch(PDO::FETCH_ASSOC)['TOTAL'];
// 总页数= 数据表的总行数/每页显示条数
$pages = ceil($total / $pageSize);//向上取整
网页展示:
<?php foreach ($lists as $list) : extract($list) ?>
<tr>
<td><?= $id ?></td>
<td><?= $name ?></td>
<td><?= $status == 1 ? '正常' : '禁用' ?></td>
<td><?= date('Y-m-d H:i:s', $create_time) ?></td>
<td><?= $update_time ?></td>
<td><button>删除</button><button>编辑 </button></td>
</tr>
<?php endforeach ?>
添加分页符:
<?php
$prev = $page - 1;
if ($page == 1) $prev = 1;
if ($page != 1) :
?>
<a href="?p=<?= $prev ?>">上一页</a>
<?php endif;
//动态生成分页
for ($i = 1; $i <= $pages; $i++) :
$active = ($page == $i) ? 'active' : null;
?>
<a class="<?= $active ?>" href="?p=<?= $i ?>"><?= $i ?></a>
<?php endfor ?>
<?php
$next = $page + 1;
if ($next == $pages) $next = $pages;
if ($page != $pages) : ?>
<a href="">下一页</a>
<?php endif ?>
redis静态分页
安装Redis,并从数据库获取数据:
if (extension_loaded('redis')) {
$redis = new Redis();
$redis->connect('127.0.0.1', '6379');
$lists = (new PDO('mysql:dbname=laravel', 'root', 'root'))->query('select * from cates')->fetchAll(PDO::FETCH_ASSOC);
$lists = json_encode(getTree($lists), 320);
$redis->set('cates', $lists);
}
Redis数据分类:
function getTree($list, $pid = 0, $level = 0)
{
static $tree = [];
foreach ($list as $row) {
if ($row['pid'] == $pid) {
$row['level'] = $level;
$tree[] = $row;
getTree($list, $row['id'], $level++);
}
}
return $tree;
}
从Redis获取数据并预处理:
$redis = new Redis();
$redis->connect('127.0.0.1', '6379');
$lists = json_decode($redis->get('cates'), true);
$page = $_GET['p'] ?? 1;
$page = $page < 1 ? 1 : $page;
$pageSize = 20;
$offset = ($page - 1) * $pageSize;
$pages = ceil(count($lists) / $pageSize);
$lists = array_splice($lists, $offset, $pageSize);
数据网页展示:
<?php foreach ($lists as $list) : extract($list) ?>
<tr>
<td><?= $id ?></td>
<td style="padding-left:<?= $level ?>cm">|<?= $name ?></td>
<td><?= $status == 1 ? '正常' : '禁用' ?></td>
<td><?= date('Y-m-d H:i:s', $create_time) ?></td>
<td><?= $update_time ?></td>
<td><button>删除</button><button>编辑 </button></td>
</tr>
<?php endforeach ?>
分页符:
<?php
$prev = $page - 1;
if ($page == 1) $prev = 1;
if ($page != 1) :
?>
<a href="?p=<?= $prev ?>">上一页</a>
<?php endif;
//动态生成分页
for ($i = 1; $i <= $pages; $i++) :
$active = ($page == $i) ? 'active' : null;
?>
<a class="<?= $active ?>" href="?p=<?= $i ?>"><?= $i ?></a>
<?php endfor ?>
<?php
$next = $page + 1;
if ($next == $pages) $next = $pages;
if ($page != $pages) : ?>
<a href="">下一页</a>
<?php endif ?>
总结:
这节课内容比较多,刚捋完一遍,Redis挺有用,还没来得及优化。