您可以根据选择从数据库中获取的内容简单地动态构建所有内容。示例:
$available_columns = array(
'FirstName',
'LastName',
'Address',
'City',
'PIN',
'Mobile'
);
$column_names = array(
'FirstName' => 'First Name',
'LastName' => 'Last Name',
'Address' => 'Address',
'City' => 'City',
'PIN' => 'PIN',
'Mobile' => 'Mobile'
);
if ($_POST && isset($_POST['condition_name']) && isset($_POST['condition_value']) && (array_search($_POST['condition_name'], $column_names) !== FALSE))
{
$columns = array();
foreach ($available_columns as $col)
if (isset($_POST[$col]))
$columns[] = $col;
echo '<table border="1"><tr>';
foreach ($columns as $col)
echo "<th>{$column_names[$col]}</th>";
echo '</tr>';
$columns = implode(', ', $columns);
$condition = mysqli_real_escape_string($_POST['condition_value']);
// $_POST['condition_name'] was already checked for validity
$res = mysqli_query("SELECT {$columns} FROM Appointments WHERE {$_POST['condition_name']} = '{$condition}'");
while ($row = mysqli_fetch_row($res))
{
echo '<tr><td>' . implode('</td><td>', $row) . '</td></tr>';
}
echo '</table>';
}
else
{
echo '<form method="POST">';
echo 'Select the columns you want:<br>';
foreach ($available_columns as $col)
echo "<input type=\"checkbox\" name=\"{$col}\" value=\"1\" checked>{$column_names[$col]}</input><br>";
echo '<br>';
echo 'Select the condition:<br>';
echo '<select name="condition_name">';
foreach ($available_columns as $col)
echo "<option value=\"{$col}\">{$column_names[$col]}</option>";
echo '</select><br>';
echo '<input type="text" name="condition_value" value="" />';
echo '<input type="submit" value="Search" />';
echo '</form>';
}