【问题标题】:Zip code checker error邮政编码检查器错误
【发布时间】:2014-08-09 06:21:29
【问题描述】:

我正在尝试为澳大利亚的少数商店构建邮政编码检查器。基本上,客户输入他们的邮政编码,然后网站将重定向到适当的本地定价/服务。

我使用this 作为基础,但我遇到了一个问题(由该站点上的一个 cmets 也列出),无论出于某种原因输入什么值,代码都将 zip 标记为 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

【问题讨论】:

  • 您是否在启用警告的情况下运行此代码,例如检查 php 日志中的消息。例如,我不知道你在这条线上的意思是什么 if ($state = NT) 是 NT 在某处定义的吗??
  • 也做这个,以便其他人可以运行它,你在哪里调用你的函数我看不出我怎么能尝试这个也考虑简化你的例子。我不知道重定向与您的问题有什么关系?!
  • 我还没有运行任何调试(仍在探索 PHP 的世界),我认为 NT 是在代码最初根据输入的邮政编码计算状态时进一步定义的,然后基于在那个选择是SA,NT,WA,QLD等。我一定是错的!
  • 嗨,Brandin,您建议我将它发布到哪里以便其他人可以运行它?谢谢
  • 检查日志并不是真正的调试,它是你如何检查脚本中的错误。基本上,您的目标应该是删除所有警告和通知。它看起来不会运行,因为我没有看到你在哪里调用你的函数 findState

标签: php zipcode


【解决方案1】:
if ($state = VIC)

这有两个问题。首先 = 用于赋值,而不是用于比较。其次,VIC 是一个字符串,应该用引号括起来,如

if ($state == 'VIC')

为你的所有 if 语句修复它,你就很好了。

另外,在开始比较状态列表之前,您没有在任何地方调用 findState 函数,您需要调用该函数来查看您的邮政编码属于哪个状态

$state=findState($postcode);

//if($state=="thisthat")

【讨论】:

  • 我试了一下,但现在发生的只是当我提交邮政编码时,它会加载包含上面代码的文件并加载一个空白页面,所以它仍然没有执行正确
  • 在添加最后一段(调用函数)后,脚本现在就像一个魅力,非常感谢您的耐心和帮助!
【解决方案2】:

这是经典的== 问题,在您使用赋值运算符而不是相等运算符时。

另外,我建议每次使用header redirect 时都使用exit,这样可以确保下一行代码不会被执行。

从您的部分代码中解释问题。您正在使用if 语句进行比较,然后将值分配给$state,而不是相等。这导致true for if ,因此代码将被执行,而不是if 块。由于您没有exit,因此它会解析下一个if

我建议将您的if 更改为switch,并在其中添加break

【讨论】:

    猜你喜欢
    • 2014-08-26
    • 2015-01-22
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多