【问题标题】:Including a PHP file then PHP foreach loop in a html table在 html 表中包含一个 PHP 文件,然后是 PHP foreach 循环
【发布时间】:2018-01-31 04:38:39
【问题描述】:

不确定这是否是最好的方法.. 但是..

我有一个 PHP 文件调用 API 并执行 foreach 循环来获取我想要的所有数据。以前我是从我的 html 文件中调用它并将其放入表中,问题是它在技术上都显示为同一行,这使得格式化变得困难。

这是我曾经拥有的:

<div class="bgimg2 w3-container">

 <table class="w3-table w3-medium w3-text-white">
<tr class="w3-blue-gray">
  <th> <h5> Name </h5> </th>
  <th> <h5> Size </h5> </th>
  <th> <h5> % </h5> </th>
</tr>
<tr>
  <th> <h4> <div id=queue> </h4> </th>
  <th> <h4> <div id=queueSize> </h4> </th>
  <th> <h4> <div id=queuePercent> </h4> </th>
</tr>
</thead>
</table>
</div>

我想让每个项目都显示为它自己的行,所以我尝试直接在表中执行 php foreach 循环。

这是我目前为桌子准备的:

<table class="w3-table w3-medium w3-text-white">
      <thead>
        <tr>
          <th>Name</th>
        </tr>
      </thead>
      <tbody>
        <?php
        require_once ('/api/sabapiQueue.php');
        foreach ($apiResultDataShift as $row){
        ?>
        <tr>
         <td> <?php $row['filename']  ?> </td>
        </tr>
        <?php  } ?>
      </tbody>
    </table>

它不工作,文件甚至没有加载。不知道我哪里出错了。

任何帮助将不胜感激。

【问题讨论】:

  • Previously I was calling this from my html file and putting it into a table 还是 HTML 文件吗?
  • sabapiQueue.php 中有什么内容?文件是否存在.php.html?请在此处添加echo&lt;?php echo $row['filename']; ?&gt;
  • 主要的事情在我面前跳出来......这不是echo声明:&lt;?php $row['filename'] ?&gt;

标签: php html arrays foreach html-table


【解决方案1】:

您似乎错过了没有回显变量的细节。我还建议,也许会向您介绍alternative PHP syntax,这对于您想要将一些 php 块紧密集成到 html 中时很有用,就像您在这里所做的那样:

<table class="w3-table w3-medium w3-text-white">
  <thead>
    <tr>
      <th>Name</th>
    </tr>
  </thead>
  <tbody>
    <?php
    require_once ('/api/sabapiQueue.php');
    foreach ($apiResultDataShift as $row):
    ?>
      <tr>
        <td><?= $row['filename'] ?></td>
      </tr>
    <?php 
    endforeach;
    ?>
  </tbody>
</table>

或者,根据echo manual page,您可以更改此行:

<td><?php echo $row['filename']; ?></td>

【讨论】:

  • 在将 PHP 和 HTML 结合在一起时,使用 : 和 endforeach 是一种很好的方法。
猜你喜欢
  • 2014-06-30
  • 2019-11-17
  • 2021-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-20
  • 2020-06-05
  • 2020-04-03
相关资源
最近更新 更多