1

我正在尝试使用我的所有货币创建一个下拉列表,目前值为 1、2 和 3。我需要下拉列表的值保持为数字,但希望选项文本显示 GBP、EUR和美元代替。我有这个代码:

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

这行得通,我只是不知道要放入什么来使其每个都改变。

4

2 回答 2

2

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

echo '<option value="', htmlspecialchars($row['id']), '">',
     htmlspecialchars($row['currency']), '</option>';
于 2013-07-29T14:43:27.863 回答
1
<?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 种货币,对您来说会更容易。

于 2013-07-29T14:47:42.467 回答