【问题标题】:Populate HTML table with array data使用数组数据填充 HTML 表
【发布时间】:2014-07-19 00:54:05
【问题描述】:

我需要用数组创建和填充一个表......现在在博主的帮助下是这样的:

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

结果是这样的:

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

我正在寻找这个:

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

在我的想法中,交易是:$times 应该拥有所有 $flight_details 数组的所有时间,然后我们填充,将为每个 $times 记录创建一行,这样我们就不需要内爆,只需将直接值从数组中取出。问题是要实现这个...

【问题讨论】:

    标签: php html arrays html-table


    【解决方案1】:

    如果你想要这样的格式,首先,你需要分开标题,2014-05-10 ... etc,因为它们在上面。然后你需要注意格式。关键是:每个时间段都分配给公司。考虑这个例子:

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

    【讨论】:

      【解决方案2】:

      要使结果如您所愿,您需要提供:

      <td>{explode("," $company_name)[0]}</td>
      <td>{explode("," $company_name)[1]}</td>
      

      在标题中,你需要这样写:

      <td colspan="2">{$date}</td>
      

      注意:我提供的代码只是一个伪代码。您需要编写自己的代码。我们可以为您提供指导!

      【讨论】:

      • 没错,代码一定是我写的,但是 SO 的意思是什么?帮助一些想法,这正是我正在寻找的......你提出的解决方案几乎可以工作......我的问题是为每个游戏中时光倒流创建一个新行......
      • 帮助编写代码,而不是完整地编写代码。我只是给了一个注释,因为人们不认为我的代码不起作用。别担心,没有什么冒犯的! :)
      • 正如我所说,可以,但是对于我的代码不能使用这个想法的情况......我的问题是其他的,为每次延时创建 1 行。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-18
      • 1970-01-01
      • 2021-09-03
      • 1970-01-01
      • 2017-03-18
      • 2018-04-13
      • 1970-01-01
      相关资源
      最近更新 更多