【问题标题】:PHP Foreach inside foreach display data in table rowPHP Foreach inside foreach 在表格行中显示数据
【发布时间】:2018-04-26 07:56:53
【问题描述】:

如何在 foreach 函数内的 tablerow 中显示数据?议程变量工作正常,行变量是问题所在,我需要在标签内使用它

<?php
$agenda = $days = json_decode(get_field( "field_uc_content_json" ));
unset($agenda[0]); ?>



<table class="table">
    <thead>
        <tr>
          <th> Day </th>
          <th>Description</th>
          <th>Hours</th>
        </tr>
    </thead>
<tbody>
   <?php foreach($agenda as $column) { ?>
   <tr>
    <td><?php echo $column[0]; ?></td>
    <td><?php
    $rows = explode( "\n", $column[1]);
    foreach ($rows as $row) { ?>
    <tr> <?php echo $row; ?> </tr>
    <?php    } ?>
   </td>
</tbody>
</table>

第二个foreach

vardump议程

vardump

【问题讨论】:

  • 你能var_dump($agenda);var_dump($rows);吗?我想知道这些数组里面有什么。所以我可以帮助你显示这些值。你的&lt;tbody&gt; 的整个代码也会很好。
  • @RonnieOosting 当然,我刚刚编辑了我的问题
  • 您也可以编辑&lt;tbody&gt; 代码吗?
  • @RonnieOosting 如何>?
  • @RonnieOosting 干杯 m8。希望 OP 仍然阅读此内容并将其中一个答案标记为已接受。 +1 来自我 :)

标签: php foreach html-table


【解决方案1】:

我认为它现在可以正常工作了。我测试了它。

<table class="table">
    <thead>
        <tr style="background: #8da80c;">
          <th> Day </th>
          <th>Description</th>
          <th>Hours</th>
        </tr>
    </thead>
<tbody>
   <?php foreach($agenda as $column) { ?>
   <tr style="background: #e0e545;">
    <td style="padding: 14px;"><?php echo $column[0]; ?></td>
    <td style="padding: 14px;"><?php echo $column[1]; ?></td>
     <td style="padding: 14px;"><?php echo $column[2]; ?></td>
   </tr>
   <?php } ?>
</tbody>
</table>

我用这些值进行了测试

  <pre>
<?php
$agenda = array (array("montag","Lorem ipsum dolor sit amet","08:00"),array("montag","Lorem ipsum dolor sit amet","08:00"),array("montag","Lorem ipsum dolor sit amet","08:00"));

print_r($agenda);
?>
     </pre>

【讨论】:

  • 不。 OP对实际问题不是很清楚。在聊天中变得更加清晰。主要是 HTML / CSS 问题。
  • 主要是。问题是当一列内的数据变长并在另一行上继续时,下一列中的数据不会对齐。但他甚至不再在聊天中回复了..
【解决方案2】:

因为你变了

$agenda = $days = json_decode(get_field( "field_uc_content_json" ));
unset($agenda[0]); ?>

它是一个对象,你不要像数组一样使用它,你需要在爆炸中像对象一样管理,像这样:

$rows = explode( "\n", $column->1);

【讨论】:

    【解决方案3】:

    这样就可以了:

    <tbody>
    <?php foreach($agenda as $column):
        $rows = explode("\n", $column[1]);?>
        <tr>
            <td rowspan="<?php count($rows) + 1?>"><?php $column[0];?></td>
        </tr>
    
        <?php foreach($rows as $row): ?>
            <tr>
                <td><?php echo $row ;?></td>
                <td><?php echo $row ;?></td>
            </tr>
        <?php endforeach;
    
    endforeach; ?>
    </tbody>
    

    正如我们(和 icecub)在聊天室中讨论的那样。

    祝你好运朋友。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-26
      • 2023-03-22
      • 2013-03-07
      • 2013-05-03
      • 1970-01-01
      • 2017-11-19
      • 2012-11-12
      • 1970-01-01
      相关资源
      最近更新 更多