0

So i have written this code -

$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>";

But if i want to execute -

SELECT FirstName, Address FROM Appointments WHERE LastName='Something'

How can i do that? How can i display just two columns? I can do that manually by changing the table in file.

But how can we change the table according to the query requested? So if two columns were requested then only two columns will be displayed.

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

[例、動作しないコード] のようなことができると思います..

$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

列名とそれぞれの表示名のリストを保持する配列を作成します。次に、そのリストから列を動的に抽出し、次のようにクエリ/ループできます。

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

いつでもすべての行を 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

データベースからフェッチするために選択されたものに従って、すべてを動的に構築するだけです。例:

$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 に答える