I got asked to write a little PHP script that takes some POSTed input from a few drop down boxes which give some selectable criteria and at the end, spits out one or more string variables containing unique codes.
The variable names are of the form $thingPlaceType, and each one is unique. The drop down boxes allow for selection of:
- either one “thing” or all “things” together
- either one “place” or all “places” together
- either one “type” or all “types” together
I can’t figure out how to select these codes without resorting to nested switch statements where I do
switch($_POST['thing'])
{
case "thing1":
switch($_POST['place'])
{
case "place1":
switch($_POST['type'])
{
case "type1":
$output = $thing1Place1Type1;
case "type2":
$output = $thing1Place1Type2;
case "alltypes":
$output = $thing1Place1Type1.$thing1Place1Type2.$thing1PlaceType3;
}
case "place2":
...
case "allplaces":
...
}
case "thing2":
switch($_POST['place'])
{
case "place1":
switch($_POST['type'])
{
case "type1":
$output = $thing1Place1Type1;
...
...
...
}
It seems that the code is turning into the Arrow Anti-Pattern. I’m thinking I could possibly do something using multi-dimensional arrays, or maybe a single array where I match the values against the keys. But I feel that’s clutching at straws and there must be something I’m missing. Is it time to turn the strings into proper objects with properties?
if you want to convert them to objects.. you can create this.