所以我写了这段代码 -
$result = mysqli_query($con,"SELECT * FROM Appointments");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Address</th>
<th>City</th>
<th>PIN</th>
<th>Mobile</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Address'] . "</td>";
echo "<td>" . $row['City'] . "</td>";
echo "<td>" . $row['PIN'] . "</td>";
echo "<td>" . $row['Mobile'] . "</td>";
echo "</tr>";
}
echo "</table>";
但如果我想执行 -
SELECT FirstName, Address FROM Appointments WHERE LastName='Something'
我该怎么做?我怎样才能显示两列? 我可以通过更改文件中的表来手动完成。
但是我们如何根据请求的查询更改表? 因此,如果请求两列,则只显示两列。
答案 0 :(得分:1)
$result = mysqli_query($con,"SELECT FirstName, Address FROM Appointments WHERE LastName='Something'");
echo "<table border='1'>";
while($row = mysqli_fetch_row($result)) {
echo "<tr><td>$row[0]</td><td>$row[1]</td></tr>"; // index 0 being FirstName and index 1 being address.
}
echo "</table>";
答案 1 :(得分:0)
而不是回应这个:
echo "<td>" . $row['FirstName'] . "</td>";
在while循环中做这样的事情:
foreach ($row as $r) {
echo "<td>" . $r . "</td>";
}
我希望它有所帮助。
答案 2 :(得分:0)
我想你可以做一些像[例子,不工作的代码] ......
$i = 0;
while($row = mysqli_fetch_array($result))
{
if($i == 0);
{
foreach($row as $key=>$value)
{
echo "<th>".$key."</th>";
}
$i = 1;
}
else
{
foreach($row as $key=>$value)
{
echo "<td>".$value."</td>";
}
}
答案 3 :(得分:0)
<?php
$result = mysqli_query($con, "SELECT * FROM Appointments");
echo "<table border='1'>";
$first = true;
while ($row = mysqli_fetch_array($result)) {
if ($first) {
$head = '';
$body = '';
foreach ($row as $key => $item) {
$body .= "<td>" . $item . "</td>";
$head .= "<td>" . $key . "</td>";
}
$first = false;
echo "<tr>".$head . "</tr>";
echo "<tr>".$body . "</tr>";
} else {
echo "<tr>";
foreach ($row as $item) {
echo "<td>" . $item . "</td>";
}
echo "</tr>";
}
}
echo "</table>";
?>
答案 4 :(得分:0)
创建一个包含列名列表及其各自显示名称的数组。然后,您可以动态地从该列表中提取列并查询/循环它们,如:
// Key = display name, value = column name
$arr = array('Firstname' => 'FirstName', 'Address' => 'Address');
$result = mysqli_query($con,"SELECT ".implode(', ', $arr)." FROM Appointments");
?>
<table border='1'>
<tr>
<?php
foreach ( $arr as $k => $v ) {
?>
<th><?php echo $k ?></th>
<?php
}
?>
</tr>
<?php
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<?php
foreach ( $arr as $k => $v ) {
?>
<td><?php echo $row[$v] ?></td>
<?php
}
?>
</tr>
<?php
}
?>
</table>
答案 5 :(得分:0)
你总是可以fetch_all这些行然后遍历每个元素。
如果你用钥匙h获得所有行,那么你可以这样做:
$tableStr="<table>";
foreach($tableData as $h=>$row){
$tableStr .="<tr><td>" . $h . "</td>";
foreach($row as $elem){
$tableStr .= "<td>" . $elem . "</td>";
}
$tableStr .= "</tr>";
}
$tableStr .= "</table>";
您可能希望使用页眉,页脚和正文标记格式化表格,但我认为您可以理解。尼克
答案 6 :(得分:0)
您可以根据从数据库中提取的内容动态构建所有内容。例:
$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>';
}