简体   繁体   English

邮政编码检查器错误

[英]Zip code checker error

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. 以此为基础,但遇到一个问题(该站点上的评论之一也列出了该问题),无论出于什么原因而输入了什么值,即使该邮政编码完全不同,该代码也会将该邮政编码标记为SA。

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 即使输入的值(例如2142)清楚地标记为NSW,我似乎也无法弄清楚是什么导致它重定向到SA

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 其次,VIC是一个字符串,应使用诸如

if ($state == 'VIC')

Fix that for all your if statements and you're good. 为所有if语句修复该错误,您就很好。

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 另外 ,在开始比较状态列表之前,您无需在任何地方调用findState函数,您需要调用该函数以查看邮政编码属于哪个状态

$state=findState($postcode);

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

it is classic == problem, in your if you have assignment operator used instead of equality operator. 这是经典的==问题,如果您使用赋值运算符而不是相等运算符。

Also, I suggest using exit whenever you use header redirect , it ensure the next line of code didn't get executed. 另外,我建议每当您使用header redirect时都使用exit ,以确保下一行代码不会被执行。

To explain the problem from your partial code. 从您的部分代码来解释问题。 you are using if statement to compare and then instead of equality you are assigning the value to $state. 您正在使用if语句进行比较,然后将值赋给$ state而不是相等性。 this result in true for if and hence the code instead if block will get executed. 对于if而言,结果为true ,因此将执行if块代码。 Since you don't have exit it parse next if . 既然你没有exit其解析未来if

I suggest changing your if to switch with break in there. 我建议您更改if要在那里break switch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM