我目前正在开发一个在线购物网站。不幸的是,我被数据库显示图像的问题所阻止。
我想要实现的结果是从数据库中检索图像并将它们显示在不同的页面中。例如,数据库中共有12个图像,我想在一个页面上显示6个图像。并且,当用户单击下一页(第2页)时,将显示其余6个图像。我被困在这里好多天了。任何人都可以帮我解决这个问题。最好的祝愿。
答案 0 :(得分:1)
试试这个
<?php
if(isset($_Get['page']))
{$page = $_Get['page'];}
else
{$page = 1; }
$firstimage = $page * 6 - 6;
$lastimage = $page * 6;
$sql = 'SELECT * from image LIMIT $firstimage, $lastimage';
?>
在botom
添加链接<a href="yourpage.php?page='<?php echo $page + 1; ?> '">Next page</a>
答案 1 :(得分:0)
您应该计算打开的页面的图像集中的偏移量,并使用对数据库的查询,如下所示:(假设您的图像元数据存储在Image
数据库表中)
SELECT * from Image LIMIT $offset, $limit
答案 2 :(得分:0)
这并不复杂,你可以使用分页。遵循这个逻辑:
为每个页面提供一个数字并从查询字符串中获取该数字,然后在您的SQL查询中使用限制
$page = $_GET['page'];
if ($_GET['page'] == "1") {
{
$startFrom = 0;
$totalCount = 6;
} else {
$startFrom = 6*$_GET['page'];
$totalCount = 6;
}
在限制中使用这两个变量
LIMIT $startFrom, $totalCount;
答案 3 :(得分:0)
您要做的事情称为pagination。幸运的是,它并不太难,网上有很多关于构建这样一个脚本的教程。
分页的一般概念是,您将从分组中选择数据,从特定的偏移量开始,只有一定长度或限制
Select * from yourTable LIMIT $yourOffset, $yourLimit
从这里开始,您最喜欢使用next
或previous
按钮来简单地告诉脚本当前offset
和limit
将是什么。< / p>
一个例子可能是:
<form action="" method="POST">
<input type="hidden" name="offset" value="10"/>
<input type="hidden" name="limit" value="20"/> // The amount to be returned per page
<input type="submit" value="next page" name="submit" />
</form>
然后,在PHP
中,您只需检索limit
和offset
值:
$limit = '';
$offset = '';
if (isset($_POST['submit'])) {
{
$limit = $_POST['limit'];
$offset = $_POST['offset'];
// Run query here
}
答案 4 :(得分:0)
$page = $_GET['page_no'];
$count = $page*6;
$sql = "select images from table limit $count-6,$count";
答案 5 :(得分:0)
您需要的是SQL代码中的限制和偏移。
在php中使用:
$limit = 15;
$offset = ($page - 1) * $limit;
The limit is the amount of values you want to be shown.
The $page variable you need to get because of the page you are on. You will need
to write some code to keep your page value in order to know what page you are on.
If you are on page 3 you want the 6 images for that page.
If you have that you pass your $limit and offset to your sql function.
在SQL中:
$sql = 'select * from images ORDER BY x LIMIT ' . $limit . ' OFFSET ' . $offset;