如何生成与OPTGROUP动态选择框(How to generate dynamic selectb

2019-08-03 00:48发布

嗨,我有这个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选择框,其中国家将会和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:动态下拉框OPTGROUP但它是有点复杂,对我来说我是新手用PHP,而且它是由2个独立的表来和我来自同一个表来。

任何帮助将是非常可观的。

Answer 1:

您需要从表中选择值

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


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


文章来源: How to generate dynamic selectbox with optgroup