如何使用optgroup生成动态选择框

时间:2012-11-21 08:53:16

标签: php sql drop-down-menu

嗨,我有这个mysql表。

CREATE TABLE IF NOT EXISTS `selling_counties` (
  `county_id` int(11) NOT NULL auto_increment,
  `country` varchar(20) collate utf8_unicode_ci NOT NULL,
  `county_name` varchar(25) collate utf8_unicode_ci NOT NULL,
  `datecreated` datetime NOT NULL,
  `datemodified` datetime NOT NULL,
  PRIMARY KEY  (`county_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=97 ;

我需要一个带有optgroup的选择框,其中country将是,而county_name将是s 比如

<select name ="county">
    <optgroup label="England">
        <option>South Yorkshire</option>
        <option>Staffordshire</option>
        <option>Suffolk</option>
        <option>Surrey</option>
        <option>Tyne and Wear</option>
        <option>Warwickshire</option>
        <option>West Midlands</option>
        <option>West Sussex</option>
        <option>West Yorkshire</option>
        <option>Wiltshire</option>
        <option>Worcestershire</option>
    </optgroup>
    <optgroup label="Wales">
        <option>Clwyd</option>
        <option>Dyfed</option>
        <option>Gwent</option>
        <option>Gwynedd</option>
        <option>Mid Glamorgan</option>
        <option>Powys</option>
        <option>South Glamorgan</option>
        <option>West Glamorgan</option>
    </optgroup>
    <optgroup label="Scotland">
        <option>Aberdeenshire</option>
        <option>Angus</option>
        <option>Argyll</option>
        <option>Selkirkshire</option>
        <option>Shetland</option>
        <option>Stirlingshire</option>
        <option>Sutherland</option>
        <option>West Lothian</option>
        <option>Wigtownshire</option>
    </optgroup>
    <optgroup label="Northern Ireland">
        <option>Antrim</option>
        <option>Armagh</option>
        <option>Down</option>
        <option>Fermanagh</option>
        <option>Londonderry</option>
        <option>Tyrone</option>
    </optgroup>
</select>

我已经尝试过此PHP: Dynamic Drop down with optgroup 但它对我来说有点复杂,因为我是php的新手,而且它来自2个单独的表,而我的来自同一个表。

任何帮助都会非常明显。

2 个答案:

答案 0 :(得分:2)

创建一个这样的新数组,以国家/地区为键,县数组为每个值。假设$rows是原始表行的数组。

<?php

$countries = array();

foreach($rows as $row)
{
    if(isset($countries[$row['country']])) // <-- edit, was missing a ]
    {
        $contries[$row['country']][] = $row['county_name'];
    }
    else
    {
        $countries[$row['country']] = array($row['county_name']);
    }
}

?>

<select name ="county">
    <?php foreach($countries as $country => $counties): ?>

        <optgroup label="<?php echo htmlentities($country); ?>">

            <?php foreach($counties as $county): ?>

                <option><?php echo htmlentities($county); ?></option>

            <?php endforeach; ?>

        </optgroup>

    <?php endforeach; ?>
</select>

答案 1 :(得分:0)

您需要从表中选择值

$mysqli = new mysqli($host, $user, $passwd, $database);
$result = $mysqli->query('select county_id, country, county_name from selling_counties');
$countries = array();
while ($row = $result->fetch_assoc()) {
    $country = $row['country'];
    if (!array_key_exists($country, $countries))
        $countries[$country] = array();

    $countries[$country][] = array($row['county_id'], $row['county']);
}

并将它们放在你的html选择中。

更新以在county_id中添加<option>

<option value="<?php echo htmlentities($county[0]); ?>"><?php echo htmlentities($county[1]); ?></option>