简体   繁体   English

php&MySQL 循环显示表

[英]php&MySQL display table with loop

I need to display results from a table, however my loop doesn't produce the table accordingly, the TH are looped for each result, please assist我需要显示表格中的结果,但是我的循环不会相应地生成表格,TH 为每个结果循环,请协助

<?php foreach ($results as $results) :  ?>
<table border="1">
    <tr>
        <th>Date</th>
        <th>GBR</th>
        <th>EUR</th>
        <th>USD</th>
    </tr>
    <tr>
        <td><?php echo $results['date'];?></td>
        <td><?php echo $results['gbr']; ?></td>
        <td><?php echo $results['eur']; ?></td>
        <td><?php echo $results['usd']; ?></td>
    </tr>
</table>
<?php endforeach; ?>

You need to write your tr within the loop.您需要在循环中编写 tr 。

try this:尝试这个:

<table border="1">
  <tr>
    <th>Date</th>
    <th>GBR</th>
    <th>EUR</th>
    <th>USD</th>
  </tr>

  <?php foreach ($results as $results) :  ?>

  <tr>
        <td><?php echo $results['date'];?></td>
        <td><?php echo $results['gbr']; ?></td>
        <td><?php echo $results['eur']; ?></td>
        <td><?php echo $results['usd']; ?></td>
  </tr>

  <?php endforeach; ?>

</table>

Just use the foreach loop where you need dynamically show the data.只需在需要动态显示数据的地方使用foreach循环。

<table border="1">
    <tr>
        <th>Date</th>
        <th>GBR</th>
        <th>EUR</th>
        <th>USD</th>
    </tr>
<?php foreach ($results as $results) :  ?>  
    <tr>
        <td><?php echo $results['date'];?></td>
        <td><?php echo $results['gbr']; ?></td>
        <td><?php echo $results['eur']; ?></td>
        <td><?php echo $results['usd']; ?></td>
    </tr>
<?php endforeach; ?>
</table>    

Don't loop table every time.Just start loop to <tr>不要每次都循环表。只需开始循环到<tr>

<?php foreach ($results as $results) : ?>

    <tr>
        <td><?php echo $results['date']; ?></td>
        <td><?php echo $results['gbr']; ?></td>
        <td><?php echo $results['eur']; ?></td>
        <td><?php echo $results['usd']; ?></td>
    </tr>
<?php endforeach; ?>
</table>

No need to include table and th tag in for each loop you just need to put tr in loop.无需为每个循环包含 table 和 th 标记,您只需要将 tr 放入循环中。

 <table border="1">
     <tr>
      <th>Date</th>
      <th>GBR</th>
      <th>EUR</th>
     <th>USD</th>
   </tr>
    <?php foreach ($results as $result) :  ?>
     <tr>
        <td><?=  $result['date'];?></td>
        <td><?=  $result['gbr']; ?></td>
        <td><?=  $result['eur']; ?></td>
        <td><?=  $result['usd']; ?></td>
    </tr>

    <?php endforeach; ?>

    </table>

Try this code hope is helps试试这个代码希望有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM