PHP显示表中的下一个图像
我在我的页面上显示db表中的图像,如下所示:PHP显示表中的下一个图像,php,jquery,Php,Jquery,我在我的页面上显示db表中的图像,如下所示: <?php if ($db_found) { $SQL = "SELECT * FROM myTable where id='$posted_id'"; $result = mysql_query($SQL); while ($db_field = mysql_fetch_assoc($result)) { echo '<img src="images/'.$db_field['image'].'" alt="" />'; }
<?php
if ($db_found) {
$SQL = "SELECT * FROM myTable where id='$posted_id'";
$result = mysql_query($SQL);
while ($db_field = mysql_fetch_assoc($result)) {
echo '<img src="images/'.$db_field['image'].'" alt="" />';
}
mysql_close($db_handle);
}
?>
<a href="#">Next</a>
如果$posted_id是示例1,我如何做到这一点。。。当我单击“下一步”链接时,会显示图像id=2,依此类推。为此,您需要刷新页面或使用ajax 您可以像这样在url中传递变量posted_id
<a href="www.yourwebsite.com?posted_id=<?php echo ($db_field['id'] + 1);?>">Next</a>
通过这种方式,您可以从数据库传递下一个id。。如果你的身份证是按顺序的
您还需要以编程方式处理问题,例如,如果数据库中不存在下一个id的记录,该怎么办。您应该使用MySQL限制和顺序过滤器
<?php
if (isset($_GET['current'])) {
$current = $_GET['current'];
} else {
$current = 0;
}
$request = "SELECT * FROM myTable ORDER BY id ASC LIMIT " . $current . ",1";
?>
然后,为了抓住下一个项目,您可以这样做:
<?php
// make the last item point to the first one
$loop = true;
$count = "SELECT COUNT(*) FROM myTable";
if ($current < $count) {
$next = $current + 1;
} else if ($loop) {
$next = 0;
// no loop, then just stay at the end
} else {
$next = $current;
}
?>
很抱歉,这是错误的:您不能保证下一个id为+1,需要通过编程进行检查。。。在这种情况下,我不能把全部逻辑都写给他(