duankanyi6539 2016-08-21 21:37
浏览 50

PHP - mysql行中的foreach循环

I am trying to generate dynamic images, I am using jquery to pass the id of the image from the mysql db to produce dynamic result , but it doesn't seems to working it only printing one image , my code is here

 <?php

 include 'conn.php';

 if (mysqli_connect_errno()) {
 printf("Connect failed: %s
", mysqli_connect_error());
 exit();
}

 $query = "SELECT id FROM homebg";
 $result = mysqli_query($conn, $query);
 if ($result->num_rows > 0) {

 while($row = $result->fetch_assoc()) {
 foreach ($row as $value) {
        echo " 
        <img class=\"responsive-img col l3\" id=\"img\">
        <script>  
$('#img').attr(\"src\",\"getImageadmin.php?id=\"+".$value.");
$('#img').show();
</script> ";
    }
  }
}
mysqli_close($conn);
?>  

Here I want to pass the value(id) of the image to the jquery used one by one , in each iteration so that it can print images one by one next to each other

  • 写回答

2条回答 默认 最新

  • dqjcb132285 2016-08-21 21:44
    关注

    Your $row is a array with all fields selected in your query (here is just id). So you need to do this :

    while($row = $result->fetch_assoc()) {
        echo "<img class=\"responsive-img col l3\" id=\"img\">
        <script>  
        $('#img').attr(\"src\",\"getImageadmin.php?id=\"+".$row['id'].");
        $('#img').show();
        </script> ";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭