用mysql选择国家html

时间:2012-04-12 15:03:17

标签: php mysql option

所以我有这样的选项国家,比如这个网站http://www.mediacollege.com/internet/samples/html/country-list.html,我在一个表mysql中有一个字段国家。我想在mysql中读取字段country,并在选项中选择与mysql中的字段匹配的国家。

我在mysql数据库中读取没有问题,我的问题是如何在一个巨大的列表中选择正确的选项国家。逐个变得不切实际

2 个答案:

答案 0 :(得分:1)

你应该有一个循环,你可以通过所有选项...比如

foreach($countries as $key=>$value){
 echo '<option value="'.$key.'" '.($key == $selected_country ? 'selected=selected':'').'>'.$value.'</option>';
}

如果您使用多个选择,则使用in_array

而不是比较

答案 1 :(得分:0)

<?php

$Countries = array(
"AF"    =>  "Afghanistan",
"AL"    =>  "Albania",
"DZ"    =>  "Algeria",
"AS"    =>  "American Samoa",
"AD"    =>  "Andorra",
"AG"    =>  "Angola",
"AI"    =>  "Anguilla",
"AG"    =>  "Antigua &amp; Barbuda",
"AR"    =>  "Argentina",
"AA"    =>  "Armenia",
"AW"    =>  "Aruba",
"AU"    =>  "Australia",
"AT"    =>  "Austria",
"AZ"    =>  "Azerbaijan",
"BS"    =>  "Bahamas",
"BH"    =>  "Bahrain",
"BD"    =>  "Bangladesh",
"BB"    =>  "Barbados",
"BY"    =>  "Belarus",
"BE"    =>  "Belgium",
"BZ"    =>  "Belize",
"BJ"    =>  "Benin",
"BM"    =>  "Bermuda",
"BT"    =>  "Bhutan",
"BO"    =>  "Bolivia",
"BL"    =>  "Bonaire",
"BA"    =>  "Bosnia &amp; Herzegovina",
"BW"    =>  "Botswana",
"BR"    =>  "Brazil",
"BC"    =>  "British Indian Ocean Territory",
"BN"    =>  "Brunei",
"BG"    =>  "Bulgaria",
"BF"    =>  "Burkina Faso",
"BI"    =>  "Burundi",
"KH"    =>  "Cambodia",
"CM"    =>  "Cameroon",
"CA"    =>  "Canada",
"IC"    =>  "Canary Islands",
"CV"    =>  "Cape Verde",
"KY"    =>  "Cayman Islands",
"CF"    =>  "Central African Republic",
"TD"    =>  "Chad",
"CD"    =>  "Channel Islands",
"CL"    =>  "Chile",
"CN"    =>  "China",
"CI"    =>  "Christmas Island",
"CS"    =>  "Cocos Island",
"CO"    =>  "Colombia",
"CC"    =>  "Comoros",
"CG"    =>  "Congo",
"CK"    =>  "Cook Islands",
"CR"    =>  "Costa Rica",
"CT"    =>  "Cote D'Ivoire",
"HR"    =>  "Croatia",
"CU"    =>  "Cuba",
"CB"    =>  "Curacao",
"CY"    =>  "Cyprus",
"CZ"    =>  "Czech Republic",
"DK"    =>  "Denmark",
"DJ"    =>  "Djibouti",
"DM"    =>  "Dominica",
"DO"    =>  "Dominican Republic",
"TM"    =>  "East Timor",
"EC"    =>  "Ecuador",
"EG"    =>  "Egypt",
"SV"    =>  "El Salvador",
"GQ"    =>  "Equatorial Guinea",
"ER"    =>  "Eritrea",
"EE"    =>  "Estonia",
"ET"    =>  "Ethiopia",
"FA"    =>  "Falkland Islands",
"FO"    =>  "Faroe Islands",
"FJ"    =>  "Fiji",
"FI"    =>  "Finland",
"FR"    =>  "France",
"GF"    =>  "French Guiana",
"PF"    =>  "French Polynesia",
"FS"    =>  "French Southern Territory",
"GA"    =>  "Gabon",
"GM"    =>  "Gambia",
"GE"    =>  "Georgia",
"DE"    =>  "Germany",
"GH"    =>  "Ghana",
"GI"    =>  "Gibraltar",
"GB"    =>  "Great Britain",
"GR"    =>  "Greece",
"GL"    =>  "Greenland",
"GD"    =>  "Grenada",
"GP"    =>  "Guadeloupe",
"GU"    =>  "Guam",
"GT"    =>  "Guatemala",
"GN"    =>  "Guinea",
"GY"    =>  "Guyana",
"HT"    =>  "Haiti",
"HW"    =>  "Hawaii",
"HN"    =>  "Honduras",
"HK"    =>  "Hong Kong",
"HU"    =>  "Hungary",
"IS"    =>  "Iceland",
"IN"    =>  "India",
"ID"    =>  "Indonesia",
"IA"    =>  "Iran",
"IQ"    =>  "Iraq",
"IE"    =>  "Ireland",
"IM"    =>  "Isle of Man",
"IL"    =>  "Israel",
"IT"    =>  "Italy",
"JM"    =>  "Jamaica",
"JP"    =>  "Japan",
"JO"    =>  "Jordan",
"KZ"    =>  "Kazakhstan",
"KE"    =>  "Kenya",
"KI"    =>  "Kiribati",
"NK"    =>  "Korea North",
"KR"    =>  "Korea South",
"KW"    =>  "Kuwait",
"KG"    =>  "Kyrgyzstan",
"LA"    =>  "Laos",
"LV"    =>  "Latvia",
"LB"    =>  "Lebanon",
"LS"    =>  "Lesotho",
"LR"    =>  "Liberia",
"LY"    =>  "Libya",
"LI"    =>  "Liechtenstein",
"LT"    =>  "Lithuania",
"LU"    =>  "Luxembourg",
"MO"    =>  "Macau",
"MK"    =>  "Macedonia",
"MG"    =>  "Madagascar",
"MY"    =>  "Malaysia",
"MW"    =>  "Malawi",
"MV"    =>  "Maldives",
"ML"    =>  "Mali",
"MT"    =>  "Malta",
"MH"    =>  "Marshall Islands",
"MQ"    =>  "Martinique",
"MR"    =>  "Mauritania",
"MU"    =>  "Mauritius",
"ME"    =>  "Mayotte",
"MX"    =>  "Mexico",
"MI"    =>  "Midway Islands",
"MD"    =>  "Moldova",
"MC"    =>  "Monaco",
"MN"    =>  "Mongolia",
"MS"    =>  "Montserrat",
"MA"    =>  "Morocco",
"MZ"    =>  "Mozambique",
"MM"    =>  "Myanmar",
"NA"    =>  "Nambia",
"NU"    =>  "Nauru",
"NP"    =>  "Nepal",
"AN"    =>  "Netherland Antilles",
"NL"    =>  "Netherlands",
"NV"    =>  "Nevis",
"NC"    =>  "New Caledonia",
"NZ"    =>  "New Zealand",
"NI"    =>  "Nicaragua",
"NE"    =>  "Niger",
"NG"    =>  "Nigeria",
"NW"    =>  "Niue",
"NF"    =>  "Norfolk Island",
"NO"    =>  "Norway",
"OM"    =>  "Oman",
"PK"    =>  "Pakistan",
"PW"    =>  "Palau Island",
"PS"    =>  "Palestine",
"PA"    =>  "Panama",
"PG"    =>  "Papua New Guinea",
"PY"    =>  "Paraguay",
"PE"    =>  "Peru",
"PH"    =>  "Philippines",
"PO"    =>  "Pitcairn Island",
"PL"    =>  "Poland",
"PT"    =>  "Portugal",
"PR"    =>  "Puerto Rico",
"QA"    =>  "Qatar",
"RE"    =>  "Reunion",
"RO"    =>  "Romania",
"RU"    =>  "Russia",
"RW"    =>  "Rwanda",
"NT"    =>  "St Barthelemy",
"EU"    =>  "St Eustatius",
"HE"    =>  "St Helena",
"KN"    =>  "St Kitts-Nevis",
"LC"    =>  "St Lucia",
"MB"    =>  "St Maarten",
"PM"    =>  "St Pierre &amp; Miquelon",
"VC"    =>  "St Vincent &amp; Grenadines",
"SP"    =>  "Saipan",
"SO"    =>  "Samoa",
"AS"    =>  "Samoa American",
"SM"    =>  "San Marino",
"ST"    =>  "Sao Tome &amp; Principe",
"SA"    =>  "Saudi Arabia",
"SN"    =>  "Senegal",
"SC"    =>  "Seychelles",
//"S0"  =>  "Serbia",
"SL"    =>  "Sierra Leone",
"SG"    =>  "Singapore",
"SK"    =>  "Slovakia",
"SI"    =>  "Slovenia",
"SB"    =>  "Solomon Islands",
"OI"    =>  "Somalia",
"ZA"    =>  "South Africa",
"ES"    =>  "Spain",
"LK"    =>  "Sri Lanka",
"SD"    =>  "Sudan",
"SR"    =>  "Suriname",
"SZ"    =>  "Swaziland",
"SE"    =>  "Sweden",
"CH"    =>  "Switzerland",
"SY"    =>  "Syria",
"TA"    =>  "Tahiti",
"TW"    =>  "Taiwan",
"TJ"    =>  "Tajikistan",
"TZ"    =>  "Tanzania",
"TH"    =>  "Thailand",
"TG"    =>  "Togo",
"TK"    =>  "Tokelau",
"TO"    =>  "Tonga",
"TT"    =>  "Trinidad &amp; Tobago",
"TN"    =>  "Tunisia",
"TR"    =>  "Turkey",
"TU"    =>  "Turkmenistan",
"TC"    =>  "Turks &amp; Caicos Is",
"TV"    =>  "Tuvalu",
"UG"    =>  "Uganda",
"UA"    =>  "Ukraine",
"AE"    =>  "United Arab Emirates",
"GB"    =>  "United Kingdom",
"US"    =>  "United States of America",
"UY"    =>  "Uruguay",
"UZ"    =>  "Uzbekistan",
"VU"    =>  "Vanuatu",
"VA"    =>  "Vatican City State",
"VE"    =>  "Venezuela",
"VN"    =>  "Vietnam",
"VB"    =>  "Virgin Islands (Brit)",
"VA"    =>  "Virgin Islands (USA)",
"WF"    =>  "Wallis &amp; Futana Is",
"YE"    =>  "Yemen",
"ZR"    =>  "Zaire",
"ZM"    =>  "Zambia",
"ZW"    =>  "Zimbabwe"
);

这是我之前制作的一个 - 您可以使用它来生成下拉列表,然后在从数据库中读取时,您可以使用2个字母的国家/地区代码作为此键的关键:

$Countries[$mysqlCountryCode];

获取完整的国家/地区名称。

它绝对不是 - 但你必须将数据存储在某个地方,无论是在mysql表还是数组中。