PHP:在嵌套的foreach循环中动态显示用户的id

时间:2017-01-21 06:53:27

标签: php mysql foreach

在我正在构建的PHP & MySql CRUD应用内,我有一个表格,显示从"users" table,中提取的数据,如下所示:

<table class="table table-bordered table-striped">
  <thead>
    <tr>
      <th>#</th>
      <th>ID</th>
      <th>First name</th>
      <th>Last name</th>
      <th>Email</th>
      <th>Actions</th>
    </tr>
  </thead>
  <tbody>
    <?php $counter=0; foreach ($get_users as $arr){ ?>
    <tr>
      <td>
        <?php echo ++$counter; ?>
      </td>
      <?php foreach ($arr as $key => $value){ ?>
      <td><?php echo $value; ?></td>
      <?php } ?>
      <td>
        <ul class="list-inline">
          <li><a href="view_user.php?id=5" title="User details"><span class="glyphicon glyphicon-eye-open"></span></a></li>
          <li><a href="#" title="Add data"><span class="glyphicon glyphicon-th-list"></span></a></li>
          <li><a href="#" title="Delete user"><span class="glyphicon glyphicon-trash"></span></a></li>
        </ul>
      </td>
    </tr>
    <?php } ?>
  </tbody>
</table>

最后一列名为&#34; Actions&#34;,有一组用于查看,添加和删除数据的图标链接。考虑上面的表格结构,如何在线上写下用户的ID:

<li><a href="view_user.php?id=5" title="User details"><span class="glyphicon glyphicon-eye-open"></span></a></li>

1 个答案:

答案 0 :(得分:1)

从您的代码中我只假设Id位于$arr的第0个索引上,因此您可以执行此操作

<li><a href="view_user.php?id=<?php echo $arr[0];?>" title="User details"><span class="glyphicon glyphicon-eye-open"></span></a></li>