繁体   English   中英

PHP Dropdown从SQL数据库中提取信息

[英]PHP Dropdown pulling information from SQL database

我正在尝试使用我所有的货币创建一个下拉列表,此时值为1,2和3.我需要下拉值保留为数字,但是希望选项文本说明英镑,欧元和美元相反。 我有这个代码:

<select class='dc_input' name='currency' id ='currency'>
    <?php

    $sql="SELECT l.currency
                    FROM locations l
                    GROUP BY l.currency";

        $sites = mysql_query($sql);
        while ($row = mysql_fetch_assoc($sites)) {
            echo '<option value="' .$row['currency']. '"' .$selected. '>  </option>';
        }
    ?>

</select>

这是有效的,我只是不知道要为每一个改变它。

您没有从数据库中选择数字。 完成后,将文本放在开始/结束标记之间,并将数值保留在value属性中。

echo '<option value="', htmlspecialchars($row['id']), '">',
     htmlspecialchars($row['currency']), '</option>';
<?php
$currencies = array('', 'GBP', 'EUR', 'USD');
?>
<select class='dc_input' name='currency' id ='currency'>
    <?php

    $sql="SELECT l.currency
                    FROM locations l
                    GROUP BY l.currency";

        $sites = mysql_query($sql);
        while ($row = mysql_fetch_assoc($sites)) {
            $index = $row['currency'];
            echo '<option value="' .$index. '"' .$selected. '> '.$currencies[$index].' </option>';
        }
    ?>

</select>

您还可以在数据库中存储货币ID和货币名称。 如果您开始支持3种以上的货币,那么对您来说会更容易。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM