i have two postcodes entered by user. i format them validate and check if they do exist then i get postcode1's postal city name from mysql server. i have two districts both arrays one called innerLondon array and the second outterlondon array
i fetch postal city name with my script from ukpostcodes table then I use in_array to compare this postal city name to find out what district it belongs to and set flag to 1 or 0 accordingly.

The flags set will be used for other queries within the same app basically the system will not function properly if i don't
set these flags properly. I know I should use msqli or pdo but this on testing server.

PHP in_array does not set the flag to 1 or to 0 and does not work here is part of my code. Any help and advice highly appreciated.

    $innerlondon = array('Bromley', 'Croydon', 'Dartford', 'East London', 'City of London', 'Enfield', 'Harrow', 'Ilford', 'Kingston-upon-Thames', 'North London', 'North West London', 'Romford', 'South East London', 'Sutton', 'South West London', 'Twickenham', 'Southall', 'West End', 'West Central');

   $outterlondon = array('Aberdeen', 'Bath', 'Belfast', 'Birmingham', 'Blackburn', 'Blackpool', 'Bolton', 'Bournemouth', 'Bradford', 'Brighton', 'Bristol', 'Cambridge', 'Canterbury', 'Cardiff', 'Carlisle', 'Chelmsford', 'Chester', 'Colchester', 'Coventry', 'Crewe', 'Darlington', 'Derby', 'Doncaster', 'Dorchester', 'Dudley', 'Dumfries', 'Dundee', 'Durham', 'Exeter', 'Falkirk', 'Galashiels', 'Glasgow', 'Gloucester', 'Guernsey', 'Guildford', 'Halifax', 'Harrogate', 'Hebrides', 'Hemel Hempstead', 'Hereford', 'Huddersfield', 'Hull', 'Ipswich', 'Isle Of Man', 'Jersey', 'Kilmarnock', 'Kirkcaldy', 'Kirkwall', 'Lancaster', 'Leeds', 'Leicester', 'Lerwick', 'Lincoln', 'Liverpool', 'Llandrindod', 'Llandudno', 'lnverness', 'Luton', 'Manchester', 'Medway', 'Milton Keynes', 'Motherwell', 'Newcastle', 'Newport', 'Northampton', 'Norwich', 'Nottingham', 'Oldham', 'Oxford', 'Paisley', 'Perth', 'Peterborough', 'Plymouth', 'Portsmouth', 'Preston', 'Reading', 'Redhill', 'Salisbury', 'Sheffield', 'Shrewsbury', 'Slough', 'Southampton', 'Southend-on-Sea', 'St Albans', 'Stevenage', 'Stockport', 'Stoke-on-Trent', 'Sunderland', 'Swansea', 'Swindon', 'Taunton', 'Teesside', 'Telford', 'Tonbridge', 'Torquay', 'Truro', 'Wakefield', 'Walsall', 'Warrington', 'Watford', 'Wigan', 'Wolverhampton', 'Worcester', 'York');


    $sql = "SELECT DISTINCT `town` FROM `ukpostcodes` WHERE `postcode`='$postcode1' LIMIT 1";
    $result = mysql_query($sql, $con) or die(mysql_error());
    $postal_town1 = mysql_fetch_assoc($result);
    if(in_array(postal_town1['town'], $innerlondon ))
    {
    echo "It is an inner London District = ";
    echo $town1=$postal_town1['town'];
    echo $inlondon=1; //set flag to 1
    }
    else
    {
    echo $inlondon=0; //set flag to 0
    }


    $sql = "SELECT DISTINCT `town` FROM `ukpostcodes` WHERE `postcode`='$postcode2' LIMIT 1";
    $result = mysql_query($sql, $con) or die(mysql_error());
    $postal_town2 = mysql_fetch_assoc($result);
    if(in_array($postal_town2['town'], $outterlondon ))
    {
    echo " It is an outter London District = ";
    echo $town2=$postal_town2['town'];
    echo $outlondon=1; //set flag to 1
    }
    else
    {
    echo $outlondon=0; //set flag to 0
    }

Recommended Answers

All 4 Replies

Your code seems correct, please share your table structure so that I can check further.

Your code seems correct, please share your table structure so that I can check further.

Thanks Peeyush.

CREATE TABLE IF NOT EXISTS ukpostcodes (
postcode varchar(10) NOT NULL,
town varchar(80) NOT NULL,
PRIMARY KEY (postcode)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

I solved the issue by using the trim() function.

commented: congrats, 'ts good when it comes together +13
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.