将一系列图像从db输出到变量?

时间:2012-05-21 19:18:46

标签: php while-loop mysqli

我的数据库中有许多图像,它们都对应于相同的项目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; ?>

2 个答案:

答案 0 :(得分:1)

$content = '更改为$content .= '

PHP String Operators

注意:在while循环

之前,将$content变量设置为空字符串或null

答案 1 :(得分:0)

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

要解决您的问题,首先要初始化一个空字符串,在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>
    ';