PHP动态下拉菜单

时间:2014-07-16 10:07:33

标签: php dynamicform

我正在尝试创建一个下拉列表,其中包含由文件确定的预选值。不幸的是,我不确定我的语法是否正确。这是我到目前为止所做的:

<select id="SampleRate", name="SampleRate">

    <?php

    $controller_file = 'controllerSetup.conf';
    $comment = "#";
    $fp = fopen($controller_file, "r");
    while (!feof($fp)) {
        $line = trim(fgets($fp));
        if ($line && !ereg("^$comment", $line)) {
            $pieces = explode("=", $line);
            $option = trim($pieces[0]);
            $value = trim($pieces[1]);
            $controller_values[$option] = $value;
        }
    }
    fclose($fp);

    $sampleRates = array("48", "96", "192", "350");
    foreach ($sampleRates as $r) {
        if ($r == $controller_values["SampleRate"]) {
            echo ("<option value=$r selected>selected</option>");
        } else {
            echo ("<option value=$r>not selected</option>");
        }
    };
    ?>

</select>

不幸的是,查看浏览器中生成的html,只会生成一个“未选中”选项。如何让它生成四个选项?我试过用简单的if ($r == "48")替换foreach中的“if”语句,但无济于事。我的echo语句的语法是否正确?

0 个答案:

没有答案