【问题标题】:Set HTML dropdown options from PHP variable从 PHP 变量设置 HTML 下拉选项
【发布时间】:2013-11-01 22:50:30
【问题描述】:

我想在一个 php 变量的 HTML 下拉菜单中设置 。我给你我的代码,这样你就可以看到我想要做什么:

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

我希望你明白我想要做什么。如果您知道更好的方法,请告诉我。 干杯

【问题讨论】:

  • 您不能在&lt;option&gt; 中放置表格。只需放入一系列&lt;option&gt; 元素,每个元素都有一个数组元素。

标签: php html variables option


【解决方案1】:
<?php
$options = '';
while($arr = pg_fetch_array($result1)) {
    $options .= '<option>'.$arr[0].'</option>';                          
}
?>

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

【讨论】:

  • 什么意思?选项不显示?
  • 是的,抱歉,应该提到...下拉菜单中没有选项可查看
  • 在while 循环中使用var_dump($arr)。更新您的代码。也许没有结果?
  • 完成 - 有结果。
  • 现在可以了。我混淆了你所有的提示,但现在我明白了。谢谢
【解决方案2】:

只需将$html_table 替换为&lt;?php echo $html_table; ?&gt; 就可以了——从PHP 的角度来看就是这样。正如其他人指出的那样,HTML 当然是无效的。

【讨论】:

    【解决方案3】:

    首先:你没有关闭你的&lt;table&gt; 标签。

    第二:不能将&lt;table&gt;标签粘贴到&lt;select&gt;中。您应该放置一系列&lt;option> 元素,每个元素都有一个数组元素。

    第三:要打印 PHP 变量,你应该这样做:

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

    但又一次:它会破坏您的 HTML,因为表格不能在选择选项中。 最好的解决办法是:

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

    【讨论】:

      【解决方案4】:

      所以像这样设置你的选项元素

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

      要知道,选项不能是表格。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-17
        • 1970-01-01
        • 2014-06-28
        • 2011-02-10
        • 2010-12-14
        • 2020-02-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多