如何使用 mysql pdo 和 php 创建分页我创建了这样的代码,但收到了一些错误消息
how to create pagination using mysql pdo and php im create code like this, but got some error msg
<?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;
?>
这是一个简单的分页方法:
Here is a simple approach to pagination:
<?php
$limit = 2;
$query = "SELECT count(*) FROM kategori";
$s = $db->query($query);
$total_results = $s->fetchColumn();
$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 ?,?";
$r = $db->prepare($show);
$r->execute([$starting_limit, $limit]);
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 创建分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!