I'm newbie in development and I have trouble with this below logic.
This is my sample DB.
$data = array(3) {
$x_count = 2;
[1] => $name = 'A';
$y_count = 2;
array(2) {
[1] => $name = 'A.1';
$z_count = 2;
array(2) {
[1] => $name = 'A.1.a';
$val = 1;
[2] => $name = 'A.1.b';
$val = 1;
}
[2] => $name = 'A.2';
$z_count = 2;
array(2) {
[1] => $name = 'A.2.a';
$val = 2;
[2] => $name = 'A.2.b';
$val = 2;
}
}
[2] => $name = 'B';
$y_count = 2;
array(2) {
[1] => $name = 'B.1';
$z_count = 2;
array(2) {
[1] => $name = 'B.1.a';
$val = 3;
[2] => $name = 'B.1.b';
$val = 3;
}
[2] => $name = 'A.2';
$z_count = 2;
array(2) {
[1] => $name = 'B.2.a';
$val = 4;
[2] => $name = 'B.2.b';
$val = 4;
}
}
}
I have no idea of using foreach loop to print data into this table or caculating the number of "colspan" for each of x, y, z title.
I really hope to receive some advice. Thanks a lot!
EDIT: the x_count, y_count and z_count are variables get from DB and it's not only 2. EDIT2: my expected result looks like this:
<table>
<tr>
<th>x_name</th>
<td colspan = "7">A</td>
<td colspan = "7">B</td>
<th rowspan = "3">SUM</th>
</tr>
<tr>
<th>y_name</th>
<td colspan = "3">A.1</td>
<td colspan = "3">A.2</td>
<td rowspan = "2">SUM</td>
<td colspan = "3">B.1</td>
<td colspan = "3">B.2</td>
<td rowspan = "2">SUM</td>
</tr>
<tr>
<th>z_name</th>
<td>A.1.a</td>
<td>A.1.b</td>
<td>SUM</td>
<td>A.2.a</td>
<td>A.2.b</td>
<td>SUM</td>
<td>B.1.a</td>
<td>B.1.b</td>
<td>SUM</td>
<td>B.2.a</td>
<td>B.2.b</td>
<td>SUM</td>
</tr>
<tr>
<th>value</th>
<td>1</td>
<td>1</td>
<td>2</td>
<td>2</td>
<td>2</td>
<td>4</td>
<td>6</td>
<td>3</td>
<td>3</td>
<td>6</td>
<td>4</td>
<td>4</td>
<td>8</td>
<td>14</td>
<td>20</td>
</tr>
</table>

https://html.com/tables/rowspan-colspan/for the idea of rowspan/colspan in table