php - 以表号形式给出数据库中的数据

标签 php mysql for-loop

我在表中有一个foreach。我为数据库中的每一行查找一行。所以我的数据库有 10 条记录,我和我的表格在彼此下方显示所有这些记录。到目前为止一切顺利。

我想对它们进行编号,从 1 到 10,显示在每行前面。

这是我的 table :

<table class="table table-striped mt-3">
      <thead>
        <tr>
          <th scope="col">#</th>
          <th scope="col">Team</th>
          <th scope="col">Player</th>
          <th scope="col">P</th>
          <th scope="col">W</th>
          <th scope="col">D</th>
          <th scope="col">L</th>
          <th scope="col">GF</th>
          <th scope="col">GA</th>
          <th scope="col">GD</th>
          <th scope="col">P</th>
        </tr>
      </thead>
      <tbody>
        <?php $count =  count($table); ?>
        <?php foreach($table as $t): ?>
            <tr>
                <td><?php for($i = 1; $i < $count; $i++;)
                {
                   echo $i; ?>} 
                </td>
                <td><?php echo $t['team']; ?></td>
                <td><?php echo $t['speler']; ?></td>
                <td><?php echo $t['gw']; ?></td>
                <td><?php echo $t['w']; ?></td>
                <td><?php echo $t['g']; ?></td>
                <td><?php echo $t['v']; ?></td>
                <td><?php echo $t['dv']; ?></td>
                <td><?php echo $t['dt']; ?></td>
                <td><?php echo $t['ds']; ?></td>
                <td><?php echo $t['points']; ?></td>
            </tr>
        <?php endforeach; ?>
      </tbody>
    </table>

这是我的方法

 public function fifaLeagueTable() {
    $getTable = "SELECT * FROM fifa_league ORDER BY points DESC";
    $table = $this->model->readAll($getTable);
    $count =  count($table);
    include('app/views/fifaLeagueTable.php');
 }

如果我 var_dump $count,我会收到 int(10)。所以它正在计算行数,并且我可以访问 10。我得到一个白页,因此 for 循环中可能有问题或其他问题。我做错了什么?

最佳答案

您只需再创建一个变量即可完成。这是更新的代码:

  <table class="table table-striped mt-3">
  <thead>
    <tr>
      <th scope="col">#</th>
      <th scope="col">Team</th>
      <th scope="col">Player</th>
      <th scope="col">P</th>
      <th scope="col">W</th>
      <th scope="col">D</th>
      <th scope="col">L</th>
      <th scope="col">GF</th>
      <th scope="col">GA</th>
      <th scope="col">GD</th>
      <th scope="col">P</th>
    </tr>
  </thead>
  <tbody>
    <?php $count =  count($table); $num = 1; ?>
    <?php foreach($table as $t): ?>
        <tr>
            <td><?php echo $num; ?> 
            </td>
            <td><?php echo $t['team']; ?></td>
            <td><?php echo $t['speler']; ?></td>
            <td><?php echo $t['gw']; ?></td>
            <td><?php echo $t['w']; ?></td>
            <td><?php echo $t['g']; ?></td>
            <td><?php echo $t['v']; ?></td>
            <td><?php echo $t['dv']; ?></td>
            <td><?php echo $t['dt']; ?></td>
            <td><?php echo $t['ds']; ?></td>
            <td><?php echo $t['points']; ?></td>
        </tr>
    <?php $num++ ; endforeach; ?>
  </tbody>
</table>

关于php - 以表号形式给出数据库中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55237300/

相关文章:

php - 查找日期范围与指定日期范围相交的行

php - Zend_Http_Response 的 getRawBody() 和 getBody() 方法之间的区别?

python - 如何修复此 mysql 社区-> connector-python(未安装 python 3.4)

java - 读取文本文件然后执行字符计数并打印每个字符的相对频率

r - dunn 的循环数据帧列的测试

php - HTML 编辑器可以通过标签折叠代码块并移动它们?

php - 带有 omnipay 的 CodeIgniter 自定义值

ios - for循环疯狂

mysql - 如何使用 MySQL 查找百分比?

php - 电视指南PHP脚本/数据库结构