# Filters with forms (problem with pluck)

**URL:** https://forum.getkirby.com/t/filters-with-forms-problem-with-pluck/4876
**Category:** Questions
**Tags:** v2
**Created:** [August 4, 2016, 9:35am UTC](https://forum.getkirby.com/t/filters-with-forms-problem-with-pluck/4876 "2016-08-04T09:35:55Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![loasca](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/loasca/32/1726_2.png) [@loasca](https://forum.getkirby.com/u/loasca)
#### Post date: [August 4, 2016, 9:35am UTC](https://forum.getkirby.com/t/filters-with-forms-problem-with-pluck/4876/1 "2016-08-04T09:35:55Z")

</div>

Hi,

I’m following the guide at [this page](https://getkirby.com/docs/solutions/filtering#using-filters-with-forms-e-g-select-fields) to get a basic store locator working with a couple of select input.

The problem seems to be the $split parameter in the second pluck(). If I remove it everything works fine, but I need to have multiple values available for the $services field.

Any ideas on what may be the problem here? Thanks!

Here is my controller:

```
return function($site, $pages, $page) {

  $region = $page->children()->pluck('region', null, true);
  $services = $page->children()->pluck('services', ',', true);
  $keys = array('region', 'services');

  $partners = $page->children()->visible();

  if(r::is('POST') && $data = get()) {
    $partners = $page->children()->visible()->filter(function($child) use($keys, $data) {

      foreach($data as $key => $value) {

        if($value && in_array($key, $keys)) {

          if(!$match = $child->$key() == $value) {
            return false;
          }
        }
      }

      return $child;

    });
  }
  return compact('partners', 'region', 'services', 'data');
};

```

and in my template I have:

```
<form id="filters" method="post">
  <select name="region" onchange="this.form.submit()">
    <option selected value="">Seleziona la regione</option>
    <?php foreach($region as $item): ?>
    <?php if(!$item) continue ?>
    <option<?php e(isset($data['region']) && $data['region'] == $item, ' selected') ?> value="<?php echo $item->html() ?>"><?php echo $item->html() ?></option>
    <?php endforeach ?>
  </select>
  <select name="services" onchange="this.form.submit()">
    <option selected value="">Seleziona l'area prodotto</option>
    <?php foreach($services as $item): ?>
    <?php if(!$item) continue ?>
    <option<?php e(isset($data['services']) && $data['services'] == $item, ' selected') ?> value="<?php echo $item->html() ?>"><?php echo $item->html() ?></option>
    <?php endforeach ?>
  </select>
</form>

```

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [August 4, 2016, 9:44am UTC](https://forum.getkirby.com/t/filters-with-forms-problem-with-pluck/4876/2 "2016-08-04T09:44:38Z")

</div>

what do you get if you do a `var_dump()`?

```auto
var_dump($services);

```

---

<div class="post-metadata">

### Author: ![loasca](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/loasca/32/1726_2.png) [@loasca](https://forum.getkirby.com/u/loasca)
#### Post date: [August 4, 2016, 9:54am UTC](https://forum.getkirby.com/t/filters-with-forms-problem-with-pluck/4876/3 "2016-08-04T09:54:06Z")

</div>

> [@texnixe](#):
>
> var\_dump($services);

array(2) { [0]=\> string(9) “service B” [1]=\> string(9) “service A” }

Right now I have 3 store listed with the following:

Store 1  
services: service A

Store 2  
services: service A, service B

Store 3  
services: service B

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [August 4, 2016, 10:02am UTC](https://forum.getkirby.com/t/filters-with-forms-problem-with-pluck/4876/4 "2016-08-04T10:02:52Z")

</div>

What exactly does not work as expected? Does the form output the options? Do you get any error messages?

---

<div class="post-metadata">

### Author: ![loasca](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/loasca/32/1726_2.png) [@loasca](https://forum.getkirby.com/u/loasca)
#### Post date: [August 4, 2016, 10:10am UTC](https://forum.getkirby.com/t/filters-with-forms-problem-with-pluck/4876/5 "2016-08-04T10:10:46Z")

</div>

The second select don’t get populated and the page stops rendering after that.

I assume there are some problems with the foreach loop in the template but I don’t know what to change to make it work.

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [August 4, 2016, 10:15am UTC](https://forum.getkirby.com/t/filters-with-forms-problem-with-pluck/4876/6 "2016-08-04T10:15:06Z")

</div>

Have you turned on debugging in your config.php?

```auto
c::set('debug', true);

```

I can’t see anything obvious, but maybe I’m just blind …😊

---

<div class="post-metadata">

### Author: ![loasca](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/loasca/32/1726_2.png) [@loasca](https://forum.getkirby.com/u/loasca)
#### Post date: [August 4, 2016, 10:21am UTC](https://forum.getkirby.com/t/filters-with-forms-problem-with-pluck/4876/7 "2016-08-04T10:21:52Z")

</div>

Debug is on but I don’t get any error…

Since the dump on $services seems ok (it splits Service A and Service B correctly), I tried a var\_dump on the $region key and it outputs all the vars stored in Kirby… That seems a bit odd.

---

<div class="post-metadata">

### Author: ![loasca](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/loasca/32/1726_2.png) [@loasca](https://forum.getkirby.com/u/loasca)
#### Post date: [August 4, 2016, 10:30am UTC](https://forum.getkirby.com/t/filters-with-forms-problem-with-pluck/4876/8 "2016-08-04T10:30:59Z")

</div>

Nevermind, if I use the $split parameter in the first pluck() as well the dump is ok:

```
array(2) { [0]=> string(9) "Lombardia" [1]=> string(5) "Lazio" }

```

But now the rendering stops at the first select…

```
<select name="region" onchange="this.form.submit()">
  <option selected="" value="">Seleziona la regione</option>

</select>
```

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [August 4, 2016, 10:46am UTC](https://forum.getkirby.com/t/filters-with-forms-problem-with-pluck/4876/9 "2016-08-04T10:46:45Z")

</div>

Pls. try removing the `html()` method when echoing the items.

---

<div class="post-metadata">

### Author: ![loasca](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/loasca/32/1726_2.png) [@loasca](https://forum.getkirby.com/u/loasca)
#### Post date: [August 4, 2016, 10:57am UTC](https://forum.getkirby.com/t/filters-with-forms-problem-with-pluck/4876/10 "2016-08-04T10:57:28Z")

</div>

That seem to did the trick for rendering the selects, thanks!

However the filter doesn’t work when there are multiple values in one field (eg. services: service A, service B). It simply doesn’t show the result.

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [August 4, 2016, 11:23am UTC](https://forum.getkirby.com/t/filters-with-forms-problem-with-pluck/4876/11 "2016-08-04T11:23:50Z")

</div>

Could you post your filtering code?

---

<div class="post-metadata">

### Author: ![loasca](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/loasca/32/1726_2.png) [@loasca](https://forum.getkirby.com/u/loasca)
#### Post date: [August 4, 2016, 12:41pm UTC](https://forum.getkirby.com/t/filters-with-forms-problem-with-pluck/4876/12 "2016-08-04T12:41:19Z")

</div>

The code is the one I took from the Docs. Here it is my controller:

```
return function($site, $pages, $page) {

  $region = $page->children()->pluck('region', null, true);
  $services = $page->children()->pluck('services', ',', true);
  $keys = array('region', 'services');

  $partners = $page->children()->visible();

  if(r::is('POST') && $data = get()) {
    $partners = $page->children()->visible()->filter(function($child) use($keys, $data) {

      foreach($data as $key => $value) {

        if($value && in_array($key, $keys)) {

          if(!$match = $child->$key() == $value) {
            return false;
          }
        }
      }

      return $child;

    });
  }
  return compact('partners', 'region', 'services', 'data');
};
```

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [August 4, 2016, 12:43pm UTC](https://forum.getkirby.com/t/filters-with-forms-problem-with-pluck/4876/13 "2016-08-04T12:43:19Z")

</div>

Oh, sorry, stupid me 😊

Yeah, you would have to adapt the code a bit, and create an array from the services field and check if the value is in the array.

---

<div class="post-metadata">

### Author: ![loasca](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/loasca/32/1726_2.png) [@loasca](https://forum.getkirby.com/u/loasca)
#### Post date: [August 4, 2016, 12:48pm UTC](https://forum.getkirby.com/t/filters-with-forms-problem-with-pluck/4876/14 "2016-08-04T12:48:57Z")

</div>

Maybe that example is not suited for multiple values in fields?  
Unfortunately I’m lost after that first if statement and I don’t know how to modify it…

Thank you for helping me!

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [August 4, 2016, 12:49pm UTC](https://forum.getkirby.com/t/filters-with-forms-problem-with-pluck/4876/15 "2016-08-04T12:49:38Z")

</div>

I don’t have the time now but will look into this later.

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [August 4, 2016, 5:27pm UTC](https://forum.getkirby.com/t/filters-with-forms-problem-with-pluck/4876/16 "2016-08-04T17:27:18Z")

</div>

Ok, now try this:

```auto
return function($site, $pages, $page) {

  $region = $page->children()->pluck('region', null, true);
  $services = $page->children()->pluck('services', ',', true);
  $keys = array('region', 'services');

  $partners = $page->children()->visible();

  if(r::is('POST') && $data = get()) {
    $partners = $page->children()->visible()->filter(function($child) use($keys, $data) {

      foreach($data as $key => $value) {

        if($value && in_array($key, $keys)) {

          if(! $match = in_array($value, $child->$key()->split(','))) {

            return false;
          }
        }
      }

    return $child;

    });
  }
  return compact('partners', 'region', 'services', 'data');
};

```

The only change is in the line that tests for `$match`.

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [August 5, 2016, 10:46am UTC](https://forum.getkirby.com/t/filters-with-forms-problem-with-pluck/4876/17 "2016-08-05T10:46:00Z")

</div>

BTW: I came across the same problem with `pluck()` and `null` as split parameter yesterday and created [an issue on GitHub](https://github.com/getkirby/toolkit/issues/187) as this is a bug.

---

<div class="post-metadata">

### Author: ![loasca](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/loasca/32/1726_2.png) [@loasca](https://forum.getkirby.com/u/loasca)
#### Post date: [August 5, 2016, 2:49pm UTC](https://forum.getkirby.com/t/filters-with-forms-problem-with-pluck/4876/18 "2016-08-05T14:49:05Z")

</div>

Now it works, thank you!

Last thing I have to figure out is where to sort alphabetically the two arrays so they selects are tidier.

---

<div class="post-metadata">

### Author: ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)
#### Post date: [August 5, 2016, 2:53pm UTC](https://forum.getkirby.com/t/filters-with-forms-problem-with-pluck/4876/19 "2016-08-05T14:53:49Z")

</div>

You can use PHP `sort()` to sort the resulting arrays:

```auto
$region = $page->children()->pluck('region', null, true);
sort($region);
$services = $page->children()->pluck('services', ',', true);
sort($services);

```

There are several flags you can use with `sort()`, check out the docs:

[http://php.net/manual/de/function.sort.php](http://php.net/manual/de/function.sort.php)

---

<div class="post-metadata">

### Author: ![loasca](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/loasca/32/1726_2.png) [@loasca](https://forum.getkirby.com/u/loasca)
#### Post date: [August 5, 2016, 3:08pm UTC](https://forum.getkirby.com/t/filters-with-forms-problem-with-pluck/4876/20 "2016-08-05T15:08:02Z")

</div>

You’re amazing! Thanks again for all you help.

[Next page](https://forum.getkirby.com/t/filters-with-forms-problem-with-pluck/4876.md?page=2)
