Jump to content

Filtering with multiple check boxes


strandoo
 Share

Recommended Posts

Hi all. I'm building a product search page with a filter consisting of selects and checkboxes. It's based on Ryan's excellent Skyscraper demo, but I'm trying to add multiple checkbox fieldtypes. I've got it working, but it's not very elegant and I'm sure there's a better way to do it.

Given the following checkboxes:
[x] Adidas (15)
[  ] Asics (10)
[x] Nike (20)

If only Adidas is checked, 15 shoes will be found; if both Adidas and Nike, 35 shoes will be found.

The form field:

foreach($pages->get("/brands/")->children() as $brand) {
   $selected = $brand->name == $input->whitelist->brand ? " checked='checked' " : "";
    echo "<label><input $selected type='checkbox' name='brand[]' value='{$brand->name}'> {$brand->title}</label>";
}

Here's what I've got for the select, based on the Skyscraper search:

$selector = '';
...
if($input->get->brand) {
    $brands = $input->get->brand;
    $selector .= "brand=";
    foreach($brands as $brand) {
        $value = $brand;
        $selector .= "$value|";
        $input->whitelist('brand', $value);
    }
    $selector .= ", ";
}
...
$selector = "template=product-page, limit=24, " . trim($selector, ", ");

This sort of works (although pagination doesn't work because I don't know how to do the whitelist part -- bonus question!). The correct results are returned, but an echo of the selector string looks weird: "template=product-page, limit=24, brand=adidas|nike| " (there's a trailing pipe character that I don't know how to remove).

Is there a better way to do this? Thanks in advance.

 

Link to comment
Share on other sites

you'd need to first change the brand selector to band.name, so it would look like brand.name=adidas|nike

i don't think the trailing pipe will affect anything, but you can remove it before you add the comma, using a rtrim.

Alternately you can use the new selector array syntax; The skyscrapers profile is probably really old and doesn't represent the latest and greatest advances and breakthroughs that the api has to offer. I would recommend to study this blog post:

https://processwire.com/blog/posts/processwire-3.0.13-selector-upgrades-and-new-form-builder-version/#building-a-selector-string-with-user-input-example

Link to comment
Share on other sites

Thanks, Macrura. I just read that post and I like the idea of using selectors as arrays. I might try that after I redo my code the 'normal' way. My script works as is (with out using brand.name) because my input IS the brand name, not the page id... if I understand correctly. But I see from that post that maybe I should use the ID and do as you suggest. After my original post, I used rtrim for something else then slapped my head for not trying it here. Then I just need to read up on whitelists... Cheers.

Link to comment
Share on other sites

57 minutes ago, strandoo said:

my input IS the brand name, not the page id... if I understand correctly.

maybe the selector engine is figuring it out based on whether it is an integer and then looking at the page name (if you are using page reference)

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