【问题标题】:Show Array of Images显示图像数组
【发布时间】:2014-04-14 04:26:56
【问题描述】:

我正在尝试在 5 x 3 表中显示图像。

如果全部为空(blank.png),我可以显示图像。

这里是代码

<?PHP

$ds ='\image';
$imagefile = array("EX_W1_01.png", "EX_W1_02.png", "EX_W2_01.png","EX_W3_01.png");
echo "<pre>"; print_r($imagefile);
$file = 'blank.png';
$d = $ds.$file;

echo "<table border = 1 width=\"540px\" cellspacing=\"0px\" cellpadding=\"0px\">";
for($row=1;$row<=5;$row++){
   echo "<tr>";

      for($col=1;$col<=3;$col++){

           // echo"<td height=60px>W$row</td>";
           //if()
           echo"<td height=60px>W$row<img border = 1 height = 120 width = 120 src = $d ></td>" .PHP_EOL;

      }
   echo "</tr>";

}
echo "</table>";     
?>

我想显示基于中间文件名数组$imagefile eg W1, W2的图像,如果不在数组中,我将显示blank.png。

我能够通过此代码获取中间文件名,但我无法在正确的行/列中显示图像。

for($i=0;$i<count($imagefile); $i++)  {
    $wd = substr($imagefile[$i], 3, strpos($imagefile[$i], '_'));
}

【问题讨论】:

  • 数组中列出的图片是否也在\image路径下?
  • 是相同的路径只显示不同的图像。

标签: php html loops html-table nested


【解决方案1】:

你能试试这个吗,

根据您的代码在您回显图像时添加这些,

$wd = substr($imagefile[$i], 3, strpos($imagefile[$i], '_'));
if($wd == *the increment either row or col*) 
{
   echo"<td height=60px>W$row<img border = 1 height = 120 width = 120 src = $d ></td>" .PHP_EOL;       
}
else
{
   echo"<td height=60px>No image</td>" .PHP_EOL;
}

看看它是否有效。

【讨论】:

    【解决方案2】:

    这里是

    <?php
    
    $ds ='/image';
    $imagefile = array("EX_W1_01.png", "EX_W1_02.png", "EX_W2_01.png","EX_W3_01.png");
    //echo "<pre>"; print_r($imagefile);
    $default = 'blank.png';
    ?>
    
    <table border = 1 width=\"540px\" cellspacing=\"0px\" cellpadding=\"0px\">
    <?php
    for($row=1;$row<=5;$row++){
    ?>
        <tr>
    <?php
        for($col=1;$col<=3;$col++){
            // construct the file name
            $filename = 'EX_W' . $row . '_0' . $col . '.png';
            // set the default image file
            $imgPath = $ds . '/' . $default;
            // in case the file name exists in your array with images,
            // set the correct path to the image
            if (in_array($filename, $imagefile)) {
                $imgPath = $ds . '/' . $filename;
            } 
    ?>
            <td height=60px>
                <img border="1" height="120" width="120" src="<?php echo $imgPath; ?>"/>
            </td>
    <?php } ?>
        </tr>
    <?php
    }
    ?>
    </table>
    

    如您所见,我也更喜欢将 php 代码“嵌入”到 HTML 中。如果可以按原样解析,那么通过 PHP 引擎输出 HTML 代码对我来说毫无意义;-)

    【讨论】:

    • 嗨 Havelock,你说得对,它按原样工作,我唯一的问题是因为最后 2 位数字是从表中随机化的,我将无法获得正确的文件名。这就是我需要解析并获取中间文件名的原因。再次感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-22
    • 1970-01-01
    • 2015-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-01
    相关资源
    最近更新 更多