2016-04-07 168 views
0

我有一个for循环插入数据到我的表,运作良好。停止循环重复表

问题是每个循环我希望它开始一个新的行,但它启动一个新的表。任何想法如何解决这一问题?

<?php if (isset($records)) : foreach ($records as $row) : ?> 

    <div id="mobile-content"> 
        <table class="table table-hover"> 
         <thead> 
         <tr> 
          <th>Account Name</th> 
          <th>Balance</th> 
          <th>Start Date</th> 
         </tr> 
         </thead> 

         <tbody> 
         <tr> 
          <td><?php echo $row->bank_name; ?></td> 
         </tr> 
         <tr> 
          <td><?php echo $row->bank_balance; ?></td> 
         </tr> 
         <tr> 
          <td><?php echo $row->bank_start_date; ?></td> 
         </tr> 
         </tbody> 
        </table> 
       </div> 
      <?php endforeach; ?> 

回答

1

你应该更改代码以循环表体内,而不是在整个表代码:

<div id="mobile-content"> 
    <table class="table table-hover"> 
     <thead> 
      <tr> 
       <th>Account Name</th> 
       <th>Balance</th> 
       <th>Start Date</th> 
      </tr> 
     </thead> 

     <tbody> 

     <?php if (isset($records)) : foreach ($records as $row) : ?> 
      <tr> 
       <td><?php echo $row->bank_name; ?></td> 
       <td><?php echo $row->bank_balance; ?></td> 
       <td><?php echo $row->bank_start_date; ?></td> 
      </tr> 
     <?php endforeach; ?> 
     </tbody> 
    </table> 
</div> 

注:我也删除多余的<tr>,因为你需要一行三个细胞。

我希望有帮助:D

+0

谢谢,看起来没错。虐待我的代码尝试。当它生病时接受你的答案。谢谢 – Beep