我有这段 PHP 代码:
for($i=0;$i<$rowsize;$i++) {
if($i == $object_id) {
if ($stmt = $mysqli->prepare('SELECT x, y, src FROM house_room1 INNER JOIN objects ON house_room1.object_id=objects.object_id WHERE ref_id = ?')) {
/* Bind parametres */
$stmt->bind_param('i', $ref_id);
/* Insert the parameter values */
$ref_id = $i;
/* Execute the query */
$stmt->execute();
/* Bind resultatet */
$stmt->bind_result($x, $y, $src);
/* Close statement */
$stmt->close();
} else {
/* Something went wrong */
echo 'Something went terrible wrong' . $mysqli->error;
}
echo '<img src="'.$src; echo '"class="item" style="position:relative; left:'.$x; echo 'px; top:'.$y; echo 'px;">';
}
}
此代码以 for 循环开始,循环遍历当前包含两行的数据库表。在执行此操作时,它表示如果变量 $i
等于数据库中的对象 ID,则执行此查询以查找 src
、x
和 y
来自数据库。之后,它将这些值插入图像中,使图像显示在页面上。然而,即使它必须经过两行,因此必须显示两张图像,但它就像是覆盖了第一张图像,因为只显示了最后一张图像。我认为这与我的 PHP 逻辑有关,但我无法弄清楚如何在不覆盖任何图像的情况下显示这两个图像。提前致谢。
最佳答案
<?php
$stmt = $mysqli->stmt_init();
$stmt->prepare('SELECT x, y, src FROM house_room1 INNER JOIN objects ON house_room1.object_id=objects.object_id WHERE objects.object_id = ?');
$stmt->bind_param('i', $object_id);
if ($stmt->execute()) {
$stmt->bind_result($x, $y, $src);
while($stmt->fetch()) {
echo '<img src="' . $src . '" class="item" style="position:relative; left:' . $x . ' px; top:' . $y . 'px;">';
}
} else {
echo 'Something went terrible wrong' . $mysqli->error;
}
$stmt->close();
?>
关于php - for 循环与 if 语句一起覆盖 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21932514/