简体   繁体   English

从多维数组创建多个HTML表

[英]Creating multiple HTML table from multidimensional arrays

I am trying to create multiple HTML tables from multidimensional array. 我正在尝试从多维数组创建多个HTML表。 I already built an array but I don't have any idea on how to translate that array into more than one HTML tables. 我已经建立了一个数组,但是对如何将该数组转换为多个HTML表一无所知。

I want to separate the tables based on the time and class name that opens in the current day. 我想根据当天打开的时间和班级名称来分开表格。

To make things clear, here is my code: 为了清楚起见,这是我的代码:

$title    = [];
$table_rows  = [];

// build table arrays
foreach ($schedule AS $row)
{
    $title[]    = $row->time.' - '.$row->class_name;
    $table_rows[$row->time][$row->class_name][] = ['student_id' => $row->student_id, 'mentor_code' => $row->mentor_code, 'student_name' => $row->student_name];
}

// $title array (using array_unique($title) to removes duplicate values
Array
(
    [0] => 07:30-08:30 - E-1
    [1] => 08:30-09:30 - E-1
    [2] => 10:00-11:00 - E-1
    [3] => 11:00-12:00 - E-1
    [12] => 07:30-08:30 - E-2
    [13] => 08:30-09:30 - E-2
)

// $table_rows array
Array
(
    [07:30-08:30] => Array
        (
            [E-1] => Array
                (
                    [0] => Array
                        (
                            [student_id] => 1012836001
                            [mentor_code] => TPA-1
                            [student_name] => Vanessa
                        )

                    [1] => Array
                        (
                            [student_id] => 1012836002
                            [mentor_code] => TPA-1
                            [student_name] => Kesya
                        )

                    [2] => Array
                        (
                            [student_id] => 3012836003
                            [mentor_code] => TPA-1
                            [student_name] => Charissa
                        )
                )

            [E-2] => Array
                (
                    [0] => Array
                        (
                            [student_id] => 1012836004
                            [mentor_code] => FIS-1
                            [student_name] => Drex
                        )
                    [1] => Array
                        (
                            [student_id] => 3012836005
                            [mentor_code] => FIS-1
                            [student_name] => Vulcano
                        )
                )
        )

    [08:30-09:30] => Array
        (
            [E-1] => Array
                (
                    [0] => Array
                        (
                            [student_id] => 1012836001
                            [mentor_code] => TPA-1
                            [student_name] => Vanessa
                        )
                    [1] => Array
                        (
                            [student_id] => 1012836002
                            [mentor_code] => TPA-1
                            [student_name] => Kesya
                        )
                    [2] => Array
                        (
                            [student_id] => 3012836003
                            [mentor_code] => TPA-1
                            [student_name] => Charissa
                        )
                )

            [E-2] => Array
                (
                    [0] => Array
                        (
                            [student_id] => 1012836004
                            [mentor_code] => FIS-1
                            [student_name] => Drex
                        )
                    [1] => Array
                        (
                            [student_id] => 3012836005
                            [mentor_code] => FIS-1
                            [student_name] => Vulcano
                        )
                )
        )

    [10:00-11:00] => Array
        (
            [E-1] => Array
                (
                    [0] => Array
                        (
                            [student_id] => 1012836001
                            [mentor_code] => FIS-1
                            [student_name] => Vanessa
                        )
                    [1] => Array
                        (
                            [student_id] => 1012836002
                            [mentor_code] => FIS-1
                            [student_name] => Kesya
                        )
                    [2] => Array
                        (
                            [student_id] => 3012836003
                            [mentor_code] => FIS-1
                            [student_name] => Charissa
                        )
                )
        )

    [11:00-12:00] => Array
        (
            [E-1] => Array
                (
                    [0] => Array
                        (
                            [student_id] => 1012836001
                            [mentor_code] => FIS-1
                            [student_name] => Vanessa
                        )
                    [1] => Array
                        (
                            [student_id] => 1012836002
                            [mentor_code] => FIS-1
                            [student_name] => Kesya
                        )
                    [2] => Array
                        (
                            [student_id] => 3012836003
                            [mentor_code] => FIS-1
                            [student_name] => Charissa
                        )
                )
        )
)

From the array, I am going to create tables like so: 从数组中,我将创建如下表:

Time : 07:30-08:30    TPA-1    Class : E-1
|------------|--------------|------------|
|Student ID  | Student Name | Status     |
|------------|--------------|------------|
|1012836001  | Vanessa      | Check None |
|1012836002  | Kesya        | Check None |
|3012836003  | Charissa     | Check None |
|------------|--------------|------------|

Time : 08:30-09:30    TPA-1    Class : E-1
|------------|--------------|------------|
|Student ID  | Student Name | Status     |
|------------|--------------|------------|
|1012836001  | Vanessa      | Check None |
|1012836002  | Kesya        | Check None |
|3012836003  | Charissa     | Check None |
|------------|--------------|------------|

Time : 07:30-08:30     FIS-1   Class : E-2
|------------|--------------|------------|
|Student ID  | Student Name | Status     |
|------------|--------------|------------|
|1012836004  | Drex         | Check None |
|3012836005  | Vulcano      | Check None |
|------------|--------------|------------|

Time : 08:30-09:30     FIS-1   Class : E-2
|------------|--------------|------------|
|Student ID  | Student Name | Status     |
|------------|--------------|------------|
|1012836004  | Drex         | Check None |
|3012836005  | Vulcano      | Check None |
|------------|--------------|------------|

Time : 10:00-11:00    FIS-1    Class : E-1
|------------|--------------|------------|
|Student ID  | Student Name | Status     |
|------------|--------------|------------|
|1012836001  | Vanessa      | Check None |
|1012836002  | Kesya        | Check None |
|3012836003  | Charissa     | Check None |
|------------|--------------|------------|

Time : 11:00-12:00    FIS-1    Class : E-1
|------------|--------------|------------|
|Student ID  | Student Name | Status     |
|------------|--------------|------------|
|1012836001  | Vanessa      | Check None |
|1012836002  | Kesya        | Check None |
|3012836003  | Charissa     | Check None |
|------------|--------------|------------|

Can I produce the results based on my array? 我可以根据阵列产生结果吗? I really need help. 我真的需要帮助 If someone here can point me to the right direction, would be much appreciated. 如果有人可以将我指出正确的方向,将不胜感激。 Thank you. 谢谢。

You can simplify this by making your array less multidimensional . 您可以通过减少数组的多维性来简化此过程。

Group your schedule rows by the full title: 将计划行按完整标题分组:

foreach ($schedule as $row)
{
    $title = "Time : $row->time   $row->mentor_code   Class : $row->class_name";
    $tables[$title][] = $row;
}

Then you'll have an array of tables indexed by title. 然后,您将获得一个按标题索引的表数组。 From that point it's much simpler to output each table. 从这一点来看,输出每个表要简单得多。

foreach ($tables as $title => $table) {
    echo $title;
    echo '<table>
            <thead>
              <tr>
                <th>Student ID</th>
                <th>Student Name</th>
                <th>Status</th>
              </tr>
            </thead>
            <tbody>';
    foreach ($table as $row) {
        echo "<tr>
                <td>{$row->student_id}</td>
                <td>{$row->student_name}</td>
                <td>Check None</td>
              </tr>"
    }
    echo '</tbody>
          </table>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM