Jump to content

Selector for getting empty checkbox


adrian
 Share

Recommended Posts

Hi guys,

I want a simple selector to test if a checkbox field is empty.

hide_from_top_menu=1 
 

works as expected and finds those that are checked, but 

hide_from_top_menu!=1
 

 does not work - it returns nothing.

Now I understand that there is no entry in the hide_from_top_menu field for a page until the checkbox is checked and I guess that is the problem.

I am using this with MarkupSimpleNavigation. Obviously I can easily hack the code of the module to add in a check something like this:

if($child->hide_from_top_menu != '1'){
 

but I feel like I should be able to achieve this more cleanly.

Obviously I could also turn this field around to be "show_in_top_menu", but since there is no way to set default values for fields, I'd rather  avoid having users have to check this for each new added page. Maybe not a big deal, but thought this might be an issue at a more general level :)

Thanks!

Link to comment
Share on other sites

Just a quick followup - I have decided to take a different approach to the problem, so I don't really need any specific help, but I'd still be curious to know if it's possible to use a selector to find all pages with unchecked instances of a checkbox.

Link to comment
Share on other sites

Does something similar to below not work?

if($page->hide_from_top_menu == 0){  }
 

See this thread and the solution (post #9) as well as the issues about '=' versus '=='. Can't find it now but if I remember correctly, in PW checkbox is either 0 (unchecked) or 1 (checked).

I could be wrong ;)

/k

  • Like 2
Link to comment
Share on other sites

kongondo - thanks!

I couldn't use the approach you suggested, because I needed to use a PW selector within the nav module. But, even though I was sure I had tried it, I just tested a '=0' selector and it does indeed return all pages without that field checked. It returns all pages, whether they have the field in their template or not, you need to add an additional selector to limit results to a parent page, or perhaps a template. But it does work - not sure how I missed it before - tired brain I guess!  :)

As for '=' vs '==' - if you're using a PW selector it needs to be '='. If you are doing something in regular php it needs to be '==' or '===' depending on what you are comparing

Link to comment
Share on other sites

You could check checkboxes like this:

if($checkbox)

and

if(!$checkbox)

example:

if($page->checkbox) echo 'i am set';

if(!$page->checkbox) echo 'i am not set';

So to modifiy your output in a foreach just put your markup in an if condition.

  • Like 1
Link to comment
Share on other sites

Thanks Luis - also good options, but I was trying to doing it just through a PW selector so I could use the selector option that Soma built into the SimpleNav module. Turns out the '=0' in a selector works great, but I still actually ended up going with a different approach just because it made more sense in the current situation :)

Link to comment
Share on other sites

In the end I think it suited the purpose better to check against templates like this:

"selector"=>"template!=person"

because I don't want the children of certain pages to show in the menu - things like a staff directory for instance where each child page is a person and has the template 'person'.

Link to comment
Share on other sites

because I don't want the children of certain pages to show in the menu - things like a staff directory for instance where each child page is a person and has the template 'person'.

depends on the structure of the site, but couldn't this also be done with a setting in MarkupSimpleNavigation?

'max_levels' => 1,
// set the max level rendered
  • Like 1
Link to comment
Share on other sites

Yeah - it is a great module and great docs - for some reason, my brain just didn't think of that approach in this situation. I am actually using a combined template!= and max_levels set of options to achieve what I need.

Link to comment
Share on other sites

  • 10 months later...

!field=value doesn't exist.

What do you mean? It is in the docs.

checkbox!=1 returned nothing and checkbox=0 worked in some cases but not others. As soon as I replaced it with !checkbox=1 (nothing else was changed in the selector or elsewhere) it worked in all cases. checkbox= seems to behave the same as checkbox=0. This is with PW 2.4.

I am using this in the selector for my global site search to not return results from pages which have the checkbox checked.

This is what the selector looks like:

$matches = $pages->find("body|sidebar|field|another_field%=$q, limit=50, !checkbox=1");
Link to comment
Share on other sites

It is the same as "checkbox=0" or "checkbox=" (these all return the expected results for me)

Can you tell where or how you know it's not returning expected result? I mean I use checkboxes all the time and never noticed something wrong with it. Maybe a mysql thing? What PW version?

Link to comment
Share on other sites

It is the same as "checkbox=0" or "checkbox=" (these all return the expected results for me) Can you tell where or how you know it's not returning expected result? I mean I use checkboxes all the time and never noticed something wrong with it. Maybe a mysql thing? What PW version?  

See my last post for the selector I'm using. It is for the site search. PW 2.4. So it is using Ryan's default search.php template with some modifications. I am also doing a similar selector for other pages in which I am just getting the children of the page, and there checkbox=0 works fine. 

The only thing I can think of is that I'm using the %= on the earlier part of the selector for the more thorough search functionality, but I don't know how or why that might effect the checkbox=0 part...

Link to comment
Share on other sites

I was able to reproduce when using a %= on multiple fields before the checkbox=, it would return results found with the %= search and checkbox checked. As soon as I move the checkbox=0 before the title|body|summary%=value it works again.

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...