0

所以我写了这段代码 -

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

我怎样才能做到这一点?我怎样才能只显示两列?我可以通过更改文件中的表来手动执行此操作。

但是我们怎样才能根据请求的查询来改变表呢?因此,如果请求了两列,则只会显示两列。

4

7 回答 7

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>";
于 2013-10-16T18:55:49.577 回答
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>";

您可能希望使用页眉、页脚和正文标签来格式化表格,但我想您明白了。缺口

于 2013-10-16T19:03:34.623 回答
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>";
?>
于 2013-10-16T18:57:04.443 回答
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>
于 2013-10-16T18:59:18.563 回答
0

而不是呼应这个:

echo "<td>" . $row['FirstName'] . "</td>";

在 while 循环中做这样的事情:

foreach ($row as $r) {
    echo "<td>" . $r . "</td>";
}

我希望它有所帮助。

于 2013-10-16T18:54:54.743 回答
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>";
      }
    }
于 2013-10-16T18:55:48.087 回答
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>';
}
于 2013-10-16T19:16:18.133 回答