问题如我所附的图像所示,我的第一天学习php和sql。
我希望将图像分配给每个特定项目,因此第一个项目图片显示在所描述的第一个项目上方,而不是每个描述上方的所有5个项目。对此抱歉,我还是很新。谢谢!
我的代码:
<?php
$con = mysqli_connect("xxxx", "xxxx",
"xxxx", "xxxx");
$query = "SELECT * FROM MyShop WHERE ID BETWEEN '1' AND '5'";
$result = mysqli_query($con, $query);
$array = array("cp.jpeg", "BV-C.jpeg", "BV-B.jpeg", "ADIY.jpeg", "CDG.jpeg",);
while($person = mysqli_fetch_array($result)) {
foreach( $array as $image ){
echo "<img src='" . $image . "' height='200' width='200'/>";
}
echo "<center><h3>" . $person['Name'] . "</h3><center>";
echo "<center><p>" . $person['Colour'] . "</p><center>";
echo "<center><p class='ex'>" . $person['Description'] . "</p><center>";
echo "<center><p> £" . $person['Price'] . "</p><center>";
}
?>
答案 0 :(得分:1)
首先显示所有五个图像,因为你的foreach循环在img标记后关闭。
因此,它会打印每个图像,然后显示您的项目信息。试试这个,看看它是否解决了你的问题。
$count = 0;
while($person = mysqli_fetch_array($result)) {
echo "<img src='" . $array[$count]. "' height='200' width='200'/>";
echo "<center><h3>" . $person['Name'] . "</h3><center>";
echo "<center><p>" . $person['Colour'] . "</p><center>";
echo "<center><p class='ex'>" . $person['Description'] . "</p><center>";
echo "<center><p> £" . $person['Price'] . "</p><center>";
$count++;
}