Welcome Guest, Not a member yet? Register   Sign In
Display an incrementing number in each row when using table class
#1

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

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

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

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

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

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




Theme © iAndrew 2016 - Forum software by © MyBB