[英]Zip code checker error
我正在尝试为澳大利亚的少数商店构建邮政编码检查器。 基本上,客户输入其邮政编码,然后该站点将重定向到适当的本地定价/服务。
我以此为基础,但遇到一个问题(该站点上的评论之一也列出了该问题),无论出于什么原因而输入了什么值,即使该邮政编码完全不同,该代码也会将该邮政编码标记为SA。
我正在使用的代码如下。
<?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);
}
即使输入的值(例如2142)清楚地标记为NSW,我似乎也无法弄清楚是什么导致它重定向到SA
if ($state = VIC)
那有两个问题。 首先,=用于分配,而不用于比较。 其次,VIC是一个字符串,应使用诸如
if ($state == 'VIC')
为所有if语句修复该错误,您就很好。
另外 ,在开始比较状态列表之前,您无需在任何地方调用findState函数,您需要调用该函数以查看邮政编码属于哪个状态
$state=findState($postcode);
//if($state=="thisthat")
这是经典的==
问题,如果您使用赋值运算符而不是相等运算符。
另外,我建议每当您使用header redirect
时都使用exit
,以确保下一行代码不会被执行。
从您的部分代码来解释问题。 您正在使用if
语句进行比较,然后将值赋给$ state而不是相等性。 对于if而言,结果为true
,因此将执行if
块代码。 既然你没有exit
其解析未来if
。
我建议您更改if
要在那里break
switch
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.