【问题标题】:How to display an image within a loop?如何在循环中显示图像?
【发布时间】:2013-02-07 05:09:57
【问题描述】:

我正在尝试从查询结果中检索数据。现在我正在尝试的是,如果问题没有图像,则只输出空白,否则如果问题确实有图像,则使用图像标签输出图像。唯一的问题是它一直在输出Array

我哪里错了?

<?php foreach ($arrQuestionId as $key => $question) { ?>

    <div class='lt-container'>

        <p><?php echo htmlspecialchars($arrQuestionNo[$key]) . ": " . htmlspecialchars($arrQuestionContent[$key]); ?></p>

        <?php
//start:procedure image
        $img_result = '';
        if (empty($arrImageFile[$key])) {
            $img_result = '&nbsp;';
        } else {
            ?>

            <p>
                <img alt="<?php echo $arrImageFile[$key]; ?>" height="200" width="200" src="<?php echo $arrImageFile[$key]; ?>">
            </p>

            <?php
        }
//end:procedure image
        ?>

    </div>

<?php } ?>

【问题讨论】:

  • $arrImageFile[$key] 是一个数组吗? $arrImageFile[$key][0] 给你第一张图片?
  • 显示你得到的数组...
  • print_r($arrImageFile) 将为您提供该数组的结构并根据该结构回显。
  • var_dump($arrImageFile) 检查它是否符合您的预期。
  • 你能发布 $arrQuestionId 结构吗?

标签: php html mysqli


【解决方案1】:
<p>
<?php foreach ($arrImageFile[$key] as $i) { ?>
    <img alt="<?= $i ?>" height="200" width="200" src="<?= 'ImageFiles/'.$i ?>">
<php } ?>
</p>

【讨论】:

  • 我应该在 foreach 循环中包含检查图像是否为空的代码还是不这样做?
  • 你可以检查它是否为空,如果是则不运行循环。
  • 那么做我正在做的事情,把你的代码放在else语句中,替换当前在else语句中的代码
  • 我尝试了您的代码,将 else 语句中的内容替换为您的代码,但它没有显示任何图像。它确实删除了Array
猜你喜欢
  • 2011-08-24
  • 2012-05-25
  • 1970-01-01
  • 2011-12-07
  • 2019-03-22
  • 1970-01-01
  • 2019-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多