[英]How can i make the pagination?
我想从数据库中对我的图片进行分页。 我试过的任何东西都不起作用,我不知道该怎么办。 有人可以为我解释一下吗? 看了很多教程,仍然找不到解决方案(我是初学者)。
<?php
// Connect to MySQL
$pdo = pdo_connect_mysql();
// MySQL query that selects all the images
$stmt = $pdo->query('SELECT * FROM images ORDER BY uploaded_date DESC');
$images = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<div class="content home">
<h2>Gallery</h2>
<p>Welcome to the gallery page, you can view the list of images below.</p>
<div class="images">
<?php foreach ($images as $image): ?>
<?php if (file_exists($image['path'])): ?>
<a href="#">
<img src="<?=$image['path']?>" alt="<?=$image['description']?>" data-id="<?=$image['id']?>" data-title="<?=$image['title']?>" width="300" height="200">
<span><?=$image['description']?></span>
</a>
<?php endif; ?>
<?php endforeach; ?>
</div>
<div class="container">
<ul class="pagination">
<li class="page-item"><a class="page-link" href="#">Previous</a></li>
<li class="page-item"><a class="page-link" href="#">1</a></li>
<li class="page-item"><a class="page-link" href="#">2</a></li>
<li class="page-item"><a class="page-link" href="#">3</a></li>
<li class="page-item"><a class="page-link" href="#">Next</a></li>
</ul>
</div>
</div>
LIMIT 和 OFFSET 的样子
SELECT * FROM images ORDER BY uploaded_date DESC LIMIT 5 OFFSET 10
OFFSET 值将具有页面的效果。 OFFSET 0 是第 1 页 OFFSET 5 是第 2 页 OFFSET 10 是第 3 页等.....
php 中的分页
<?php
$limit = 3; //set Number of entries to show in a page.
// Look for a GET variable page if not found default is 1.
if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else { $page=1;
}
//determine the sql LIMIT starting number for the results on the displaying page
$page_index = ($page-1) * $limit; // 0
$All_Users=mysqli_query($con,"select * from users limit $page_index, $limit");
while($row=mysqli_fetch_array($All_Users))
{
//show data in table or where you want..
}
$all_data=mysqli_query($con,"select count(*) from users");
$user_count = mysqli_fetch_row($all_data); //total count 9
$total_records = $user_count[0]; //9
$total_pages = ceil($total_records / $limit); // 9/3= 3
if($page >= 2){
echo "<a href='blog.php?page=".($page-1)."' class='btn customBtn2'>Previous</a>";
}
if($page<$total_pages) {
echo "<a href='blog.php?page=".($page+1)."' class='btn customBtn2'>NEXT</a>";
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.