使用PHP创建HTML表格

4

我很难理解这个问题:

MySQL表:tableexample,具有列(flightnumber、company、datearrive、timeleave、timearrive)

我需要生成一个像这样的数组:

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  

然后我需要将这个内容放置在一个HTML表格中:
<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>

我已经完成了以下工作:

在表头中填写了日期,但现在我卡住了……

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

巨大的困难在于表格是按行填充的......如果它是按列填充的话,在这种情况下会更容易。 - malaquias
展示一下你是如何填充表头的。 - Smith Smithy
我会首先建立一个以时间间隔为键的表格数组。 - Deadooshka
只是提醒一下,你不需要在 } 后面加上 ;,只需要一个 } 就足够了,这只是一种风格问题 :) - celeriko
1个回答

5
这可能可以更加简洁,但我尽可能地保持了内容的分离,并进行了注释,这样您就可以理解它是如何工作的。看起来这应该能起到作用:
$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;

我用它做了一个代码展示页面,但是它不能像表格一样显示,这很麻烦,但你可以复制它输出的HTML代码看看效果。


@MLeFevre 非常感谢,代码很棒 :) 我需要将一个值放在每个单元格中,因为我正在使用JS onclick打开其他页面中的记录。但我会从这里得到它。再次感谢。 - malaquias
针对滥用问题... @MLeFevre ,我正在尝试调整代码,如果同一天和时间有两个公司,则重复该行与时间,但是我遇到了一些问题...能再帮我一点吗? - malaquias

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接