在表格中安排数据?

时间:2016-07-28 11:52:49

标签: html5 codeigniter foreach html-table codeigniter-3

我从数据库中检索数据并尝试在表中实现它们,但现在我不知道如何在表中排列它们。我的观点是:

<?php include('inc/header.php');?>

<div class="main">

<table>

    <thead>
            <tr>ID:</tr>
            <th>Name:</th>

</thead>

<tbody>
<tr>

        <?php foreach ($view as $row) :?>

            <?php $i = 1;?>
            <?php echo "<td>".$row->audio."</td>";?>
            <?php echo $i++;?>
            <?php endforeach;?>


    </tr>
</tbody>

</table>


</div>
<?php include('inc/footer.php');?>

我只想让地方ID从一个增加到多个记录并将它们排列到一个表中。

4 个答案:

答案 0 :(得分:1)

你的foreach循环应该是这样的

<tbody>
    <?php 
    $i = 1;
    foreach ($view as $row)
    {
        echo "<tr>";
        echo "<td>".$i."</td>";
        echo "<td>".$row->audio."</td>";
        $i++;
        echo "</tr>";
    }
   ?>
</tbody>

<thead>应该是

<thead>
    <tr>
        <th>ID:</th>
        <th>Name:</th>
    </tr>
</thead>

答案 1 :(得分:0)

你可以试试这个

<?php $i = 1;?>
<?php foreach ($view as $row) :?>
<tr>
            <?php echo "<td>".$i++."</td>";?>
            <?php echo "<td>".$row->audio."</td>";?>

 </tr>
<?php endforeach;?>

答案 2 :(得分:0)

试用此代码

  <?php
  $i=1;
  foreach ($view as $row) 
  {

 ?>
    <tr>
        <td><?php echo $i; ?></td>
        <td><?php echo $row->audio;?></td>        
     </tr>
    <?php 
    $i++;
    } ?>

答案 3 :(得分:0)

请尝试以下代码:

<?php
  $i=0;//place the initial value outside the loop
  foreach ($view as $row) 
  {
  $i++;//increment the value
 ?>
    <tr>
        <td><?php echo $i;//display the value ?></td>
        <td><?php echo $row->audio;?></td>        
     </tr>
    <?php 

    } ?>