Overview

In this example, we will discuss How to create a dynamic table using CodeIgniter.
- Table.php: (application\controllers\Table.php)
In this example, we will discuss How to create a dynamic table using CodeIgniter.
<?php
class Table extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$data = [
0 => [
'level1' => 'System',
'level2' => 'System Apps',
'level3' => 'System Env',
'info' => 'App Test',
'name' => 'Foo'
],
1 => [
'level1' => 'System',
'level2' => 'System Apps',
'level3' => 'System Env',
'info' => 'App Memory',
'name' => 'Bar'
],
2 => [
'level1' => 'System',
'level2' => 'System Apps',
'level3' => 'System Env',
'info' => 'App Details',
'name' => 'Foobar'
],
3 => [
'level1' => 'System',
'level2' => 'System Apps',
'level3' => 'System Env2',
'info' => 'App Test 2',
'name' => 'Foo1'
],
4 => [
'level1' => 'System',
'level2' => 'System Memory',
'level3' => 'Memory Test',
'info' => 'App Function',
'name' => 'bar1'
],
5 => [
'level1' => 'System',
'level2' => 'System Memory',
'level3' => 'Memory Test',
'info' => 'App Test',
'name' => 'Foobar Test'
],
];
$i = 0;
$rowspans = array();
echo '<table border="1">';
foreach($data as $index => $row) {
if($i == 0) {
echo '<tr>';
foreach (array_keys($row) as $field) {
$rowspans[$field] = 1;
echo '<th>'.$field.'</th>';
}
echo '</tr>';
}
echo '<tr>';
foreach ($row as $field => $value) {
if($rowspans[$field] == 1) {
$nextIndex = $index + 1;
while (!empty($data[$nextIndex][$field]) && $value == $data[$nextIndex][$field]) {
$nextIndex++;
$rowspans[$field]++;
}
echo '<td rowspan="'.$rowspans[$field].'">'.$value.'</td>';
} else {
$rowspans[$field]--;
}
}
echo '</tr>';
$i++;
}
echo '</table>';
}
}
?>