I'm working on a type of directory. One Select section has a place where a user can choose their state. The next select section contains 4 values for listings.
When they choose their appropriate state and the listing type it's supposed to pull back listings in that state.
Unfortunately, I'm having 2 problems.
The first 4 listing types work beautifully. If you choose California and one of the first 2 listing types all of the data comes back. But if you choose a state and either listing type 3 - 4, it throws the error I created "Please choose a state and a listing type". This doesn't make sense, because the code for the 3rd and 4th listing types are identical to the 1st 2.
I'm using switch commands to trigger a class >> function for each state. I've provided a condensed version of what this looks like, but I have like 600+ lines of switch/case statements and that seems excessive. But don't it with if/elseif commands would be even more prolific.
HTML
<form action="" method="post">
<select name="state">
<option>Choose State</option>
<option value="AL">AL</option>
<option value="AK">AK</option>
<option value="AZ">AZ</option>
<option value="AR">AR</option>
</select>
<select name="type">
<option>Type</option>
<option value="location">Location</option>
<option value="hotels">Hotels</option>
<option value="cars">Cents</option>
<option value="trucks">Trucks</option>
</select>
<p><input type="submit" name="submit" value="Search For" /></p>
</form>
PHP
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$state = $_POST['state'];
$accommodation_type = $_POST['accommodation-type'];
if( isset($state) && (isset($accommodation_type)) ) {
if($accommodation_type == 'location') {
require('includes/location.php');
switch($state) {
case $state == 'AL':
$location = new Location();
$location->Alabama_Location();
break;
case $state == 'AK':
$location = new Location();
$location->Alaska_Location();
break;
case $state == 'AZ':
$location = new Location();
$location->Arizona_Location();
break;
}
}
elseif($accommodation_type == 'hotels') {
require('includes/hotels.php');
switch($state) {
case $state == 'AL':
$hotel = new Hotels();
$hotel->Alabama_Hotels();
break;
case $state == 'AK':
$hotel = new Hotels();
$hotel->Alaska_Hotels();
break;
case $state == 'AZ':
$hotel = new Hotels();
$hotel->Arizona_Hotels();
break;
}
} else {
echo "Please choose a state and an accommodation type";
}
} else {
} } else {}
So I need a way to simplify this, because I've condensed this and when you tally up 50 states x 4 categories you get 200 case statements. Seems excessive.
Also I still don't understand why it loads the right data for the first 2 options, but not the last 2, when all I did was copy the working code and repeat it with different data.
Plus, it's not checking to see if both state and type are selected. It only shows the user the warning when state hasn't been selected.
Any thoughts? and thank you for any help.