I’m trying to fill table with values which I’m getting from the database but when I loop though array some values are getting out of the table.
DB structure with dump:
https://www.db-fiddle.com/f/9gTDRBFnguHtagJC4YNnRg/5
Code:
$query = "SELECT * FROM monthly_report";
$result = $this->db->query($query)->result_array();
$data['data'] = $result;
<table class="table table-hover table-striped">
<thead>
<tr>
<th style="text-align:center;">Month</th>
<th style="text-align:center;">Total Register</th>
<th style="text-align:center;">January</th>
<th style="text-align:center;">February</th>
<th style="text-align:center;">March</th>
<th style="text-align:center;">April</th>
<th style="text-align:center;">May</th>
<th style="text-align:center;">June</th>
<th style="text-align:center;">July</th>
<th style="text-align:center;">August</th>
<th style="text-align:center;">September</th>
<th style="text-align:center;">October</th>
<th style="text-align:center;">November</th>
<th style="text-align:center;">December</th>
</tr>
</thead>
<tbody>
<?php
$num = 0;
for ($m = 1; $m <= 12; $m++) {
$month = date('F', mktime(0, 0, 0, $m, 1, date('Y')));
echo "<tr style='text-align:center;'>";
echo "<td>" . $month . "</td>";
$short_month_name = date('M', strtotime("2022-$m-01"));
if (isset($data[$num]['month']) && $data[$num]['month'] == $short_month_name) {
$decode_data = json_decode($data[$num]['data'], true);
$decode_isset = isset($decode_data[$short_month_name]) ? $decode_data[$short_month_name] : '';
echo "<td>" . $decode_isset . "</td>";
}
$num_1 = 0;
for ($day = 1; $day <= 12; $day++) {
$short_month_name_1 = date('M', strtotime("2022-$day-01"));
if (isset($data[$num]['month']) && $data[$num]['month'] == $short_month_name_1) {
$decode_data_1 = json_decode($data[$num]['data'], true);
$decode_isset_1 = isset($decode_data_1) ? $decode_data_1 : [];
foreach ($decode_isset_1 as $v) {
if (!empty($v)) {
echo "<td>" . $v . "</td>";
} else {
echo "<td>0</td>";
}
}
} else {
echo "<td>0</td>";
}
$num_1++;
}
echo "</tr>";
$num++;
}
?>
</tbody>
</table>
And output is:
I want to remove extra generated td (Marked in red box).

