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

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改