php - 如何在 while 循环中显示数据库中的图像

标签 php html css

这里是 PHP/MYSQL 初学者,我正在创建一个类别浏览页面,我必须显示来自名为“inventory”的 mysql 数据库的 50 张图像。我通过 while 循环生成了链接,但不知道如何在没有 50 行 <img src 的情况下连续生成图像。 .我想生成它的方式是:

图片

链接(换行),
image2(换行),
link2(换行),
等等...

我的图片编号从 1.jpg 到 50.jpg。

<?php
    include 'connection.php';
    $sql = "SELECT * FROM products";
    $result = mysqli_query($mysql, $sql) or die("Bad Query: $sql");
    if(mysqli_num_rows($result) > 0 ) {
        while($row = mysqli_fetch_array($result)) {
            echo "<a href = 'details.php?ID={$row['Item_Number']}'>{$row['Item_Name']}</a><br>\n";

        }
    }
?>

最佳答案

除非我错过了那里的要点,否则您只需将 img 标记添加到循环中生成的 HTML - 从数据库中选择适当的路径和数据。

<?php

    include 'connection.php';
    $sql = 'select `jpg`, `item_name`, `item_number` from `products`';


    $result = mysqli_query( $mysql, $sql ) or die("bad query: $sql");
    if( mysqli_num_rows( $result ) > 0 ) {

        while( $row = mysqli_fetch_array( $result ) ) {

            # Ensure that the path below is correct
            echo "
                <img src='/path/to/images/{$row['jpg']}' alt='{$row['item_name']}' />
                <br />
                <a href='details.php?id={$row['item_number']}'>{$row['item_name']}</a>
                <br />
                <br />";

        }
    }
?>

关于php - 如何在 while 循环中显示数据库中的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56488722/

相关文章:

php - 通过 PHP 上传远程服务器文件

javascript - 使用 Bootstrap 的两列等高布局

javascript - 点击叠加效果一次触发所有叠加

php - 表单内的表单 HTML

javascript - 选中多个复选框时,表单字段应相应地与公共(public)字段一起显示

html - Internet Explorer 8 背景色环绕在下方

html - 跟随其主容器的重叠 div

PHP:从查询结果发送批量电子邮件

javascript - 自动从其他网站检索视频缩略图

php - 在 mod_rewrite 之后获取变量