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.

7 Answers 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>";
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of echoing this:

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

Do something like this WITHIN the while loop:

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

I hope it helps.

Comments

0

I suppose you could do somthing like [example, not working code]..

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

Comments

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

Comments

0

Create an array that holds a list of column names and their respective display names. You can then dynamically pluck columns from that list and query/loop them like:

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

Comments

0

you could always fetch_all the rows then loop over each element.

If you get all your rows with keys h then you could do something like this:

  $tableStr="<table>";
  foreach($tableData as $h=>$row){
         $tableStr .="<tr><td>" . $h . "</td>";

     foreach($row as $elem){
        $tableStr .= "<td>" . $elem . "</td>";
     }
     $tableStr .= "</tr>";
  }
  $tableStr .= "</table>";

You may wish to format the table with header, footer and body tags but I think you get the idea. Nick

Comments

0

You can simply build everything dynamically according to what was selected to be fetch from the database. Exemple:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.