1

I have a list of postcodes in the UK with a region id next to it. Now for delivering products it costs more depending on the region a user lives in.

For example, if a user lives in Birmingham and has a postcode that starts with B, he will get free delivery because that postcode region doesn't have any charge.

Likewise, if a user has a postcode starting with IM , they have to pay more delivery as that postcode region is more.

Sample postcode list:

Postcode | Region
AL | A
BA | A
BB | A
BD | A
B | B
BH | B
LN | D
LS | D
IV1 | E
IV23 | F

From the example above if a user wants to get a delivery and their postcode starts with BA then I want to apply the delivery charge rate of region A.

I'm actually a bit confused as to how I can programmatically do this. At first I thought I would simply do something similar to:

$postcodes = [
    'AL'=>'A',
    'BA'=>'A',
    //And so on ....
];

//get the first 2 letters
$user_input = substr( $user_postcode, 0, 2 );

if(array_key_exists($user_input,$postcodes)){
    //Get the region code
    $region = $postcodes[$user_input];

    // Charge the user with the delivery rate specific to that user, then carry on 
}

But problem is that some similar postcodes can be in different regions, so for example, IV1 is region E and IV23 is region F like seen above.

That means I have to match a users post code on either, the 1 , 2 ,3 or 4 characters. That probably doesn't make sense. To elaborate more see below:

//From Birmingham and is in region B
$user1_input = 'B';

//From Bradford and is in region A
$user1_input = 'BD';

//From Inverness and is in region E
$user1_input = 'IV1';

So if the user input is from Birmingham and user input starts with B , how can i tell that apart from a postcode that also starts with B but then has other letters in it which makes it a different postcode.

I'm trying my best to explain, hopefully, this does make sense. If not please ask for more info.

Can anyone please help me with the logic to how I could achieve this? Either in Javascript or PHP , because i can convert the logic afterwards.

virepo
  • 320
  • 5
  • 22

2 Answers2

1

One option would be to order your postcode/region array by the descending length of the postcode key. This way, the longer (more specific) keys are checked first. Taking your list above, it would become something like this...

$postcodes = array(
    "IV23" => "F",
    "IV1" => "E",
    "LS" => "D",
    "LN" => "D",
    "BH" => "B",
    "BD" => "A",
    "BB" => "A",
    "BA" => "A",
    "AL" => "A",
    "B" => "B",
);

After you have that, it's as simple as looping through the array, checking for a match against the provided postcode (starting from the left), and stopping when you find a match.

foreach($postcodes as $code => $region)
{
    if($code == substr($user_postcode, 0, strlen($code)))
    {
        $shippingRegion = $region;
        break;
    }
}

echo $shippingRegion;
Patrick Q
  • 6,373
  • 2
  • 25
  • 34
1

If you have what looks like a valid UK postcode, then remove the spaces and just search the array till you find a match:

$lookup = [
   '' => 'X', // in case no match is found
   'AL'=>'A',
   'BA'=>'A',
    //And so on ....
];

function get_delivery_for($postcode)
{
   global $lookup;
   for ($x=5; $x>0 && !$result; $x--) {
      $result=$lookup[substr($postcode, 0, $x)];
   }
   return ($result);
}

Note that the code above is intended for illustration, I would recommend using something more elaborate to avoid it throwing warnings....

$result=isset($lookup[substr($postcode, 0, $x)]) 
       ?  $lookup[substr($postcode, 0, $x)]
       : false;
Community
  • 1
  • 1
symcbean
  • 47,736
  • 6
  • 59
  • 94