1

I need to translate my database result into a nice HTML table by using dimensional array. I know that this is a very conditional situation.

I am using CodeIgniter and already have some data. I tried to extract the database result and make new arrays format. Now I am confused on how to format it nicely into HTML table.

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

And here the result using 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>

I want the HTML table looks like below :

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 |
...

I don't know how to loop through the arrays to create a table like above. If someone here could point me in the right direction, would be much appreciated. Thank you.

MSI
  • 165
  • 10
  • could you provide me with an export of the sql table with structure + data so I can test? – Alex May 25 '19 at 00:35
  • @Alex Sure. You can download it [here](https://www.sendspace.com/file/yjho4f) . I have 2 tables that joined together, ```msi_class``` and ```msi_class_schedule``` – MSI May 25 '19 at 08:18

2 Answers2

1

This works for me:

<?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

Result

I hope this helps!

Bruno Leveque
  • 2,647
  • 2
  • 23
  • 33
  • 1
    The problem with this solution is it assumes that E-1 has all the times. Assume that E3 has 12-1. It will not output. Further, what if the dataset has an E4 or E5. I realize that OPs dataset suggests otherwise, but I can imagine down the line this becoming an issue. *This solution will only work for this particular dataset*. – Alex May 25 '19 at 00:37
  • 1
    You're totally right @Alex, I've just updated my solution to make it 100% dynamic. It will now work whatever the number of classes, times or mentors is. It will also work if a specific class is missing a time-slot. Thank you. – Bruno Leveque May 25 '19 at 00:53
  • @BrunoLeveque If I want to add the ```room``` name, where can I put in the array? I want to place it after ```mentor_code``` like so ```TPA-1 (A)```. I tested your codes and it works. Just want to add ```room``` after ```mentor_code```. Thank you. – MSI May 25 '19 at 08:22
  • Thank you for your codes and time, really appreciated!. I really want to accept both yours and Alex :) but, yeah, I choose @Alex just of the way building arrays it simpler for me. Cheers! – MSI May 26 '19 at 21:23
1

This will work (tested with your example database).

// 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>';
    }
Alex
  • 9,215
  • 8
  • 39
  • 82
  • I will try out your codes. I already put the link of sql file in the comment above. – MSI May 25 '19 at 08:27
  • alright. well i've updated my answer as there was a minor mistake. it works great now. – Alex May 26 '19 at 04:40
  • I tested your codes and its working fine. I think I'm gonna accept yours because the way of building the array is simpler. Thank you for your time, really appreciated! – MSI May 26 '19 at 21:16