duanlvxing7707 2014-05-08 15:30
浏览 18
已采纳

将数据库中的图像显示到PHP上的表中

I have some problems creating a table that will display pictures from a php database

This is my code:

$query = "SELECT * FROM images ORDER BY name ASC ";

            $result = $db->query($query);
            $num_result = $result->num_rows;

            echo "<h1> Images</h1>";

            $array = array();
            for ($i = 0; $i < $num_result; $i++){

                $row = $result->fetch_assoc();
                $name = $row['name'];
                $URL = $row['imageURL'];


                $array[] = $URL;
            }
//this loop is printing the images correctly in order

foreach ($array as $image){
        echo '<img src="'.$image.'"/>';
          }  

What I am trying to accomplish is to create a table with 2 Columns that will print the images there, something like this

    echo '<table>';
    echo ' <tr>';
    echo '      <td>image 1</td>';
    echo '      <td>image 2</td>';
    echo '   </tr>';

    echo '   <tr>';
    echo '      <td>image 3</td>';
    echo '      <td> image 4</td>';
    echo '   </tr>';

      // and so on if there is more images

   echo '</table>';

Any suggestions will help , Thanks!

  • 写回答

1条回答 默认 最新

  • duangu1878 2014-05-08 15:43
    关注

    i think you want something like

    $i=0;
    echo"<table>";
    echo"<tr>";
    foreach ($array as $image)
    {
        if($i%2==0 && $i>0 )
        echo"</tr><tr>";
    
        echo"<td>";
        echo '<img class="coupons" src="'.$image.'"/>';
        echo"</td>";
        $i++;
    }
    echo"</tr>";  
    echo"</table>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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