I am attempting to construct a postcode checker for a small number of stores in Australia. Basically, the customer enters their zip code and the site will then redirect to appropriate local pricing/services.
I am using this as a base but I am having a problem (listed by one of the comments on that site too) that no matter what value is entered for some reason the code marks the zip as SA even when it is completely different.
The code I am using is below.
<?php
// v1.0 Postcode Checker Just iStuff
// Copyright Ben Green 2014
// Redirect Customers to Appropriate Local Pricing and Services
// Sets cookie upon entering postcode, will then remember postcode for 3 days on customer's system
if (preg_match('#^\d+$#', $_POST['cf_postcode'])):
else:
print "Please enter your postcode correctly.";
endif;
$postcode = $_POST['cf_postcode'];
function findState($postcode) {
$ranges = array(
'NSW' => array(
1000, 1999,
2000, 2599,
2619, 2898,
2921, 2999
),
'ACT' => array(
200, 299,
2600, 2618,
2900, 2920
),
'VIC' => array(
3000, 3999,
8000, 8999
),
'QLD' => array(
4000, 4999,
9000, 9999
),
'SA' => array(
5000, 5999
),
'WA' => array(
6000, 6797,
6800, 6999
),
'TAS' => array(
7000, 7999
),
'NT' => array(
800, 999
)
);
$exceptions = array(
0800 => 'NT',
872 => 'NT',
2540 => 'NSW',
2611 => 'ACT',
2620 => 'NSW',
3500 => 'VIC',
3585 => 'VIC',
3586 => 'VIC',
3644 => 'VIC',
3707 => 'VIC',
2899 => 'NSW',
6798 => 'WA',
6799 => 'WA',
7151 => 'TAS'
);
$postcode = intval($postcode);
if ( array_key_exists($postcode, $exceptions) ) {
return $exceptions[$postcode];
}
foreach ($ranges as $state => $range)
{
$c = count($range);
for ($i = 0; $i < $c; $i+=2) {
$min = $range[$i];
$max = $range[$i+1];
if ( $postcode >= $min && $postcode <= $max ) {
return $state;
}
}
}
return null;
}
//Redirect for NT
if ($state = NT)
{
header( "Location: http://www.justistuff.com.au/ntrepairs.php" );
setcookie("postcode", $postcode, time()+259200);
}
//Redirect for QLD
if ($state = QLD)
{
header( "Location: http://www.justistuff.com.au/mailin.php" );
setcookie("postcode", $postcode, time()+259200);
}
//Redirect for VIC
if ($state = VIC)
{
header( "Location: http://www.justistuff.com.au/mailin.php" );
setcookie("postcode", $postcode, time()+259200);
}
//Redirect for ACT
if ($state = ACT)
{
header( "Location: http://www.justistuff.com.au/mailin.php" );
setcookie("postcode", $postcode, time()+259200);
}
//Redirect for NSW
if ($state = NSW)
{
header( "Location: http://www.justistuff.com.au/mailin.php" );
setcookie("postcode", $postcode, time()+259200);
}
//Redirect for WA
if ($state = WA)
{
header( "Location: http://www.justistuff.com.au/mailin.php" );
setcookie("postcode", $postcode, time()+259200);
}
//Redirect for TAS
if ($state = TAS)
{
header( "Location: http://www.justistuff.com.au/mailin.php" );
setcookie("postcode", $postcode, time()+259200);
}
//Redirect for SA
if ($state = SA)
{
header( "Location: http://www.justistuff.com.au/sarepairs.php" );
setcookie("postcode", $postcode, time()+259200);
}
I can't seem to work out what is causing it to redirect to SA even when the value entered (for example 2142) is clearly marked as NSW
if ($state = VIC)
That has two issues. First of all = is used for assignment, not for comparison. Secondly VIC is a string and should be in quotes like
if ($state == 'VIC')
Fix that for all your if statements and you're good.
Also, you are not calling findState function anywhere, before you start comparing the list of states you need to call that function to see what state your postcode belongs to
$state=findState($postcode);
//if($state=="thisthat")