Solarwin
Solarwin

Reputation: 31

How to insert drop-down-list into table cell?

Is it possible to insert drop-down-list into table cell?

Thank you!

Here is some of the code:

$sql_query="SELECT nameOfwarning FROM warnings";
$sodss=mysqli_query($d,$sql_query);
if (mysqli_num_rows($result)<1)

?>
<select name = "warnings">
        <option value="Choose one of the warnings: " </option>
        <?php
            while ($row = mysqli_fetch_array($warnings)) {
                print "<option value=";
                print $row["id_warning"];
                print ">" ;
                print $row["warning"];
                print "</option>";
            }
        ?>
        </select><br>
<?php

print "<tr><td>Insert drop down list here?: </td><td><input type=\"text\" name=\"OR HERE\"></td></tr>";

Upvotes: 2

Views: 1084

Answers (1)

Alin P.
Alin P.

Reputation: 44346

I took the liberty to also format your code. Hope you don't mind. You should do that as a general practice to make it more readable.

<?php
$sql_query = "SELECT id_warning, warning FROM warnings";
$result = mysqli_query($d, $sql_query);

$dropDownHtml = '';
if (mysqli_num_rows($result) > 0) {

    $dropDownHtml .= '<select name = "warnings">'; 
    $dropDownHtml .= '<option value="Choose one of the warnings: " </option>';
    while ($row = mysqli_fetch_array($result)) {
        $dropDownHtml .= 
            '<option value="{$row["id_warning"]}">' . 
            $row["warning"] . 
            '</option>';
    }
    $dropDownHtml .= '</select><br>';
}
?>

<tr>
    <td><?php echo $dropDownHtml; ?></td>
    <td><input type=\"text\" name=\"OR HERE\"></td>
</tr>

It looks like you skipped some of the code, because what the SQL query should return doesn't match with the results array, also there is some variable mismatching. I changed it a bit to look believable, you should make sure you adapt it back to your specific case if you don't want to post all of your code in the question.

Upvotes: 1

Related Questions