从php中的数组中的多个下拉表单

时间:2013-10-13 19:48:04

标签: php forms

我正在尝试制作两个下拉表单,其中第二个菜单中的信息会根据第一组选项中选择的内容而更改。到目前为止,我有两个下拉菜单,但我不知道如何使第二个依赖于第一个。任何帮助将非常感激。我正在使用csv文件来存储我的数据。这就是我到目前为止所做的:

<?php
$filename = ("florida.csv");
$csvfile = fopen($filename,'rb');
while(!feof($csvfile)) {
$csvarray[] = fgetcsv($csvfile);
} 

print "<select>";
foreach($csvarray as $row => $arr) {
echo '<option value="' . $row . '">' . $arr[2] . $arr[0] . '</option>';

}
print "</select>";

print "<select>";
foreach($csvarray as $row => $arr) {
echo '<option value="' . $row . '">' . $arr[1] . '</option>';
}
print"</select>";

//print_r($csvarray);

?>

这是我的数组在我的csv文件中的样子。

[0] => Array
    (
        [0] => WNDT
        [1] => 92.5 FM
        [2] => Alachua
        [3] => Ocala Broadcasting Corporation, L.L.C.
        [4] => Classic Rock
        [5] => Florida
    )

[1] => Array
    (
        [0] => WORL
        [1] => 660 AM
        [2] => Altamonte Springs
        [3] => Salem Media of Illinois, LLC
        [4] => Sports
        [5] => Florida
    )
[2] => Array
    (
        [0] => WFCT
        [1] => 105.5 FM
        [2] => Apalachicola
        [3] => Williams Communications, Inc.
        [4] => Adult Standards
        [5] => Florida
    )

[3] => Array
    (
        [0] => WOYS
        [1] => 100.5 FM
        [2] => Apalachicola
        [3] => 3G Broadcasting, Inc.
        [4] => Classic Hits
        [5] => Florida
    )

[4] => Array
    (
        [0] => WBZW
        [1] => 1520 AM
        [2] => Apopka
        [3] => Pennsylvania Media Associates, Inc.
        [4] => 
        [5] => Florida
    )

[5] => Array
    (
        [0] => WFLN
        [1] => 1480 AM
        [2] => Arcadia
        [3] => Integrity Radio of Florida LLC
        [4] => News/Talk
        [5] => Florida
    )

1 个答案:

答案 0 :(得分:0)

$state = isset($_POST['state']) ? $_POST['state'] : null;
echo "<select name='state'>";
foreach ($states as $key => $statename) {
    echo "<option value='" . $key . "'" . ($key == $state ? ' selected' : '') . ">" . $statename . "</option>";
}
echo "</select>";

if (isset($state)) {
    echo "<select name='city'>";
    $csvarray = array();
    $csvfile = fopen(strtolower(str_replace(' ', '', $states[$state])) . ".csv");
    $row = 0;
    while ($csvarray = fgetcsv($csvfile)) {
        echo '<option value="' . $row . '">' . $arr[1] . '</option>';
    }
    echo "</select>";
}