所以我有这个选项国家,比如这个网站http://www.mediacollege.com/internet/samples/html/country-list.html,我在一个表mysql中有一个字段国家。我想读取 mysql 中的字段 country 并在选项中选择与 mysql 中的字段匹配的国家。
我在 mysql 数据库中读取没有问题,我的问题是如何在这样的巨大列表中选择正确的选项国家。让一一变得不切实际
所以我有这个选项国家,比如这个网站http://www.mediacollege.com/internet/samples/html/country-list.html,我在一个表mysql中有一个字段国家。我想读取 mysql 中的字段 country 并在选项中选择与 mysql 中的字段匹配的国家。
我在 mysql 数据库中读取没有问题,我的问题是如何在这样的巨大列表中选择正确的选项国家。让一一变得不切实际
你应该有一个循环,你可以通过所有选项..比如
foreach($countries as $key=>$value){
echo '<option value="'.$key.'" '.($key == $selected_country ? 'selected=selected':'').'>'.$value.'</option>';
}
如果您使用多项选择,则不要使用比较,而是使用in_array
<?php
$Countries = array(
"AF" => "Afghanistan",
"AL" => "Albania",
"DZ" => "Algeria",
"AS" => "American Samoa",
"AD" => "Andorra",
"AG" => "Angola",
"AI" => "Anguilla",
"AG" => "Antigua & 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 & 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 & Miquelon",
"VC" => "St Vincent & Grenadines",
"SP" => "Saipan",
"SO" => "Samoa",
"AS" => "Samoa American",
"SM" => "San Marino",
"ST" => "Sao Tome & 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 & Tobago",
"TN" => "Tunisia",
"TR" => "Turkey",
"TU" => "Turkmenistan",
"TC" => "Turks & 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 & Futana Is",
"YE" => "Yemen",
"ZR" => "Zaire",
"ZM" => "Zambia",
"ZW" => "Zimbabwe"
);
这是我之前制作的一个 - 您可以使用它来生成下拉列表,然后在从数据库中读取时,您可以使用 2 个字母的国家/地区代码作为此键:
$Countries[$mysqlCountryCode];
获取您的完整国家/地区名称。
这绝对不是优雅的——但你必须将数据存储在某个地方,无论是在 mysql 表中还是在数组中。