John Davidson

string - PHP: Display a state depending upon zipcode with varibles & isset

0 comments
Message:


With the first line of code below I'm able to grab the last part of the URL.


The second line of code I'm able to print out / display the zipcode (which is the last part of the URL).


The third part is where my issue is... I'm trying to get the $state to display what State depending upon the zipcode.


I have Alaska as my example. my whole code includes all 50 states.


<?php $str = "https://www.mywebsite.com/depts/$_GET[zipcode]"; $zipcode = explode("/",$str);?>
<?php print end($zipcode); ?>
<?php

if(isset($_GET['zipcode'])){$checkZip=$_GET['zipcode'];}

//Alaska
if($checkZip<99950 && $checkZip>99501){$state='Alaska';} ?>

For example: on my website the first page I ask for their zipcode. I then take their zipcode and create a clean URL with .htaccess which then spits out the https://www.mywebsite.com/depts/90210


What I'm trying to do is to display the "State" on the second page depending upon the supplied zipcode from the first page.



My Answer:
Here is the corrected code:

php
$str = "https://www.mywebsite.com/depts/$_GET[zipcode]";
$zipcode = explode("/", $str);

if(isset($_GET['zipcode'])){
$checkZip = $_GET['zipcode'];
}

//Alaska
if($checkZip < 99950 && $checkZip > 99501){
$state = 'Alaska';
}
?>

print end($zipcode);
echo $state;
?>


This code will display the state "Alaska" if the zipcode falls within the range specified. You can add similar if statements for the rest of the states to display the correct state based on the zipcode provided.

Rate this post

4 of 5 based on 2294 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us