0

我的数据库中有许多图像,它们都对应于相同的项目 ID。

我希望它们在浏览器中彼此相邻显示。

但以下代码仅输出最新图像,而不是全部:

//get all the images in this project and add them to the content variable for output

                if (!empty($FormProjectID)) {

                    $DBQuery3 = mysqli_query($dblink, "SELECT * FROM images WHERE project_id = '$FormProjectID'");

                    if (mysqli_num_rows($DBQuery3) < 1) {
                        $content = '
                            <p>This project is empty. <a href="index.php?page=upload&id='.$FormProjectID.'">Upload</a> some files to get started.</p>
                        ';
                    } else {

                        while($row = mysqli_fetch_array($DBQuery3)) {
                            $DBImageID = $row['image_id'];
                            $DBProjectID = $row['project_id'];
                            $DBImageName = $row['image_name'];
                            $DBImageDescription = $row['image_description'];
                            $DBDateCreated = $row['date_created'];
                            $DBLinkToFile = $row['link_to_file'];
                            $DBGivenName = $row['given_name'];

                            //if the image was given a name by the user, display it
                            //otherwise display the generated name

                            if (strlen($DBGivenName) > 1) {
                                $FileName = $DBGivenName;
                            } else {
                                $FileName = $DBImageName;
                            }

                            $content = '
                                <div class="image">
                                <a href="index.php?page=image&id='.$DBImageID.'"><img src="'.$DBLinkToFile.'" alt="'.$FileName.'" title="'.$FileName.'"/></a>
                                </div>
                            ';
                    }
                }

如何获取所有图像,以便最终 html 看起来像这样:

<div class="image">
                                <a href="index.php?page=image&id='.$DBImageID.'"><img src="'.$DBLinkToFile.'" alt="'.$FileName.'" title="'.$FileName.'"/></a>
                                </div>
<div class="image">
                                <a href="index.php?page=image&id='.$DBImageID.'"><img src="'.$DBLinkToFile.'" alt="'.$FileName.'" title="'.$FileName.'"/></a>
                                </div>
<div class="image">
                                <a href="index.php?page=image&id='.$DBImageID.'"><img src="'.$DBLinkToFile.'" alt="'.$FileName.'" title="'.$FileName.'"/></a>
                                </div>

稍后在我的 html 页面中,我有:

<?php echo $content; ?>
4

2 回答 2

1

更改$content = '$content .= '

PHP 字符串运算符

注意: $content在 while 循环之前将变量设置为空字符串或 null

于 2012-05-21T19:23:25.957 回答
0

您正在寻找的是字符串连接(请参阅字符串运算符

要解决您的问题,您需要首先初始化一个空字符串,在您的 while 循环之前执行此操作

} else {
    $content = '';
    while($row = mysqli_fetch_array($DBQuery3)) {

然后使用连接运算符.附加每个图像

    $content .= '
        <div class="image">
        <a href="index.php?page=image&id='.$DBImageID.'"><img src="'.$DBLinkToFile.'" alt="'.$FileName.'" title="'.$FileName.'"/></a>
        </div>
    ';
于 2012-05-21T19:24:51.777 回答