Filter based on select option in index ctp

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

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

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

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

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:

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

the result still null and undefne variable refkel

: 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 ?

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

  $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);
  }

}

thanx @dirk your suggest is working,

if i mixed the suggest below !

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

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

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.

thanx @dirk so should can I do, I confused

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

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#=

then I have modified the code below

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

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

'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

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/ or can be called from your form or it calls itself via the redirect.