I want to set the on a HTML dropdown menu from a php variable. I give you my code so you can see I want to do:

<?php
$html_table = '
<table border="0" cellspacing="0" cellpadding="0"><tr>';
while($arr = pg_fetch_array($result1))
{
$html_table .= "<tr><td> $arr[0] </td></tr>";                          
}
$html_table .='</tr>';
?>

<p>
<select name="db" size="1">
<option> $html_table </option>  #### <- that is my question, how to get that working
</select>
</p>

I hope you understand what I want to do. If you know about nicer ways, let me know. Cheers

有帮助吗?

解决方案

<?php
$options = '';
while($arr = pg_fetch_array($result1)) {
    $options .= '<option>'.$arr[0].'</option>';                          
}
?>

<p>
<select name="db" size="1">
    <?php echo $options; ?>
</select>
</p>

其他提示

Just replace $html_table with <?php echo $html_table; ?> and you're good to go - from the PHP point of view that is. HTML is of course invalid, as pointed out by others.

First: you didn't close your <table> tag.

Second: you can't paste<table> tag into <select>. You should put a series of <option> elements, each with one element of the array.

Third: To print PHP variable you should do like this:

<option><?php echo $html_table; ?></option>

But again: it will break your HTML, because table can't be inside select option. The best solution is:

<select name="db" size="1">
    <?php while ($arr = pg_fetch_array($result1)): ?>
        <option><?php echo $arr[0]; ?></option>
    <?php endwhile; ?>
</select>

so set your option elements like this

<option value="<?php echo $html_table; ?>"><?php echo $html_table; ?></option>

just know, an option can't be a table.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top