简体   繁体   中英

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.

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")

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.

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. this result in true for if and hence the code instead if block will get executed. Since you don't have exit it parse next if .

I suggest changing your if to switch with break in there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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