如何用PDO PHP创建分页[关闭]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用PDO PHP创建分页[关闭]相关的知识,希望对你有一定的参考价值。
如何使用mysql pdo创建分页和php我创建这样的代码,但得到了一些错误消息
<?php
$halaman = 2; //batasan halaman
$page = isset($_GET['halaman'])? (int)$_GET["halaman"]:1;
$mulai = ($page>1) ? ($page * $halaman) - $halaman : 0;
$result = $db->query("SELECT * FROM kategori");
$result->execute();
$total = $result->fetchAll();
$pages = ceil(count($total/$halaman));
$query = $db->prepare("SELECT * FROM kategori LIMIT $mulai, $halaman");
$query->execute();
$data = $query->fetchAll();
$no =$mulai+1;
?>
<?php foreach ($data as $value): ?>
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $value['nama_kat']; ?></td>
<?php
endforeach;
?>
答案
这是一种简单的分页方法:
<?php
$limit = 2;
$query = "SELECT * FROM kategori";
$s = $db->prepare($query);
$s->execute();
$total_results = $s->rowCount();
$total_pages = ceil($total_results/$limit);
if (!isset($_GET['page'])) {
$page = 1;
} else{
$page = $_GET['page'];
}
$starting_limit = ($page-1)*$limit;
$show = "SELECT * FROM kategori ORDER BY id DESC LIMIT $starting_limit, $limit";
$r = $db->prepare($show);
$r->execute();
while($res = $r->fetch(PDO::FETCH_ASSOC)):
?>
<h4><?php echo $res['id'];?></h4>
<p><?php echo $res['nama_kat'];?></p>
<hr>
<?php
endwhile;
for ($page=1; $page <= $total_pages ; $page++):?>
<a href='<?php echo "?page=$page"; ?>' class="links"><?php echo $page; ?>
</a>
<?php endfor; ?>
以上是关于如何用PDO PHP创建分页[关闭]的主要内容,如果未能解决你的问题,请参考以下文章