如何使用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;
?>
答案 0 :(得分:7)
这是一种简单的分页方法:
<?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; ?>