Mir Abzal Ali
Mir Abzal Ali

Reputation: 579

dynamic row column in codeigniter

Simply I can fetch an array with foreach and create a table as:

<?php foreach ($trainee as $key => $value): ?>
 <tr>
  <td><?= $value->TraineeID ?></td>
 </tr> 
<?php endforeach?>

But when numbers of table column not fixed then I can not create the columns and its value.

data array:

Array(
    [0] => stdClass Object
        (
            [TraineeID] => 30012
            [Inv.1] => 720
            [Inv.2] => 2100
            [Inv.3] => 3540
            [Inv.4] => 4920
            [Inv.5] => 6300
            [Inv.6] => 7800
            [Inv.7] => 8700
        )
    [1] => stdClass Object
        (
            [TraineeID] => 30033
            [Inv.1] => 720
            [Inv.2] => 2100
            [Inv.3] => 3540
            [Inv.4] => 4920
            [Inv.5] => 6300
            [Inv.6] => 7800
            [Inv.7] => 8700
        )
    [2] => stdClass Object
        (
            [TraineeID] => 30037
            [Inv.1] => 720
            [Inv.2] => 2100
            [Inv.3] => 3540
            [Inv.4] => 4920
            [Inv.5] => 6300
            [Inv.6] => 7800
            [Inv.7] => 8700
        )
    [3] => stdClass Object
        (
            [TraineeID] => 30038
            [Inv.1] => 720
            [Inv.2] => 2100
            [Inv.3] => 3540
            [Inv.4] => 4920
            [Inv.5] => 6300
            [Inv.6] => 7800
            [Inv.7] => 8700
        )
)

desired Output like below:

TraineeID   Inv.1   Inv.2   Inv.3   Inv.4   Inv.5   Inv.6   Inv.7
30012        720    2100    3540    4920    6300    7800    8700
30033        720    2100    3540    4920    6300    7800    8700
30037        720    2100    3540    4920    6300    7800    8700
30038        720    2100    3540    4920    6300    7800    8700

Upvotes: 0

Views: 788

Answers (2)

Syed Arif Iqbal
Syed Arif Iqbal

Reputation: 1427

Try This

<?php 
    foreach ($trainee as $traine):
        echo "<tr>";
        $tds = get_object_vars($traine);
        foreach ($tds as $property => $value) {
            echo sprintf("<td>%s</td>",$traine->{$property});
        }
        echo "</tr> ";
    endforeach;
?>

Upvotes: 1

438sunil
438sunil

Reputation: 188

Its very simple. Use this

<?php foreach ($trainee as $key => $value): ?>
<tr>
 <td><?= $value->TraineeID ?></td>
 <td><?= $value->Inv.1 ?></td>
 <td><?= $value->Inv.2 ?></td>
 <td><?= $value->Inv.3 ?></td>
 <td><?= $value->Inv.4 ?></td>
 <td><?= $value->Inv.5 ?></td>
 <td><?= $value->Inv.6 ?></td>
 <td><?= $value->Inv.7 ?></td>
</tr> 
<?php endforeach?>

Upvotes: 0

Related Questions