动态生成选项列表最终会重复使用optgroup标记

时间:2015-04-07 02:47:35

标签: php html mysql algorithm html-select

我知道必须有一个简单的答案:我正在尝试动态填充选择框。我在某种程度上取得了成功,但当我选择它时,它会不断重复每一个选项请检查我的代码并告诉我在做错了什么

$country_id = 'management';
$country_id = trim(mysql_escape_string($_POST["country_id"]));
<!--db details-->
$dbc2 = mysqli_connect(DB_HOST2, DB_USER2, DB_PASSWORD2, DB_NAME2) or trigger_error(mysql_error(),E_USER_ERROR);
$q = "SELECT b.institute_state, b.institute_district, b.institute_location 
FROM institutes_courses a, institutes b
WHERE a.institute_course_stream = '".$country_id."'
AND a.institute_id = b.institute_id
group by b.institute_district
ORDER BY institute_state ASC
";
$r = mysqli_query($dbc2, $q);
$institutes = array();

while(list($institute_state, $institute_district, $institute_location) = mysqli_fetch_array($r, MYSQLI_BOTH)) {
    $institutes[] = array('state' => $institute_state, 'district' => $institute_district, 'location' => $institute_location);
}//end while

echo  '<select name="state" id="drop2" required>
<option value="" id="state" selected>Please Select A Location</option>';

foreach ($institutes as $inst) {
    echo '<optgroup label="'.$inst['state'].'">';
    //echo '<optgroup label="'.$inst['state'].'">';


        echo '<option value="'.$inst['location'].'">State: '.$inst['state'].', Location: '.$inst['location'].'</option>';

        echo '</optgroup>';
    }

echo '</select>';

这给我一个结果:

<optgroup label:state>
  <option>District</option>
</optgroup>
<optgroup label:state>
  <option>District</option>
</optgroup>
<optgroup label:state2>
  <option>District</option>
</optgroup>
<optgroup label:state2>
  <option>District</option>
</optgroup>etc...

我希望它是这样的:

<optgroup label:state>
  <option>District</option>
  <option>District</option>
</optgroup>
<optgroup label:state2>
  <option>District</option>
  <option>District</option>
</optgroup>etc...

1 个答案:

答案 0 :(得分:1)

您应该验证您的$inst['state']更改,在这种情况下,我添加名为$count的控件var,将您的代码更改为:

<?php

$institutes = array();
$institutes[] = array('state' => 'delhi', 'location' => 'mg road');
$institutes[] = array('state' => 'delhi', 'location' => 'delhi_loc');
$institutes[] = array('state' => 'karnataka', 'location' => 'kannur_ayikere');
$institutes[] = array('state' => 'state1', 'location' => 'location1');
$institutes[] = array('state' => 'state1', 'location' => 'location2');
$institutes[] = array('state' => 'state1', 'location' => 'location3');
$institutes[] = array('state' => 'state2', 'location' => 'location1');
$institutes[] = array('state' => 'state3', 'location' => 'location1');
$institutes[] = array('state' => 'state3', 'location' => 'location2');

echo  '<select name="state" id="drop2" required>
<option value="" id="state" selected>Please Select A Location</option>';

$label = '';
$cant = array_count_values(array_column($institutes, 'state'));
$count = 0;

foreach ($institutes as $inst) {

    if($count == 0) {
    echo '<optgroup label="'.$inst['state'].'">';
    }
        echo '<option value="'.$inst['location'].'">State: '.$inst['state'].', Location: '.$inst['location'].'</option>';
        $count++;

    if($cant[$inst['state']] == $count) {
        echo '</optgroup>';
        $count = 0;
    }

    $label =  $inst['state'];
  }

echo '</select>';