Jump to content

Using Switch Statement with Select Options Fieldtype


louisstephens
 Share

Recommended Posts

I thought I had it figured out, but what I coded doesnt seem to be working at all. First let me set up what I am trying to do. Each page of the site has a different background color, so I was going to use a switch statement to just change the div class in conjunction with the Select Options Fieldtype,

The switch:

  $switchColor = $page->switchColor;

  switch ($switchColor) {
    case "brown":
      echo "<div class='container inner-brown'>";
      break;

    case "blue":
      echo "<div class='container inner-blue'>";
      break;

    case "red":
      echo "<div class='container inner-red'>";
      break;

    default:
      echo "<div class='container inner-green'>";

  }

I also set up a Select Options field type to be used on those pages, which is set to allow only 1 selection. However, it seems that it is failing somewhere and is defaulting to the "green". Did I not "select" the selection the correct way?

Link to comment
Share on other sites

Maybe you have simplified your example, but why not simply do this:

echo "<div class='container inner-{$page->switchColor}'>";

or to take care of no selection:

$switchColor = $page->switchColor ? $page->switchColor : 'green';
echo "<div class='container inner-{$switchColor}'>";

PS I think you might actually be looking for $page->switchColor->value or $page->switchColor->title but I would need to confirm as I haven't used the Options fieldtype very often.  Look at the docs: https://processwire.com/api/modules/select-options-fieldtype/#separate-option-values

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...