PHP - UK Counties Drop Down

November 9th 2010

So, here I am, wondering how on earth I can get a function together to list UK counties in a simple method that can be used over and over again.

I've put this together and it seems to work rather well.

PHP Code:
<?php
/**
 * UK DROPDOWN LIST
 * ROGER E THOMAS November 2010
 * http://www.rogerethomas.com
 * Completely free to download and use in your scripts.
 * Call as: echo listUKCounties ($dropdown_name,$key_selected);
 *
 * $dropdown_name is the name of the final select box.
 * $key_selected is the value of the index that should be selected by default
 *              This should come in handy when dealing with incorrect address
 *              information and promting the user for more info.
 *
 *
 */
function listUKCounties ($dropdown_name,$key_selected) {
    
$county_dropdown=array(
                        
"Avon"=>"Avon",
                        
"Bedfordshire"=>"Bedfordshire",
                        
"Berkshire"=>"Berkshire",
                        
"Borders"=>"Borders",
                        
"Buckinghamshire"=>"Buckinghamshire",
                        
"Cambridgeshire"=>"Cambridgeshire",
                        
"Central"=>"Central",
                        
"Cheshire"=>"Cheshire",
                        
"Cleveland"=>"Cleveland",
                        
"Clwyd"=>"Clwyd",
                        
"Cornwall"=>"Cornwall",
                        
"County Antrim"=>"County Antrim",
                        
"County Armagh"=>"County Armagh",
                        
"County Down"=>"County Down",
                        
"County Fermanagh"=>"County Fermanagh",
                        
"County Londonderry"=>"County Londonderry",
                        
"County Tyrone"=>"County Tyrone",
                        
"Cumbria"=>"Cumbria",
                        
"Derbyshire"=>"Derbyshire",
                        
"Devon"=>"Devon",
                        
"Dorset"=>"Dorset",
                        
"Dumfries and Galloway"=>"Dumfries and Galloway",
                        
"Durham"=>"Durham",
                        
"Dyfed"=>"Dyfed",
                        
"East Sussex"=>"East Sussex",
                        
"Essex"=>"Essex",
                        
"Fife"=>"Fife",
                        
"Gloucestershire"=>"Gloucestershire",
                        
"Grampian"=>"Grampian",
                        
"Greater Manchester"=>"Greater Manchester",
                        
"Gwent"=>"Gwent",
                        
"Gwynedd County"=>"Gwynedd County",
                        
"Hampshire"=>"Hampshire",
                        
"Herefordshire"=>"Herefordshire",
                        
"Hertfordshire"=>"Hertfordshire",
                        
"Highlands and Islands"=>"Highlands and Islands",
                        
"Humberside"=>"Humberside",
                        
"Isle of Wight"=>"Isle of Wight",
                        
"Kent"=>"Kent",
                        
"Lancashire"=>"Lancashire",
                        
"Leicestershire"=>"Leicestershire",
                        
"Lincolnshire"=>"Lincolnshire",
                        
"Lothian"=>"Lothian",
                        
"Merseyside"=>"Merseyside",
                        
"Mid Glamorgan"=>"Mid Glamorgan",
                        
"Norfolk"=>"Norfolk",
                        
"North Yorkshire"=>"North Yorkshire",
                        
"Northamptonshire"=>"Northamptonshire",
                        
"Northumberland"=>"Northumberland",
                        
"Nottinghamshire"=>"Nottinghamshire",
                        
"Oxfordshire"=>"Oxfordshire",
                        
"Powys"=>"Powys",
                        
"Rutland"=>"Rutland",
                        
"Shropshire"=>"Shropshire",
                        
"Somerset"=>"Somerset",
                        
"South Glamorgan"=>"South Glamorgan",
                        
"South Yorkshire"=>"South Yorkshire",
                        
"Staffordshire"=>"Staffordshire",
                        
"Strathclyde"=>"Strathclyde",
                        
"Suffolk"=>"Suffolk",
                        
"Surrey"=>"Surrey",
                        
"Tayside"=>"Tayside",
                        
"Tyne and Wear"=>"Tyne and Wear",
                        
"Warwickshire"=>"Warwickshire",
                        
"West Glamorgan"=>"West Glamorgan",
                        
"West Midlands"=>"West Midlands",
                        
"West Sussex"=>"West Sussex",
                        
"West Yorkshire"=>"West Yorkshire",
                        
"Wiltshire"=>"Wiltshire",
                        
"Worcestershire"=>"Worcestershire"
    
);
    
$string="<select name="".$dropdown_name."">n";
    if (!empty(
$county_dropdown)) {
        if (
$key_selected=="" || !isset($key_selected)) {
            
$string.="<option value="">Please select</option>n";
        }
        foreach(
$county_dropdown as $county_key=>$county_value) {
            if (
$key_selected!="" && $key_selected==$county_key) {
                
$additional=" SELECTED";
            }
            else {
                
$additional="";
            }
                
$string.="<option value="".$county_key.""".$additional.">".$county_value."</option>n";
        }
    }
    
$string.="</select>n";
    return 
$string;
}
?>

Simon Bennett commented on Aug 2nd 2013

Cheers :)

Really helpful with Paypal checkout

Drika commented on Oct 21st 2013

Home run! Great work with that answer!