如何使用optgroup生成动态选择框 [英] How to generate dynamic selectbox with optgroup

查看:137
本文介绍了如何使用optgroup生成动态选择框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个mysql表。

Hi I am having this mysql table.

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
例如

and I need have a selectbox with optgroup where country will be and county_name will the s such as

<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分开表和我的来自同一张表。

I have already tried this PHP: Dynamic Drop down with optgroup but it is bit complex for me as I am newbie with php, moreover it is coming from 2 seperate tables and mine is coming from same table.

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

Any help will be highly appreciable.

推荐答案

您需要从表格中选择值

$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选择中。

and put these in your html select.

更新< option> 中添加 county_id

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

这篇关于如何使用optgroup生成动态选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆