Jump to content

How to qery the state of an option at option fieldtype?


kreativmonkey
 Share

Recommended Posts

Hi,

i have an option field with display options with an set of options:

  1. Hid article image
  2. Feature article
  3. some more....

now i will make the logic in my template for the article image. If the Option is checked the image will be hid.

$artikelIMG='';

if(count($page->images)){
  if(!$page->article_opiton == 1) {
    $image = $page->images->first();
    $output = "<div class='article-image'>";
    $output .= "<a href='{$image->url}' data-lightbox='titleimage'><img class='img-responsive img-rounded' src='{$image->size(900,350)->url}' alt=''></a>".
              "<span class='quelle'>Quelle des Bildes</span> </img>".
              "</div>";

    $artikelIMG = $output;
  }
}

But i don't know how i can ask if the option 1 is checked or option 3 is checked ....

Thanks alot for helping me,

Sebastian

Link to comment
Share on other sites

Hi Sebastian,

if(!$page->article_option == 1) { ...

This may only check if the field is present, not what value the field has. maybe if the value is an integer, it'd be better to check with "===", not "==".

Depending on how you defined the values in the select opitions setup, for example

1=Hide
2=Feature Article
3=Whatever

you can then check for the value

switch($page->article_options){
  case "1":
  // do nothing
   break;
  case "2":
  // do something
    break;
  case "3":
  // do something else
    break;
}

In your case, where you have the "hide" option for which your code does not need to do something, I would make that the "0" value in the field's setup:

0=Hide
1=Article
2=Whatever

So now you can check if the field's value is larger than "0", and only then put out your code

$articleIMG = '';
if( $page->article_options > 0 ){
  // display image
  $out = 'code only if image is shown';
  switch($page->article_options){
      case 1:
      // do soemthing
      $out .= 'your code';
        break;
      // do something else
      $out .= 'your other code';
        break;
  }
  $articleIMG = $out;
}

Hope this helps,
cheers
Tom

Link to comment
Share on other sites

I'm really much more keen on using semantic values in my code, as it's much more readable.

0=hide|Hide
1=article|Article
2=else|Whatever
if($page->is("article_options.value=hide"){
  //the hide option is ticked, clear markup
  $out = "";
}
  • Like 2
Link to comment
Share on other sites

Hi Webrocker,

thanks four your answere. This field handels the display of an article, not only for the image in the article. So wenn the Hide option is checked the article image doesn't show, when the featured option is checked, the post will shown on the first position on an article list.... i can make an checkbox for each option but i think the optienfield with radio button is for this the better solution.

I want to find out if the option 0 = hide is checked or not!

Link to comment
Share on other sites

Good call, LostKobrakai. Using "speaking" values remove the true/false 0/1 string/ornot confusion while checking against the value, too :-)

I want to find out if the option 0 = hide is checked or not!

Uhm, yes. That's what my code above is doing in the first example :-) But LostKobrakai's more like it.

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...