Hiya,
Can anyone suggest tips or alterations to make this code cleaner and faster? This was the only way I could think of doing it on a Friday evening, but I'm sure there must be a more efficient way of doing it...
I know regexs aren't efficient but I can't honestly see how else I can do this, especially if the Postcode data can be anything from:
e1 2be e1ebe e10ebe e10 ebe ex1 ebe ex1ebe
and so on...
Thanks a lot for any coding tips, H
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Amma Gawd! Someone ate our database!');
mysql_select_db($dbname);
$result = mysql_query("SELECT * FROM `Consumer`
WHERE left(`Postcode`,2) = 'E'
OR left(`Postcode`,1) = 'N'
OR left(`Postcode`,1) = 'W'");
while($row = mysql_fetch_array($result)) {
$email = $row['Email'];
if (preg_match("/^[Ee]{1}[0-9]{2}/",$row['Postcode'])) {
mysql_query("UPDATE `Consumer` SET `CONYES` = '1' WHERE `Email` = '$email'") or die ("Bugger");
$counter = $counter +1;
} elseif (preg_match("/^[Nn]{1}[0-9]{2}/",$row['Postcode'])) {
mysql_query("UPDATE `Consumer` SET `CONYES` = '1' WHERE `Email` = '$email'") or die ("Bugger");
$counter = $counter +1;
} elseif (preg_match("/^[Ww]{1}[0-9]{2}/",$row['Postcode'])) {
mysql_query("UPDATE `Consumer` SET `CONYES` = '1' WHERE `Email` = '$email'") or die ("Bugger");
$counter = $counter +1;
}
}
$result1 = mysql_query("SELECT * FROM `Consumer`
WHERE left(`postcode`,2) = 'BR'
OR left(`postcode`,2) = 'CR'
OR left(`postcode`,2) = 'EC'
OR left(`postcode`,2) = 'EN'
OR left(`postcode`,2) = 'KT'
OR left(`postcode`,2) = 'NW'
OR left(`postcode`,2) = 'RM'
OR left(`postcode`,2) = 'SE'
OR left(`postcode`,2) = 'SM'
OR left(`postcode`,2) = 'SW'
OR left(`postcode`,2) = 'TW'
OR left(`postcode`,2) = 'WC'
OR left(`postcode`,2) = 'BD'
OR left(`postcode`,2) = 'HG'
OR left(`postcode`,2) = 'LS'
OR left(`postcode`,2) = 'WF'
OR left(`postcode`,2) = 'YO'
OR left(`postcode`,2) = 'HD'
OR left(`postcode`,2) = 'HX'");
while($row1 = mysql_fetch_array($result1)) {
$email = $row1['Email'];
mysql_query("UPDATE `Consumer` SET `CONYES` = '1' WHERE `Email` = '$email'") or die ("Bugger");
$counter = $counter +1;
}
echo $counter;
mysql_close($conn);