Jump to content

Array, Switch or something else. Can you point me in the right direction?


Recommended Posts

Not sure how to approach this ...

 

I have a form where someone will select a city from a dropdown (over 80 cities currently, will be expanding to more).

 

Each city falls in one of three categories (i.e. small, medium, large).

 

After the user submits the form I need to determine what city the user selected and the size (small, medium, or large).

 

Should I store the size in an array for each city name? That sounds wrong to me; or is it?

 

I could use a switch statement I guess to check if 'New York' was selected and then set the city size to large, but that seems like excessive code also.

 

Am I close? If you could point me in the right direction so I could read up on it, that would be awesome.

 

 

Thanks!

 

 

 

 

You could do the switch statement or even an if/else.

 

I think the best way would be to make 3 arrays of small, medium, large, ..with your appropriate cities in each array.

 

Then do a check if in_array is ........

 

<?php
$small = array("city1", "city2", "city3", "city4");
$medium = array("city1", "city2", "city3", "city4");
$large = array("city1", "city2", "city3", "city4");

if (in_array($the_city, $small)) {
    echo "$the_city is small";
} elseif (in_array($the_city, $medium)) {
    echo "$the_city is medium";
} elseif  (in_array($the_city, $large)) {
    echo "$the_city is large";
} else {
   echo "$the_city size is unknown";
}
?>

 

Something like that should work with whatever results you may want to do versus the echo.

I wrote up a code specific for you.

 

It uses a text file for the array using a pipe to separate the city to size

 

demo here if would like to try it

http://get.blogdns.com/dynaindex/citysize/?city=altoona

 

<?php
//data.txt contains all city information. On each line is new york,large 
$city = mysql_real_escape_string($_GET['city']);
$city = trim(strtolower($city));
?>

<form name="input" action="" method="get">
City <input type="text" name="city" value="<?php echo $city;?>" />
<input type="submit" value="See City Size" />
</form>

<?php
<?php
//data.txt contains all city information. On each line is new york,large 
$city = mysql_real_escape_string($_GET['city']);
$city = trim(ucfirst(strtolower($city)));
?>

<form name="input" action="" method="get">
City <input type="text" name="city" value="<?php echo $city;?>" />
<input type="submit" value="See City Size" />
</form>

<?php
if (!isset($_GET['city']) || $city == ""){
echo "Insert a city name";
EXIT;
}

function get_size($string) {
    $pos = strrpos($string, '|');
    if($pos===false) {
        return false;
    } else {
        return substr($string, $pos+1);
    }
}

$my_file = "data.txt";
if (file_exists($my_file)) {
$data = file($my_file);
$check = "No";
foreach ($data as $line) {
if (preg_match("/$city/i", $line)) {
$check = "Yes";
}

}
if ($check != "Yes"){
echo "$city not found";
} else {
$line = trim($line);
$line = get_size($line);
echo "$city is $line size.<br />";
}


} else {
echo "No file to display";
}
?>

 

data.txt example  used (of course is not all cities or accurate data)

 

Abbeville|small

Adamsville|large

Addison|medium

Akron|large

Alabaster|small

Albertville|small

Alexander City|small

Aliceville|small

Allgood|medium

Altoona|large

Anderson|medium

 

 

 

 

 

oops, pasted that wrong somehow

 

<?php
//data.txt contains all city information. On each line is new york,large 
$city = mysql_real_escape_string($_GET['city']);
$city = trim(strtolower($city));
?>

<form name="input" action="" method="get">
City <input type="text" name="city" value="<?php echo $city;?>" />
<input type="submit" value="See City Size" />
</form>

<?php

if (!isset($_GET['city']) || $city == ""){
echo "Insert a city name";
EXIT;
}

function get_size($string) {
    $pos = strrpos($string, '|');
    if($pos===false) {
        return false;
    } else {
        return substr($string, $pos+1);
    }
}

$my_file = "data.txt";
if (file_exists($my_file)) {
$data = file($my_file);
$check = "No";
foreach ($data as $line) {
if (preg_match("/$city/i", $line)) {
$check = "Yes";
}

}
if ($check != "Yes"){
echo "$city not found";
} else {
$line = trim($line);
$line = get_size($line);
echo "$city is $line size.<br />";
}


} else {
echo "No file to display";
}
?>

If you're going to be adding more cities in the future, it would probably be best to store all of this data in a database.  Then you just use a simple query to determine the city size, and can have a quick little admin UI to add/edit cities in the list.

For what it's worth I redid this into selects that get the cities names from the same data.txt file.

 

Same link for demo

http://get.blogdns.com/dynaindex/citysize/

 

<?php
//data.txt contains all city information. On each line is  similar to new york,large or smallville,medium
$city = mysql_real_escape_string($_GET['city']);
$city = trim($city);
$my_file = "data.txt";
if (file_exists($my_file)) {
$data = file($my_file);
?>
<form name="input" action="" method="get">
<select name="city">
<?php
foreach($data as $lines){
$info = explode("|",$lines);
$cities = ucwords(strtolower($info[0]));
echo "<option value='$cities'>$cities</option>";
  
}
?>

</select>
<input type="submit" value="Get Size" />
</form>

<?php

if (!isset($_GET['city']) || $city == ""){
echo "Insert a city name";
EXIT;
}

function get_size($string) {
    $pos = strrpos($string, '|');
    if($pos===false) {
        return false;
    } else {
        return substr($string, $pos+1);
    }
}

foreach ($data as $line) {
if (preg_match("/$city/i", $line)) {
$check = "Yes";
}

}
if ($check != "Yes"){
echo "$city not found";
} else {
$line = trim($line);
$line = get_size($line);
echo "$city is $line size.<br />";
}

} else {
echo "No file to display";
}
?>

That's incredibly helpful; I can't thank you enough.

 

I feel like I know just enough to get into trouble and I'm never quite sure if I'm doing it the 'best way' ... so seeing someone else's approach is really insightful.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.