【问题标题】:New table row(tr) is not incrementing / getting created, after the cols become 4?在 cols 变为 4 之后,新表行(tr)没有递增/创建?
【发布时间】:2017-12-13 18:21:23
【问题描述】:

我正在从目录中检索图像,但所有图像仅显示在一行中,因此在每列 4 之后,该行应递增,其余应显示在下一行..

$<?php

      $files = glob("images/hardware/*.*");
      //$files1=glob("images/software/*.*");
      ?>
      <div class="container">
            <h3>Hardware Solutions</h3>
      <table>

      <tr>
            <?php for ($j = 0; $j < count($files); $j++) {
            $image = $files[$j];

            //$imagePath=pathinfo($files[$j]);
            //$imgdetail=implode(" ",$imagePath);
            //echo ''.$imgdetail.''; 

            ?>
            <td>
            <img src="<?php echo ''.$image.'';?>" width="300px" height="300px" style="text-align:center;padding:10px;"/>
            <?php }?>
            </td>

      </tr>
      </table>

【问题讨论】:

  • 您在此处向我们展示的代码没有显示试图解决问题的证据。此外,提出的问题是解决布局问题的糟糕方法。

标签: php html loops html-table


【解决方案1】:

我建议使用foreach,并使用Modulo PHP Operator,因为您的代码表明数组的键是严格的数字。这是它的样子:

<?php foreach($files as $key => $value) : ?>
    <td>
        <img src="<?php echo $value;?>" width="300px" height="300px" style="text-align:center;padding:10px;"/>
    </td>
    <?php if ($key % 3 == 0) : ?>
        </tr><tr>
    <?php endif; ?>
<?php endforeach; ?>

【讨论】:

    猜你喜欢
    • 2019-10-25
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 2021-06-01
    • 1970-01-01
    • 2012-08-11
    • 2016-02-10
    • 1970-01-01
    相关资源
    最近更新 更多