-4
<table class="table table-bordered table-hover">
    <?php foreach ($pin_data as $pin) {
      ?>
      <tr>
        <td><?php  echo $pin->location;?> <?php  echo $pin->pincode;?></td>
      </tr>
      <?php
    } ?>

  </table>

我从数据库中获取数据。我想在表中显示该数据,但我想连续显示 5 列。之后它从新行开始。请帮忙。

4

3 回答 3

0

希望对你有帮助 :

做这样的事情:

<table class="table table-bordered table-hover">
    <?php foreach ($pin_data as $pin) { ?>
      <tr>

         /*change your column name as according to you*/

         <td><?php  echo $pin->location;?></td> 
         <td><?php  echo $pin->pincode;?></td>
         <td><?php  echo $pin->location;?></td> 
         <td><?php  echo $pin->pincode;?></td>
         <td><?php  echo $pin->location;?></td> 
      </tr>
      <?php } ?>
</table>
于 2018-07-09T15:21:06.673 回答
0

您可以使用模数运算符和计数器:

<table class="table table-bordered table-hover">
   <tr> <!-- start initial row -->
    <? $counter = 1; 
       foreach ($pin_data as $pin):?>
          <td>
              <?= $pin->location;?> <?= $pin->pincode;?>
          </td>
       <?if($counter % 5 == 0):?> //if $counter is a multiple of 5, close the current row and start an new one 
       </tr> 
       <tr>  
       <?endif;?>
       <? $counter++;?>
       <?endforeach;?>
</table>
于 2018-07-09T15:22:07.560 回答
0

在第一个值中添加 <tr>,在第五个值中添加“< /tr>”。它会运行。

<table class="table table-bordered table-hover">
<?php $timer=1;
 foreach ($pin_data as $pin) {
 if($timer%5==1)
 {
 ?>
  <tr>
 <?php
 }
  ?>
    <td><?php  echo $pin->location;?> <?php  echo $pin->pincode;?></td>
  if($timer%5==0)
  {
  ?>
  </tr>
  <?php
   }
  <?php
   $timer++;
   } ?>
  </table>
于 2018-07-09T15:11:28.313 回答