siamiam Posted April 6, 2011 Share Posted April 6, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/232829-array-switch-or-something-else-can-you-point-me-in-the-right-direction/ Share on other sites More sharing options...
QuickOldCar Posted April 6, 2011 Share Posted April 6, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/232829-array-switch-or-something-else-can-you-point-me-in-the-right-direction/#findComment-1197584 Share on other sites More sharing options...
siamiam Posted April 6, 2011 Author Share Posted April 6, 2011 Thanks so much for the help, I really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/232829-array-switch-or-something-else-can-you-point-me-in-the-right-direction/#findComment-1197791 Share on other sites More sharing options...
QuickOldCar Posted April 6, 2011 Share Posted April 6, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/232829-array-switch-or-something-else-can-you-point-me-in-the-right-direction/#findComment-1197832 Share on other sites More sharing options...
QuickOldCar Posted April 6, 2011 Share Posted April 6, 2011 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/232829-array-switch-or-something-else-can-you-point-me-in-the-right-direction/#findComment-1197854 Share on other sites More sharing options...
Stooney Posted April 6, 2011 Share Posted April 6, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/232829-array-switch-or-something-else-can-you-point-me-in-the-right-direction/#findComment-1197855 Share on other sites More sharing options...
QuickOldCar Posted April 7, 2011 Share Posted April 7, 2011 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/232829-array-switch-or-something-else-can-you-point-me-in-the-right-direction/#findComment-1198071 Share on other sites More sharing options...
siamiam Posted April 7, 2011 Author Share Posted April 7, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/232829-array-switch-or-something-else-can-you-point-me-in-the-right-direction/#findComment-1198294 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.