OK, so I have a database table which stores member details including addresses. I essentially want to add a list of counties to a form select box, but rather than list all counties and have some return no matches, I want to list only counties that actually exist in the database in the select box.
I may be going about this the wrong way, but thought if I had an array of all the counties and an array of counties in the database I could then use array_intersect to see which counties exist ??
The address details are all stored in one column on the database called memberproperties as such: {“address”:“12 Address Street”,“city”:“City”,“county”:“County”,“post_code”:“MMM 1MM”} so I also assume I need json_decode.
I am getting in a complete pickle as I really don’t know a great deal about fetching data from a database and my PHP knowledge is basic so I have Frankensteined a few bits together that I have found online!
This is what I have so far, but it doean’t work outside of the foreach which of course means I get multiple arrays (on for each member) rather than just a nice single array of matching counties. I don’t even have a clue if what I am trying to do is possible:
[php]$con = new PDO(“mysql:host=$servername;dbname=$dbname”, $username, $password);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sth = $con->prepare(‘SELECT memberProperties FROM perch2_members’);
$sth->execute();
$data = $sth->fetchAll(PDO::FETCH_ASSOC);
$counties = array (
‘Aberdeenshire’, ‘Angus’, ‘Antrim’, ‘Argyll & Bute’, ‘Armagh’, ‘Ayrshire’, ‘Banffshire’, ‘Bedfordshire’, ‘Berkshire’, ‘Berwickshire’, ‘Blaenau Gwent’, ‘Borders’, ‘Bridgend’, ‘Buckinghamshire’, ‘Caerphilly’, ‘Caithness’, ‘Cambridgeshire’, ‘Cardiff’, ‘Carmarthenshire’, ‘Ceredigion’, ‘Cheshire’, ‘Clackmannanshire’, ‘Cleveland’, ‘Conwy’, ‘Cornwall’, ‘County Durham’, ‘Cumbria’, ‘Denbighshire’, ‘Derbyshire’, ‘Devon’, ‘Dorset’, ‘Down’, ‘Dumfries & Galloway’, ‘Dunbartonshire’, ‘East Ayrshire’, ‘East Dunbartonshire’, ‘East Lothian’, ‘East Renfrewshire’, ‘East Riding of Yorkshire’, ‘East Sussex’, ‘Essex’, ‘Fermanagh’, ‘Fife’, ‘Flintshire’, ‘Gloucestershire’, ‘Greater London’, ‘Greater Manchester’, ‘Gwynedd’, ‘Hampshire’, ‘Herefordshire’, ‘Hertfordshire’, ‘Highland’, ‘Inverclyde’, ‘Isle of Anglesey’, ‘Isle of Wight’, ‘Isles of Scilly’, ‘Kent’, ‘Kincardineshire’, ‘Lanarkshire’, ‘Lancashire’, ‘Leicestershire’, ‘Lincolnshire’, ‘Londonderry’, ‘Merseyside’, ‘Merthyr Tydfil’, ‘Midlothian’, ‘Monmouthshire’, ‘Moray’, ‘Neath Port Talbot’, ‘Newport’, ‘Norfolk’, ‘North Ayrshire’, ‘North Lanarkshire’, ‘North Yorkshire’, ‘Northamptonshire’, ‘Northumberland’, ‘Nottinghamshire’, ‘Orkney’, ‘Oxfordshire’, ‘Pembrokeshire’, ‘Perth & Kinross’, ‘Powys’, ‘Renfrewshire’, ‘Rhondda Cynon Taff’, ‘Rutland’, ‘Shetland’, ‘Shropshire’, ‘Somerset’, ‘South Ayrshire’, ‘South Lanarkshire’, ‘South Yorkshire’, ‘Staffordshire’, ‘Stirlingshire’, ‘Suffolk’, ‘Surrey’, ‘Swansea’, ‘Torfaen’, ‘Tyne & Wear’, ‘Tyrone’, ‘Vale of Glamorgan’, ‘Warwickshire’, ‘West Dunbartonshire’, ‘West Lothian’, ‘West Midlands’, ‘West Sussex’, ‘West Yorkshire’, ‘Western Isles’, ‘Wiltshire’, ‘Worcestershire’, ‘Wrexham’
);
foreach($data as $row) {
$myData = json_decode($row[‘memberProperties’], true);
extract($myData);
$result = array_intersect($counties, $myData);
print_r($result);
}[/php]
print_r($result); is obviously just to test what I am getting. Using the array to populate a select box will be the next thing on my list.
Please can someone more knowledgeable help me out here?!