如何根据codeigniter中的数据库行数回显自动增量行号?

时间:2017-08-10 20:28:33

标签: codeigniter auto-increment

我有一个包含7行的表格。 我想在索引基础上显示我的数据库行数的自动编号 首先我得到:

$data['total'] = $this->db->counts_all('posts')

在控制器中,然后将其传递给索引和索引

<table>
    <thead>
        <tr>
            <th>No.</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $counter = 0;
        while($counter < $total){
        $counter++;
        ?>
        <tr>
            <td>
            <?php
            echo $counter;
            }
            ?>
            </td>
        </tr>
    </tbody>
</table>

但它在7次中从1,2,3,4,5,6,7行循环。 如何修复和循环。 TKX

1 个答案:

答案 0 :(得分:0)

for ($i = 1; $i <= $total; $i++) {
    // echo $i
}

更多详细信息后编辑:

您不需要while或for循环。只需在foreach循环中设置生成密钥。因为它是从零开始的,所以像$key + 1一样回应它,你将获得自然顺序:

<tbody>
<?php foreach ($posts as $key => $post): ?>
    <tr>
        <td><?php echo $key + 1; ?></td>
    </tr>
<?php endforeach; ?>
</tbody>