CodeIgniter Forums
Display an incrementing number in each row when using table class - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Display an incrementing number in each row when using table class (/showthread.php?tid=26139)



Display an incrementing number in each row when using table class - El Forum - 01-07-2010

[eluser]max123[/eluser]
I want to display an incrementing number in each row by using table class

eg:

1 column1 column2
2 column1 column2
3 column1 column2
4 column1 column2

how can i do that. I'm new, therefore explain little bit more

thanx in advance


Display an incrementing number in each row when using table class - El Forum - 01-07-2010

[eluser]flaky[/eluser]
when you are building the array for the table, just add the extra column with the number
Code:
$this->load->library('table');

$data = array(
             array(1, 'Name', 'Color', 'Size'),
             array(2, 'Fred', 'Blue', 'Small'),
             array(3, 'Mary', 'Red', 'Large'),
             array(4, 'John', 'Green', 'Medium')
             );

echo $this->table->generate($data);



Display an incrementing number in each row when using table class - El Forum - 01-07-2010

[eluser]max123[/eluser]
I forgot to mention that i'm taking data from the database table


Display an incrementing number in each row when using table class - El Forum - 01-07-2010

[eluser]flaky[/eluser]
Then I wouldn't recommend using the table class, generate it yourself
example
Code:
//in the view
<table>
&lt;?php
    $no = 1;
    foreach($item_list as $item){
?&gt;
    <tr>
        <td>&lt;?php echo $nr; ?&gt;</td>
        <td>&lt;?php echo $item['item_1']; ?&gt;
        <td>&lt;?php echo $item['item_2']; ?&gt;
        <td>&lt;?php echo $item['item_3']; ?&gt;
        <td>&lt;?php echo $item['item_4']; ?&gt;
    </tr>
&lt;?php
        $nr++;
    }
?&gt;
</table>



Display an incrementing number in each row when using table class - El Forum - 01-07-2010

[eluser]max123[/eluser]
Thanx. But I need a way to do that using codeigniter table class. Otherwise it is point less to use the table class. I have already used the table class.

Any ways, thanx for your support


Display an incrementing number in each row when using table class - El Forum - 01-07-2010

[eluser]flaky[/eluser]
one other way you could do it, change function in the the model to this
Code:
$query = $this->db->get('mytable');

$nr = 1;
$data = "";
foreach ($query->result() as $row)
{
    $data[] = array('no' => $nr, 'value' => $row->item_1);
    $nr++;
}

return $data;