multiple if then/ or statements

This is probably a newb question, but I have a script where I capture a zip code from a text box and then depending on what it is display different content. I’m not using a database of any kind for this. It is to find the nearest service center based on zip code. A snippet of my code looks like this. Above this code I have set what displays when each variable is echoed out. My question is how can I do this more efficiently. I have a map of the station locations and a zip code map.

I’ve been reading about arrays. Is that something that would work well here?

<?php $zipinput = $_POST["input"]; if($zipinput =='55005' or '55011' or '55014' or '55070' or '55421") { echo "

We have found your 3 nearest service centers

"; echo $ServiceCenter1; echo $ServiceCenter5; echo $ServiceCenter12; } elseif($zipinput == '55315' or '55316' or '55317' or '55320") { echo "

We have found your 3 nearest service centers

"; echo $ServiceCenter3; echo $ServiceCenter8; echo $ServiceCenter13; } else { echo "You are outside of our service area"; } ?>

Maybe you could do it in switch. It won’t shorten your code that much, but it will be definitely prettier.

[php]
$zipinput = $_POST[“input”];
switch($zipinput) {
case ‘55005’:
case ‘55011’:
case ‘55014’:
case ‘55070’:
case ‘55421’:
echo “

We have found your 3 nearest service centers

”;
echo $ServiceCenter1;
echo $ServiceCenter5;
echo $ServiceCenter12;
break;
case ‘55315’:
case ‘55316’:
case ‘55317’:
case ‘55320’:
echo “

We have found your 3 nearest service centers

”;
echo $ServiceCenter3;
echo $ServiceCenter8;
echo $ServiceCenter13;
break;
default:
echo “No services found”;
}
[/php]

But I’d rather propose databse, even SQLite might be fine for your requirements. That would simplify a lot.

Here’s another way of doing using the in_array function:

[php]<?php

$zone1 = [55005, 55011, 55014, 55070, 55421]; // For Service Centers 1, 5, 12
$zone2 = [55315, 55316, 55317, 55320]; // For Service Centers 3, 8, 13

/* You can have the different ‘zones’ match the $sevice_center array */
$service_center[‘zone1’][1] = “Jimmy’s Garage”;
$service_center[‘zone1’][5] = “Bob’s All-Star Garage”;
$service_center[‘zone1’][12] = “Fillmore’s Classic Garage”;

$service_center[‘zone2’][3] = “Lisa’s Hot Rods Garage”;
$service_center[‘zone2’][8] = “Mikey Does It All Garage”;
$service_center[‘zone2’][13] = “John’s Service Center”;

/* The structure of the $service_center array */
echo “

” . print_r($service_center, 1) . “
\n”;

/* Would pull in from a HTML Form */
$zip_code = filter_input(INPUT_POST, ‘zipcode’, FILTER_SANITIZE_NUMBER_INT);

/* Can use if statements or the switch statement /
if (isset($zip_code) && in_array($zip_code, $zone1)) {
/
Zone 1 Service Centers /
echo “

We have found " . count($service_center[‘zone1’]) . " service centers that are near you!

\n”;
foreach($service_center[‘zone1’] as $value) {
echo $value . “
\n”;
}
} elseif (isset($zip_code) && in_array($zip_code, $zone2)) {
/
Zone 2 Service Centers /
echo “

We have found " . count($service_center[‘zone2’]) . " service centers that are near you!

\n”;
foreach($service_center[‘zone2’] as $value) {
echo $value . “
\n”;
}
} elseif (isset ($zip_code)) {
/
Outside of Service Area */
echo “

We are sorry, but we found 0 service centers that are near you.

\n”;
}[/php]

Though like stated if you start having a ton of data, it probably would be best to use a database table.

Sponsor our Newsletter | Privacy Policy | Terms of Service