doumi1852 2016-02-28 00:39
浏览 36
已采纳

图像数组在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!

enter image description here

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>";
}



?>

  • 写回答

1条回答 默认 最新

  • duangan6731 2016-02-28 00:44
    关注

    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++;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况