如何使用行跨度创建表

时间:2018-10-04 11:36:59

标签: php html arrays

我从解析为数组的sql查询中检索了数据。

[D20180821] => Array
    (
        [mark01] => Array
            (
                [PARIS] => Array
                    (
                        [0] => Array
                            (
                                [event_id] => 1
                                [evnet] => OPEN_TICKET_D
                                [id] => 80
                                [time] => 2018-08-22 06:56:41
                            )

                        [1] => Array
                            (
                                [event_id] => 5
                                [evnet] => CLOSE_TICKET_D
                                [id] => 651
                                [time] => 2018-08-22 11:31:00
                            )

                        [2] => Array
                            (
                                [event_id] => 4
                                [evnet] => GO
                                [id] => 82
                                [time] => 2018-08-22 11:50:35
                            )

                    )

                [LONDON] => Array
                    (
                        [0] => Array
                            (
                                [event_id] => 1
                                [evnet] => OPEN_TICKET_D
                                [id] => 79
                                [time] => 2018-08-22 06:56:38
                            )

                        [1] => Array
                            (
                                [event_id] => 5
                                [evnet] => CLOSE_TICKET_D
                                [id] => 652
                                [time] => 2018-08-22 11:29:00
                            )

                        [2] => Array
                            (
                                [event_id] => 4
                                [evnet] => GO
                                [id] => 81
                                [time] => 2018-08-22 11:50:35
                            )

                        [3] => Array
                            (
                                [event_id] => 6
                                [evnet] => CLOSE_TICKET_R
                                [id] => 647
                                [time] => 2018-08-24 10:40:00
                            )

                    )

            )

        [rows] => 2
    )

我想在知道有行跨度的情况下创建一个表。为了准备该表,我计算了[rows]中的行跨度。

这是我要绘制的一幅画

enter image description here

我无法摆脱建立这张桌子的麻烦

非常感谢

1 个答案:

答案 0 :(得分:2)

输入数据

$tab=Array(
        'D20180821' => Array
            (
                'mark01' => Array
                    (
                        'PARIS' => Array
                            (
                                '0' => Array
                                    (
                                        'event_id' => '1',
                                        'evnet' => 'OPEN_TICKET_DEPLOY',
                                        'id' => '80',
                                        'time' => '2018-08-22 06:56:41'
                                    ),
                                '1' => Array
                                    (
                                        'event_id' => '5',
                                        'evnet' => 'CLOSE_TICKET_DEPLOY',
                                        'id' => '651',
                                        'time' => '2018-08-22 11:31:00'
                                    ),
                                '2' => Array
                                    (
                                        'event_id' => '4',
                                        'evnet' => 'POST_GO',
                                        'id' => '82',
                                        'time' => '2018-08-22 11:50:35'
                                    )
                            ),
                        'LONDON' => Array
                            (
                                '0' => Array
                                    (
                                        'event_id' => '1',
                                        'evnet' => 'OPEN_TICKET_DEPLOY',
                                        'id' => '79',
                                        'time' => '2018-08-22 06:56:38'
                                    ),
                                '1'=> Array
                                    (
                                        'event_id' => '5',
                                        'evnet' => 'CLOSE_TICKET_DEPLOY',
                                        'id' => '652',
                                        'time' => '2018-08-22 11:29:00'
                                    ),
                                '2' => Array
                                    (
                                        'event_id' => '4',
                                        'evnet' => 'POST_GO',
                                        'id' => '81',
                                        'time' => '2018-08-22 11:50:35'
                                    ),
                                '3' => Array
                                    (
                                        'event_id' => '6',
                                        'evnet' => 'CLOSE_TICKET_REMOVE',
                                        'id' => '647',
                                        'time' => '2018-08-24 10:40:00'
                                    )
                            )
                    ),

                'rows' => '2'
            )
    );

代码:(您可以在phptester.net上对其进行测试)

echo '<table border class="table table-striped">';
    echo '<tr>';
        echo '<th>Event</th>
              <th>mark</th>
              <th>Place</th>
              <th>OPEN_TICKET_D</th>
              <th>CLOSE_TICKET_D</th>
              <th>GO</th>
              <th>CLOSE_TICKET_R</th>';
    echo '</tr>';
    foreach ($tab as $k => $qal) {
        $rowspan = $qal['rows'];
        unset($qal['rows']);
        $first = true;
        foreach ($qal as $mark => $cities) {
            foreach ($cities as $city => $details) {
                echo '<tr>';
                    if ($first) {
                        echo "<td rowspan='$rowspan'>$k</td>";
                        $first = false;
                    }
                    echo "<td>$mark</td>";
                    echo "<td>$city</td>";
                    $temp = array_replace(
                                [
                                    'OPEN_TICKET_DEPLOY' => '',
                                    'CLOSE_TICKET_DEPLOY' => '',
                                    'POST_GO' => '',
                                    'CLOSE_TICKET_REMOVE' => ''
                                ],
                                array_column($details, 'time', 'evnet')
                            );
                    foreach ($temp as $val) {
                        echo "<td>$val</td>";
                    }
                echo '</tr>';
            }
        }
    }
echo '</table>';

输出:

enter image description here

在存储rows值之后,我将其从数组中删除,以便在下一个foreach循环中不对其进行迭代。

包含rowpan属性的单元格必须仅回显一次,这就是为什么我使用$first变量并检查true | false的原因。

对于最深的子数组数据,我是array_column()array_replace(),以确保所有期望的列都被表示,具有默认值并且在循环之前具有正确的顺序。在回显硬编码的键之前,也可以使用一系列isset()条件来执行此部分操作-我随意选择使用数组函数。


以下是备用语法:

echo '<table border class="table table-striped">';
    echo '<tr>';
        echo '<th>Event</th>
              <th>mark</th>
              <th>Place</th>
              <th>OPEN_TICKET_D</th>
              <th>CLOSE_TICKET_D</th>
              <th>GO</th>
              <th>CLOSE_TICKET_R</th>';
    echo '</tr>';
    foreach ($tab as $k => $qal) {
        $rowspan = $qal['rows'];
        unset($qal['rows']);
        $first = true;
        foreach ($qal as $mark => $cities) {
            foreach ($cities as $city => $details) {
                echo '<tr>';
                    if ($first) {
                        echo "<td rowspan='$rowspan'>$k</td>";
                        $first = false;
                    }
                    echo "<td>$mark</td>";
                    echo "<td>$city</td>";
                    echo '<td>' , (isset($details[0]['time']) ? $details[0]['time'] : '') , '</td>'; 
                    echo '<td>' , (isset($details[1]['time']) ? $details[1]['time'] : '') , '</td>'; 
                    echo '<td>' , (isset($details[2]['time']) ? $details[2]['time'] : '') , '</td>'; 
                    echo '<td>' , (isset($details[3]['time']) ? $details[3]['time'] : '') , '</td>'; 
                echo '</tr>';
            }
        }
    }
echo '</table>';