Dracula2507
Dracula2507

Reputation: 35

Print PHP array into table views with foreach

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

And I have to print like this enter image description here

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>

Upvotes: 1

Views: 302

Answers (1)

Thien Huynh
Thien Huynh

Reputation: 559

You don't need to add y_count, x_count, you can use count() function on the object $data instead.

Here is HTML's structure I created, you may take a look

For data easier to process, you can consider to use my data structure below

$data = [
    [
        "name" => 'A', // x_name
        "children" => [
            [
                "name" => 'A.1', // y_name
                "children" => [
                    [
                        "name" => 'A.1.a', // z_name
                        "value" => 1
                    ],
                ]
            ]
        ]
    ],
    ...
];

NOTE: All the code's logic below is used for your provided HTML, not for my jsFiddle's HTML

First step: Run a loop on every element of array $data for processing x_name data. Assign each row's html to variable for further use

/* Init html variable for $x_name, $y_name, $z_name, $value */
$x_html = '';
$y_html = '';
$z_html = '';
$value_html = '';
foreach ($data as $x_index => $x_data) {
    $x_children = $x_data['children'];
    $x_colspan = 1; // this variable will be sum of 1 'SUM' column and each $y_colspan
    foreach ($x_children as $y_index => $y_data) {
        $y_children = $x_data['children'];
        $y_colspan = count($y_children) + 1; // all of y's children column + SUM column

        // Update x_colspan
        $x_colspan += $y_colspan;

        // process assign html to $y_html
        $y_html .= '<td colspan = "'.$y_colspan.'">'.$y_data['name'].'</td>';
        // add SUM column
        if ($y_index == count($x_children)-1) {
            $y_html .= '<td>SUM</td>';
        }

        $sum = 0;
        foreach ($y_children as $z_index => $z_data) {
            $z_value = $z_data['value'];
            $z_html .= '<td>'.$z_data['name'].'</td>';
            // add SUM column for every looped 2 $z_data column
            if ($z_index == count($y_children)-1) {
                $z_html .= '<td>SUM</td>';
            }

            $sum += $z_value; // sum all value of current y_children
            $value_html .= '<td>'.$z_data['value'].'</td>';
        }
        // After finish assign z_html, value_html, sum it all
        $value_html .= '<td>'.$sum.'</td>'
    }


    $x_html .= '<td colspan = "'.$x_colspan.'">'.$x_data['name'].'</td>';
}

Last step: you print out all variable x_html, y_html, z_html, value_html. Merge it with the actual table html

$table_html = '
<table>
    <tr>
        <th>x_name</th>
        '.$x_html.'
        <th rowspan = "3">SUM</th>
    </tr>
    <tr>
        <th>y_name</th>
        '.$y_html.'
    </tr>
    <tr>
        <th>z_name</th>
        '.$z_html.'
    </tr>
    <tr>
        <th>z_name</th>
        '.$value_html.'
    </tr>
</table>
';

This code above may be not runable, it's just a idea. Hope it would help you

Upvotes: 1

Related Questions