CodeIgniter Forums

Full Version: Display an incrementing number in each row when using table class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[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

El Forum

[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);

El Forum

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

El Forum

[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>

El Forum

[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

El Forum

[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;