0

我正在尝试在 PHP 中创建一个表,其中我将有一堆由表单设置的列标题,然后该表将仅显示这些列。

因此,表格可能有 4 列或 6 列,具体取决于输入的日期范围。

最好的方法是什么,因为我已经尝试了一些东西,但没有什么能接近我想要的。

这是一些代码:

echo '<table>';
echo '<tr>';
echo '<th>Last Name</td>';
echo '<th>First Name</td>';
if($column_date1 != '') { echo '<th>' . $column_date1 . '</td>'; }
if($column_date2 != '') { echo '<th>' . $column_date2 . '</td>'; }
if($column_date3 != '') { echo '<th>' . $column_date3 . '</td>'; }
if($column_date4 != '') { echo '<th>' . $column_date4 . '</td>'; }
if($column_date5 != '') { echo '<th>' . $column_date5 . '</td>'; }
echo '</tr>';

// I will have a loop in here that searches records in a database.
$mysqliFunctionResult = $mysqliFunction->query($mysqliFunctionQuery);
while($line = $mysqliFunctionResult->fetch_assoc()) {
?><tr>
    <td><?php echo $line['tabledata1']; ?></td>
    <td><?php echo $line['tabledata2']; ?></td>
    <td><?php echo $line['tabledata3']; ?></td>
    <td><?php echo $line['tabledata4']; ?></td>
    <td><?php echo $line['tabledata5']; ?></td>
  </tr>
<?php } ?>
</table>
4

3 回答 3

0

对 $_post 数据执行 foreach($key => $value)。然后使用 $key 访问您提取的数据。对于每个键,回显您要显示的相应数据。

于 2013-10-25T14:47:03.740 回答
0

也许我可能会想念你的答案,但是。这是我的尝试。

/******************************
 * get the $database with same
 * names as the $_POST 
 *****************************/

<table>
<thead>
<tr>
    <?php // make the headers 
    foreach ($_POST as $column => $inside) {
        if (!empty($column)) {
            echo "<th>" . $column . "<th>";
        }
     }   
    ?>
</tr>
</thead>
<tbody>
foreach($database, $row) {
    foreach ($_POST as $column) {
        if (!empty($column)) {
            $row[$column];
        }
     }   
}
?>
</tbody>
</table>

我只是把它放在一起,我怀疑它是否接近完美,但也许会让你知道一种方法来做到这一点。

于 2013-10-25T15:11:18.803 回答
0

如果您可以像数组一样保存后数据,则更容易:

foreach($_POST['column_date'] as $column_date)
    if("" != $column_date)
        echo '<th>' . $column_date . '</td>';

...您必须将输入字段命名为 column_date[1] 等。

于 2013-10-25T15:13:38.253 回答