# Filter based on select option in index ctp

**URL:** <https://discourse.cakephp.org/t/filter-based-on-select-option-in-index-ctp/9110>\
**Category:** Need Help\
**Created:** [March 11, 2021, 6:29am UTC](https://discourse.cakephp.org/t/filter-based-on-select-option-in-index-ctp/9110 "2021-03-11T06:29:44Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![arizzoye](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@arizzoye](https://discourse.cakephp.org/u/arizzoye)\
**Post date:** [March 11, 2021, 6:29am UTC](https://discourse.cakephp.org/t/filter-based-on-select-option-in-index-ctp/9110/1 "2021-03-11T06:29:44Z")

</div>

I want to filter data in index ctp that used select options.  
I’m put in same index ctp

```auto
echo $this->Form->create(null,array('type' => 'get', 'valueSources' => 'query'));
echo "<select name='refkel id='refKel'>";
echo "<option value='1'>TANGERANG</option>";
echo "<option value='2'>PINANG</option>";
echo "</select>";
echo $this->Html->link(__('KIRIM'),['controller' => 'postcodes', 'action' => 'index']);
echo $this->Form->end();

```

inside index controller

```auto
public function index controller
{
  $data = $this->request->getData();
  $refdist = $data['refkel'];
  debug($refdist);
  $query = $this->Postcodes->find()->where(['district_id' => $refdist]);
  $this->set(compact('query'));
}

```

getting result I’m used `debug($refdist);` is below

```auto
Undefined index: refkel [APP/Controller\PostcodesController.php, line 25]

\src\Controller\PostcodesController.php (line 26)
null

```

I hope someone to help me and giving idea, thanx

---

<div class="post-metadata">

**Author:** ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)\
**Post date:** [March 11, 2021, 7:58am UTC](https://discourse.cakephp.org/t/filter-based-on-select-option-in-index-ctp/9110/2 "2021-03-11T07:58:16Z")

</div>

As your Form uses the “get” method, the data will not be in $this-\>request-\>getData(), but in $this-\>request-getQuery();

In your Controller this should work:

```auto
  $data = $this->request->getQuery();
  $refdist = $data['refkel'];
  debug($refdist);

```

---

<div class="post-metadata">

**Author:** ![arizzoye](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@arizzoye](https://discourse.cakephp.org/u/arizzoye)\
**Post date:** [March 11, 2021, 8:13am UTC](https://discourse.cakephp.org/t/filter-based-on-select-option-in-index-ctp/9110/3 "2021-03-11T08:13:43Z")

</div>

the result still null and undefne variable refkel

```auto
: Undefined index: refkel [APP/Controller\PostcodesController.php, line 25]

\src\Controller\PostcodesController.php (line 26)
null

```

is there above form helper that I was wrong ?

---

<div class="post-metadata">

**Author:** ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)\
**Post date:** [March 11, 2021, 8:35am UTC](https://discourse.cakephp.org/t/filter-based-on-select-option-in-index-ctp/9110/4 "2021-03-11T08:35:55Z")

</div>

Everytime you call your Postcodes-Controller, this lines of Code will be executed:

```auto
  $data = $this->request->getData();
  $refdist = $data['refkel'];
  debug($refdist);
  $query = $this->Postcodes->find()->where(['district_id' => $refdist]);
  $this->set(compact('query'));

```

So at the first call “$this-\>request-\>getData(‘refkel’)” is empty and will bring the error you mentioned.

Change it to:

> ```
> public function index()
> {
> 
> if (!is_NULL($this->request->getQuery('refkel')))
> {
> debug($this->request->getData());
> debug($this->request->getquery('refkel'));
> $data=$this->request->getQuery();
> debug($data);
> $refdist=$data['refkel'];
> debug($refdist);
> }
> 
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![arizzoye](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@arizzoye](https://discourse.cakephp.org/u/arizzoye)\
**Post date:** [March 11, 2021, 9:25am UTC](https://discourse.cakephp.org/t/filter-based-on-select-option-in-index-ctp/9110/5 "2021-03-11T09:25:10Z")

</div>

thanx @dirk your suggest is working,

if i mixed the suggest below !

```auto
if (!is_NULL($this->request->getQuery('refkel'))) 
{
  debug($this->request->getData());
  debug($this->request->getQuery('refkel'));
  $data=$this->request->getQuery();
  debug($data);
  $refdist=$data['refkel'];
  debug($refdist);
  $query = $this->Postcodes->find()->where(['district_id' => $refdist]);
 debug($query);
  $this->set(compact('query'));		
}

```

then show the error message

```auto
: Undefined variable: query [APP/Template\Postcodes\index.ctp, line 133]
Warning (2): Invalid argument supplied for foreach() [APP/Template\Postcodes\index.ctp, line 133]

```

---

<div class="post-metadata">

**Author:** ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)\
**Post date:** [March 11, 2021, 9:55am UTC](https://discourse.cakephp.org/t/filter-based-on-select-option-in-index-ctp/9110/7 "2021-03-11T09:55:13Z")

</div>

Your variable $query is only sent from controller to your template, if the template send data to your controller.

So the first time you call “http://…postcodes” your template has no variable $query.

---

<div class="post-metadata">

**Author:** ![arizzoye](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@arizzoye](https://discourse.cakephp.org/u/arizzoye)\
**Post date:** [March 11, 2021, 10:18am UTC](https://discourse.cakephp.org/t/filter-based-on-select-option-in-index-ctp/9110/8 "2021-03-11T10:18:59Z")

</div>

thanx @dirk so should can I do, I confused

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [March 11, 2021, 3:27pm UTC](https://discourse.cakephp.org/t/filter-based-on-select-option-in-index-ctp/9110/9 "2021-03-11T15:27:55Z")

</div>

```auto
if (isset($query)) { ...
```

---

<div class="post-metadata">

**Author:** ![arizzoye](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@arizzoye](https://discourse.cakephp.org/u/arizzoye)\
**Post date:** [March 12, 2021, 9:16am UTC](https://discourse.cakephp.org/t/filter-based-on-select-option-in-index-ctp/9110/10 "2021-03-12T09:16:08Z")

</div>

I was browsing in stackoverflow [https://stackoverflow.com/questions/42896451/how-to-show-data-on-select-from-a-dropdown-in-index-page-in-cakephp-3#_=_](https://stackoverflow.com/questions/42896451/how-to-show-data-on-select-from-a-dropdown-in-index-page-in-cakephp-3#_=_)

then I have modified the code below

```auto
public function index($district_id = null)
{
    if($this->request->is(['post'])){
        return $this->redirect(['action'=>'index', $this->request->data['district_id']]);
    }
    $this->loadModel('Districts');
    $districts = $this->Districts->find('list');
    $postcodes = $this->Postcodes->find('all', ['conditions' => ['Postcodes.district_id' => 
    $district_id]]);
   debug($postcodes);
   $this->paginate = [
      'contain' => ['Countries', 'Provinces', 'Regions', 'Districts', 'Subdistricts'],
   ];
   $postcodes = $this->paginate($this->Postcodes);
   $this->set(compact('district_id', 'postcodes', 'districts'));

}

```

index.ctp below

```auto
<?php
  echo $this->Form->create(null);
  echo $this->Form->control('district_id', array('type' => 'select', 'label' => false, 'multiple' => false, 'empty' => true, 'options' => $districts, 'value' => $district_id ));
  echo $this->Html->link(__('KIRIM'),['controller' => 'postcodes', 'action' => 'index']);
  echo $this->Form->end();
?>

<?php foreach ($postcodes as $postcode): ?>
<tr>
  <td><?= $this->Number->format($postcode->ID) ?></td>
  <td><?= h($postcode->country->Negara) ?></td>
  <td><?= h($postcode->province->Provinsi) ?></td>
  <td><?= h($postcode->region->Kabupaten) ?></td>
  <td><?= h($postcode->district->Kecamatan) ?></td>
  <td><?= h($postcode->subdistrict->Keurahan) ?></td>
  <td><?= h($postcode->Kodepos) ?></td>
</tr>
<?php endforeach; ?>

```

when I used `debug($postcodes)` the params was sent but contain `null`

```auto
'params' => [
  ':c0' => [
	'value' => null,
	'type' => 'integer',
	'placeholder' => 'c0'
 ]

```

all the record is show display without filter `$district_id` i want to filter is working.

any more idea @Zuluru and @dirk

---

<div class="post-metadata">

**Author:** ![dirk](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dirk/32/2174_2.png) [@dirk](https://discourse.cakephp.org/u/dirk)\
**Post date:** [March 12, 2021, 11:40am UTC](https://discourse.cakephp.org/t/filter-based-on-select-option-in-index-ctp/9110/11 "2021-03-12T11:40:32Z")

</div>

the content of $postcode depends on $district\_id.  
You should look through your controller code and consider which value $district\_id has, depending on from where the controller was called.  
as far as I see, your controller can be called directly via [http://localhost/project/postcodes/](http://localhost/project/postcodes/) or can be called from your form or it calls itself via the redirect.
