Jump to content
Peter Knight

Options field and check

Recommended Posts

I have a checkbox with only 1 possible value called "A Project". I'm using the relatively new Options field type.

I'm trying to echo some html if that checkbox is checked.

I tried this 

if ($page->portfolio_type ) {

but it always outputs my HTML even if the checkbox isn't checked. 

I then tried the following from the forum suggestion

if ($page->portfolio_type == 1) // 1 is checked, 0 is unchecked

This has no effect either so I'm wondering if I need a different approach based on the fact that I'm using the Options fieldtype rather than a standard page based options?

Share this post


Link to post
Share on other sites

Hi,

if I'm not mistaking, you can check if a specific option is selected using the has() method.

So in your case one of these options should work:

if ($page->portfolio_type->has(1)) { ... }  //assuming the option is defined as: "1=A Project"
if ($page->portfolio_type->has("title='A Project'")) { ... }

Hope this helps.

Edit: Background should be that the option field returns a WireArray. Which has the has() method for checking its content.

  • Like 4

Share this post


Link to post
Share on other sites

@Peter

one more suggestion, have you tried the Tracy debugger yet?

Once you have it installed you could simply bd($page->portfolio_type) and then see what the system is outputting in either case, instead of guessing at it...

  • Like 4

Share this post


Link to post
Share on other sites

@Peter

one more suggestion, have you tried the Tracy debugger yet?

Once you have it installed you could simply bd($page->portfolio_type) and then see what the system is outputting in either case, instead of guessing at it...

Just tried it and discovered a missing include file (unrelated issue). Looks very useful.

The setup options are a little daunting but I'll definitely try this as a first port of call in future.

Share this post


Link to post
Share on other sites

Just tried it and discovered a missing include file (unrelated issue). Looks very useful.

Hey Pete - be sure to post about this in the Tracy support thread so I can take a look.

The setup options are a little daunting but I'll definitely try this as a first port of call in future.

It works out of the box with the settings as they are. For more info though, be sure to check out the blog post: https://processwire.com/blog/posts/introducing-tracy-debugger/ 

  • Like 1

Share this post


Link to post
Share on other sites

Hey Pete - be sure to post about this in the Tracy support thread so I can take a look.

Hey Adrian

I just meant that TracyDebugger had uncovered a call to one of my own missing include files. I didn't mean there was an issue with TD as such :)

  • Like 1

Share this post


Link to post
Share on other sites

The truth is that the Tracy module is only a cover. It's adrian who analyses your code in the background :)

  • Like 4

Share this post


Link to post
Share on other sites

The truth is that the Tracy module is only a cover. It's adrian who analyses your code in the background :)

Is Adrian actually a French Canadian called Tracey DeBugger ? :-/

  • Like 2

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By RicknRoller
      In the Doc it says:
      I have an options field (options_colors) configured like this:
      I want to have a query returning only pages with the VALUE 'red'. This is a query that works:
      $newsentries = $pages->find('template=news-entry,options_colors=number1,sort=-created,limit=16'); this is a query which does not work
      $newsentries = $pages->find('template=news-entry,options_colors=red,sort=-created,limit=16'); How do I query for 'red' instead of the title for the value?
    • By rolspace.net
      Hi Guys,
      Just finished a website locally and wanted to upload it on the webserver of my customer. I got a server 500 error. Now, the guidelines of the hoster (world4you.com) does not allow "Options" in the htaccess-file. So, when I uncomment these:
      Options -Indexes
      Options +FollowSymLinks
      the site is visible, but the content won't show and no links are available. Not sure if I need the Symlinks-part but I guess I need a workaround for the Index-part. Can anybody help here? I need the website up and running asap....
      Thanks!
      Roli
    • By jploch
      Hi! 
      this should be easy, but I can't get it to work.
      I have a repeater with events.
      Every event has an option-field with multiple checkboxes.
      To filter the events on the frontend I have a script, that uses the class names to filter the results (an event can have multiple categories).

      Now I just want to add every checked option (title) to use as my class name.
      This is what I have so far, wich only gets the first title.
      foreach($page->events as $event) { $tags = $event->options->title; echo "<div class='size1of2 {$tags}'>"; echo "<img src='{$event->image->url}'>"; echo "{$event->text_editor}"; echo "</div>"; }
      I know there is an example with a foreach, but how would I use it in this context?
      foreach($page->countries as $country) { echo "<li>$country->title</li>"; }
       
    • By pppws
      hey there,
      i'm using processwire for the first time, so maybe it's a dumb question. but i'm trying to have an options field which values are the users of the processwire. i don't need the values to be displayed, it's just for the administration of the page. but each time a new user is created / a user is deleted the options field should be updated automatically. is something like that possible?
    • By obesegiraffe
      Hi all, first time poster on the forums but long-time user of Processwire. I couldn't find this exact problem in the forums and I'm hoping it's just something simple I'm missing (first day back from vacation so I am a little brain dead). 
      I have a Select Options Field Type where the client can choose an array of numbers. If nothing is chosen, nothing is outputted onto the page. This works for most pages using this specific template. However, on just one page where I have the field set to blank, I am finding it is still outputting "0", when it actually shouldn't be outputting anything. My code:
      <?php if($page->percent_leased->has('id!=0')){ ?> <h2><?=$page->percent_leased->title?>% Leased</h2> <?php } ?> On this specific page, it is outputting "0% Leased". It's also doing something kind of weird in the database under the percent_leased field type. It's repeating the same ID of the specific problem page twice (page ID 1086) when I set it to blank. It works fine if I actually choose a number and the duplicate goes away. I've never really delved deep into the databases so maybe this is normal? 

      The weird thing is is that this is working fine on all other pages using this template. It's just this one page acting weird. FWIW, this particular site is running version 2.6.1. 
      Thanks for reading!
×
×
  • Create New...