图像数组在PHP / HTML中重复/复制
问题描述:
The problem is as shown in the image I've attached, my first day learning php and sql.
I want the images to be assigned to each particular item, so first item picture be displayed above the first item described rather than all 5 of them above each description. I'm still very new to this sorry. Thank you!
My code:
<?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>";
}
?>
问题如我附图所示,我第一天学习php和sql。 p>
我希望将图像分配给每个特定项目,因此第一个项目图片显示在所描述的第一个项目上方,而不是每个描述上方的所有5个项目。 对这个抱歉,我还是很新。 谢谢! p>
我的代码: p>
p>
&lt;?php
$ con = mysqli_connect(“xxxx”,“xxxx”,
“xxxx”,“xxxx”);
$ query = “SELECT * FROM MyShop WHERE ID BETWEEN'1'和'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“&lt; img src ='”。 $ image。 “'height ='200'width ='200'/&gt;”;
}
echo“&lt; center&gt;&lt; h3&gt;” 。 $ person ['姓名']。 “&lt; / h3&gt;&lt; center&gt;”;
echo“&lt; center&gt;&lt; p&gt;” 。 $ person ['Color']。 “&lt; / p&gt;&lt; center&gt;”;
echo“&lt; center&gt;&lt; p class ='ex'&gt;” 。 $ person ['描述']。 “&lt; / p&gt;&lt; center&gt;”;
echo“&lt; center&gt;&lt; p&gt;£”。 $ person ['Price']。 “&LT; / p为H.&LT;中心&gt;” 中; \ N}
&GT;吗?
代码> PRE>
p>
p>
p>
div>
答
All five images are getting displayed first because your foreach loop closes after the img tag.
So it is printing each image, and then displaying your item information. Try this and see if it fixes your issue.
$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++;
}