如何从多维数组制作一个好的html表示表?

时间:2019-05-24 23:45:17

标签: php mysql codeigniter

我需要使用维度数组将数据库结果转换为漂亮的HTML表。我知道这是一个非常有条件的情况。

我正在使用CodeIgniter,并且已经有一些数据。我试图提取数据库结果并制作新的数组格式。现在,我对如何将其很好地格式化为HTML表感到困惑。

$schedule = [];

foreach($this->data['jadwal_kelas'] AS $row)
{
  $schedule['class_name'][$row->class_name]['time'][$row->start.'-'.$row->end] = ['room' => $row->room, 'mentor_code' => $row->mentor_code];
}

这里是使用print_r

的结果
<pre>Array
(
    [class_name] => Array
        (
            [E-1] => Array
                (
                    [time] => Array
                        (
                            [07:30:00-08:30:00] => Array
                                (
                                    [room] => A
                                    [mentor_code] => TPA-1
                                )

                            [08:30:00-09:30:00] => Array
                                (
                                    [room] => A
                                    [mentor_code] => TPA-1
                                )

                            [10:00:00-11:00:00] => Array
                                (
                                    [room] => A
                                    [mentor_code] => FIS-1
                                )

                            [11:00:00-12:00:00] => Array
                                (
                                    [room] => A
                                    [mentor_code] => FIS-1
                                )

                        )

                )

            [E-2] => Array
                (
                    [time] => Array
                        (
                            [07:30:00-08:30:00] => Array
                                (
                                    [room] => D
                                    [mentor_code] => FIS-1
                                )

                            [08:30:00-09:30:00] => Array
                                (
                                    [room] => D
                                    [mentor_code] => FIS-1
                                )

                            [10:00:00-11:00:00] => Array
                                (
                                    [room] => D
                                    [mentor_code] => BIO-1
                                )

                            [11:00:00-12:00:00] => Array
                                (
                                    [room] => D
                                    [mentor_code] => BIO-1
                                )

                        )

                )

        )

)
...
</pre>

我希望HTML表格如下所示:

Time         | E1    | E2     | E3     |
-------------|-------|--------|--------|
07:30-08:30  | TPA-1 | FIS-1  |        |
08:30-09:30  | TPA-1 | FIS-1  |        |
10:00-11:00  | FIS-1 | BIO-1  | MATH-1 |
11:00-12:00  | FIS-1 | BIO-1  | MATH-1 |
...

我不知道如何loop遍历数组以创建如上的表格。如果有人可以指出我正确的方向,将不胜感激。谢谢。

2 个答案:

答案 0 :(得分:1)

这对我有用:

<?php

$available_sections = array();
$available_times = array();
foreach ($a['class_name'] as $s => $records)
{
    $available_sections[$s] = 1;
    foreach ($records['time'] as $t => $values)
        $available_times[$t] = 1;
}
ksort($available_times);

$output = '
<table border="1" cellpadding="4" style="font-family: Arial; font-size: 12px;">
    <tr>
        <th>Time</th>';
        foreach ($available_sections as $as => $v)
            $output .= '<th>'.$as.'</th>';
$output .= '
    </tr>';

foreach ($available_times as $at => $v)
{
    $output .= '
    <tr>
        <td>'.$at.'</td>';
    foreach ($available_sections as $as => $v2)
        $output .= '<td>'.(isset($a['class_name'][$as]['time'][$at]) ? $a['class_name'][$as]['time'][$at]['mentor_code'] : '').'</td>';
    $output .= '</tr>';
}

$output .= '
</table>';

echo $output;

结果

Result

我希望这会有所帮助!

答案 1 :(得分:1)

这将起作用(在示例数据库中进行了测试)。

// database
        $this->load->database();
        $query = 'SELECT sched.room, sched.start, sched.end, sched.mentor_code, class.class_name FROM msi_class_schedule as sched LEFT JOIN msi_class as class ON class.id_class = sched.id_class';
        $res = $this->db->query($query)->result();

        // init arrays
        $headers = [];
        $times = [];

        // build table arrays
        foreach ($res AS $row) {
            // header already exists?
            if (!in_array($row->class_name, $headers)) {
                $headers[] = $row->class_name;
            }
            $times[$row->start . '-' . $row->end][$row->class_name] = ['room' => $row->room, 'mentor_code' => $row->mentor_code];
        }

        // start building table

        echo '<table>';

        echo "<th>Time</th>";

        foreach ($headers as $header) {

            echo "<th>{$header}</th>";
        }

        foreach ($times as $time => $val) {

            echo '<tr>';

            echo "<td>{$time}</td>";

            foreach ($headers as $header) {

                if (isset($times[$time][$header])) {

                    $v = $times[$time][$header]['mentor_code'];
                } else {

                    $v = '';
                }

                echo "<td>{$v}</td>";
            }

            echo '</tr>';
        }

        echo '</table>';
    }