I have a table with 7 rows. i want to show a auto number in index base on number of my database rows first I get:
$data['total'] = $this->db->counts_all('posts')
In controller then pass it to index and in index
<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>
But it goes to a loop of rows from 1, 2, 3, 4, 5, 6 ,7 in 7 times. How to fix and loop just on. tkx
for ($i = 1; $i <= $total; $i++) {
// echo $i
}
Edit after more detailes:
You don't need while or for loops. Just set generating keys in foreach loop. Since it is zero-based, echo it like $key + 1
and you'll get the natural order:
<tbody>
<?php foreach ($posts as $key => $post): ?>
<tr>
<td><?php echo $key + 1; ?></td>
</tr>
<?php endforeach; ?>
</tbody>