如何使用 PDO PHP 创建分页

2021-12-26 00:00:00 php mysql pdo pagination

如何使用 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; ?>

相关文章