4

i'm kind off stuck in this:

mysql table : tableexample with the columns ( flightnumber, company, datearrive, timeleave, timearrive)

Where I generate a array like this:

Array[0]

 - Array[0][1] => 1000
 - Array[0][2] => Company A
 - Array[0][3] => 2014-05-10
 - Array[0][4] => 10:00:00
 - Array[0][5] => 15:00:00  

Array[1]

 - Array[1][1] => 2000
 - Array[1][2] => Company A
 - Array[1][3] => 2014-05-11
 - Array[1][4] => 10:00:00
 - Array[1][5] => 15:00:00  

Array[2]

 - Array[2][1] => 3000
 - Array[2][2] => Company B
 - Array[2][3] => 2014-05-10
 - Array[2][4] => 10:00:00
 - Array[2][5] => 15:00:00  

Array[3]

 - Array[3][1] => 4000
 - Array[3][2] => Company B
 - Array[3][3] => 2014-05-11
 - Array[3][4] => 16:00:00
 - Array[0][5] => 19:00:00  

Then I need to place this in a html table:

<p>....................| 2014-05-10|2014-05-11 |</p>
<p>10:00:00 - 15:00:00 | Company A |...........|</p>
<p>10:00:00 - 15:00:00 | Company B | Company A |</p>
<p>16:00:00 - 19:00:00 | ..........| Company B |</p>

I've already:

populate the head of the table with the dates, but now i'm stuck....

<thead>
   <?php
     echo "<tr>";
     for ($row = 0; $row <  5;) {
       $dia=$array[$row][2];
       echo "<th>Dia " . $dia . "</th>";
       $i=$row;
       for ($rownext = ($row+1); $rownext < 5;){
         if ($array[$rownext][2]==$dia){
             $i++;
         };
         $rownext++;
       };
       $row=($i+1);
      };
      echo "</tr>";
    ?>
</thead>
4
  • the great dificulty is that the table is pouplated um rows....if it was in columns that would be easier in this case Commented May 28, 2014 at 16:02
  • Show us how you populated the headers Commented May 28, 2014 at 16:03
  • I would first build a tabular array with time intervals as keys. Commented May 28, 2014 at 17:09
  • just fyi, you dont need ; after }, just } is sufficient, just a style thing :) Commented May 28, 2014 at 17:32

1 Answer 1

5

This could be a lot neater, but I've tried to keep things as separated as possible and commented throughout so you can understand how it works. This looks like it should do the trick:

$details = array(
  1 => array(
      1 => 1000,
      2 => 'Company A',
      3 => '2014-05-10',
      4 => '10:00:00',
      5 => '15:00:00',
  ),
  2 => array(
      1 => 1000,
      2 => 'Company A',
      3 => '2014-05-11',
      4 => '10:00:00',
      5 => '15:00:00',
  ),
  3 => array(
      1 => 1000,
      2 => 'Company B',
      3 => '2014-05-10',
      4 => '10:00:00',
      5 => '15:00:00',
  ),
  4 => array(
      1 => 1000,
      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;

Made a codepad with it, doesn't display as a table which is annoying, but you can copy the html output it gives you and see what it looks like.

Sign up to request clarification or add additional context in comments.

2 Comments

@MLeFevre tks so much, great code :) I nedded it to place one value in each cell because i'm doing a JS onclick to open the record in other page. But I'll get it from here. Tks a lot again.
And for the abuse... @MLeFevre , i'm trying to ajust the code to repeat the line with the time if there are two companys on the same day and time, but I'm having some problems.... a litle more help?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.