I have a database that contains UK postcodes and prices. Each postcode record has a matching price (see below):
W1 £45.00
W2 £48.00
W3 £54.00
SW5 £48.00
SW1 £34.00
SW11 £45.00
You may or may not know, but UK postcodes are alphanumeric and between six and eight characters long.
On my booking form I am taking a full postcode, eg: SW1A 1AA
In my database I am only storing a single value (£34.00) for SW1. This should be a matching record for ANY SW1 full postcode.
I thought about matching the first three characters of the postcode, but that brings up another issue.
If somebody enters a full SW11 postcode, the first three characters will match both SW1 and SW11 so that won't work.
This is why I think I need something that does the following:
Do the first four characters match?
if YES - retrieve record
if NO - Do the first three characters match?
if YES - retrieve record
if NO - Do the first two characters match?
if YES - retrieve record
if NO - Postcode is invalid
Can anybody suggest a better way of doing this? If you think that this method would work, how would I go about writing this function?
W1 £45.00
W2 £48.00
W3 £54.00
SW5 £48.00
SW1 £34.00
SW11 £45.00
You may or may not know, but UK postcodes are alphanumeric and between six and eight characters long.
On my booking form I am taking a full postcode, eg: SW1A 1AA
In my database I am only storing a single value (£34.00) for SW1. This should be a matching record for ANY SW1 full postcode.
I thought about matching the first three characters of the postcode, but that brings up another issue.
If somebody enters a full SW11 postcode, the first three characters will match both SW1 and SW11 so that won't work.
This is why I think I need something that does the following:
Do the first four characters match?
if YES - retrieve record
if NO - Do the first three characters match?
if YES - retrieve record
if NO - Do the first two characters match?
if YES - retrieve record
if NO - Postcode is invalid
Can anybody suggest a better way of doing this? If you think that this method would work, how would I go about writing this function?