簡體   English   中英

如何使用optgroup生成動態選擇框

[英]How to generate dynamic selectbox with optgroup

嗨,我有這個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的selectbox,其中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:使用optgroup進行動態下拉,但是對我來說有點復雜,因為我是php的新手,而且它來自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>

您需要從表中選擇值

$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 select中。

更新以在<option>添加county_id

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM