malaquias malaquias - 1 month ago 15
PHP Question

Populate HTML table with array data

I need to create and populate a table with a array... for now with some help from the bloggers goes like this:

<?php
$details = array(
1 => array(
1 => 1000,
2 => 'Company A',
3 => '2014-05-10',
4 => '10:00:00',
5 => '15:00:00',
),
2 => array(
1 => 2000,
2 => 'Company A',
3 => '2014-05-11',
4 => '10:00:00',
5 => '15:00:00',
),
3 => array(
1 => 3000,
2 => 'Company B',
3 => '2014-05-10',
4 => '10:00:00',
5 => '15:00:00',
),
4 => array(
1 => 4000,
2 => 'Company B',
3 => '2014-05-11',
4 => '16:00:00',
5 => '19:00:00',
)
);

// Format our data into something we can use more easily
$flight_dates = array();
$times = array();
$dates = array();
foreach ($details as $flight_details) {
$company_name = $flight_details[2];
$date = $flight_details[3];
$time = $flight_details[4] . ' - ' . $flight_details[5];

// Keep a unique record of each date, and the times of flights
$dates[$date] = 1;
$times[$time] = 1;

// Record which date/time each company is flying
$flight_dates[$date][$time][] = $company_name;
}

// Create the table header
$html = '<table border="1">';
$html .= '<thead>';
$html .= '<tr>';
$html .= '<th>&nbsp;</th>';
foreach ($dates as $date => $value1) {
$html .= '<th>' . $date . '</th>';
}
$html .= '</tr>';

// Create the rows in the table
foreach ($times as $time => $value1) { // Loop through each flight time
$html .= '<tr>';
$html .= '<td>' . $time . '</td>'; // The first column is always the time
foreach ($dates as $date => $value2) { // Loop through each date
if (!empty($flight_dates[$date][$time])) { // Check if flights exist at the current time
$html .= '<td>' . implode(', ', $flight_dates[$date][$time]) . '</td>'; // List companies
} else { // No flights
$html .= '<td>&nbsp;</td>'; // Leave cell blank
}
}
$html .= '</tr>';
}
$html .= '</table>';

echo $html;
?>


The result is this:

|....................|.. 2014-05-10 ........| 2014-05-11|
|10:00:00 - 15:00:00 | Company A, Company B | Company A |
|16:00:00 - 19:00:00 |......................| Company B |


I'm looking to get this:

|....................|2014-05-10 | 2014-05-11|
|10:00:00 - 15:00:00 | Company A | Company A |
|10:00:00 - 15:00:00 | Company B |...........|
|16:00:00 - 19:00:00 |...........| Company B |


In my idea the deal is:
$times
should have all the times for all the
$flight_details
array, and then we populate, will create a row for each
$times
record, that way we don't need to implode, just put the direct value off the array. The problem is to achieve this...

Answer

If you want such format, first off, you need to separate the headers, 2014-05-10 ... etc since they are above. Then you need to take care of the format. The key is: every time slot assign it to the company. Consider this example:

<?php

$details = array( 1 => array( 1 => 1000, 2 => 'Company A', 3 => '2014-05-10', 4 => '10:00:00', 5 => '15:00:00', ), 2 => array( 1 => 2000, 2 => 'Company A', 3 => '2014-05-11', 4 => '10:00:00', 5 => '15:00:00', ), 3 => array( 1 => 3000, 2 => 'Company B', 3 => '2014-05-10', 4 => '10:00:00', 5 => '15:00:00', ), 4 => array( 1 => 4000, 2 => 'Company B', 3 => '2014-05-11', 4 => '16:00:00', 5 => '19:00:00', ), 5 => array( 1 => 5000, 2 => 'Company C', 3 => '2014-05-10', 4 => '10:00:00', 5 => '15:00:00', ), 6 => array( 1 => 5000, 2 => 'Company D', 3 => '2014-05-12', 4 => '10:00:00', 5 => '15:00:00', ),);

//properly sort the raw data
// get headers
$header_dates = array();
foreach($details as $value) {
    $header_dates[] = $value[3];
}

$header_dates = array_unique($header_dates);
// reformat main data companies
foreach($details as $key => $value) {
    $time = "$value[4] - $value[5]"; // use this as key
    foreach($header_dates as $date) {
        // simple initialization
        if(!isset($final[$value[2]][$time][$date])) $final[$value[2]][$time][$date] = '';
    }

    $final[$value[2]][$time][$value[3]] = $value[2]; 
    // then set them accordingly to their date and time
}


?>

<!-- format them inside the table -->
<table border="1" cellpadding="10">
    <!-- print the headers -->
    <thead><tr><td></td><?php foreach($header_dates as $value): ?><th><?php echo $value; ?></th><?php endforeach; ?></tr></thead>
    <tbody>
    <?php foreach($final as $key => $data): ?>
        <?php foreach($data as $index => $element): ?>
            <tr><td><?php echo $index; ?></td> <!-- loop they time -->
                <?php foreach($element as $v):?>
                    <td><?php echo $v; ?></td> <!-- values (Company A, ...) -->
                <?php endforeach; ?>
            </tr>
        <?php endforeach; ?>
    <?php endforeach; ?>
    </tbody>
</table>

Sample Output