Miuranga Miuranga - 13 days ago 7
PHP Question

Cake PHP3 Pagination with multiple search filters

In My search there are four filters. After getting the search results how to paginate with the search terms ? It mean If I go to second page of the
pagination the search filer form data will not appear and show the below error

Notice (8): Undefined index: to [APP/Controller/Admin/ScheduleController.php, line 257]


Controller Code

public function search() {

$this->viewBuilder()->template('allschedule');

$scheduleto = date('Y-m-d', strtotime($this->request->data['to']." +1 days"));
$schedulefrom =date('Y-m-d', strtotime($this->request->data['from']));

if($this->request->data['company'] != 'all' && $this->request->data['gig'] == 'all')
{
$searchfilter = array(
'Schedule.shift_from > ' => $schedulefrom,
'Schedule.shift_from < ' => $scheduleto,
'Job.company' =>$this->request->data['company'],
'Schedule.status !=' =>0,
);
}
elseif($this->request->data['company'] == 'all' && $this->request->data['gig'] != 'all')
{
$searchfilter = array(
'Schedule.shift_from > ' => $schedulefrom,
'Schedule.shift_from < ' => $scheduleto,
'Schedule.gig_id' =>$this->request->data['gig'],
'Schedule.status !=' =>0,
);

}
elseif($this->request->data['company'] != 'all' && $this->request->data['gig'] != 'all')
{
$searchfilter = array(
'Job.company' =>$this->request->data['company'],
'Schedule.gig_id' =>$this->request->data['gig'],
'Schedule.shift_from > ' => $schedulefrom,
'Schedule.shift_from < ' => $scheduleto,
'Schedule.status !=' =>0,
);

}
else
{
$searchfilter = array(
'Schedule.shift_from > ' => $schedulefrom,
'Schedule.shift_from < ' => $scheduleto,
'Schedule.status !=' =>0,
);

}


$this->paginate = [
'fields' => ['User.name', 'Schedule.shift_from', 'Schedule.id', 'Schedule.shift_to', 'Schedule.shift_description', 'Schedule.checkin', 'Schedule.checkout', 'Schedule.start', 'Schedule.stop', 'Schedule.comment', 'Schedule.attested'],
'conditions' => $searchfilter,
'contain' => ['Job','User'],
'order' => [
'shift_from' => 'ASC'
],
'limit' => 30 //'limit'
];
$schedules = $this->paginate($this->Schedule);

$this->set(compact('schedules'));
$this->set('_serialize', ['schedules']);

}


CTP file

<div class="pagination pagination-large">
<ul>
<?php
echo $this->Paginator->prev(__('prev'), array('tag' => 'li'), null, array('tag' => 'li','class' => 'disabled','disabledTag' => 'a'));
echo $this->Paginator->numbers(array('separator' => '','currentTag' => 'a', 'currentClass' => 'active','tag' => 'li','first' => 1));
echo $this->Paginator->next(__('next'), array('tag' => 'li','currentClass' => 'disabled'), null, array('tag' => 'li','class' => 'disabled','disabledTag' => 'a'));
?>
</ul>
</div>


The above code generate the pagination but when it goes to next page in pagination results the search POST data is missing and data list show the error.

How could I get the pagination results list with the filter ?

Filer Form

<?php echo $this->Form->create('Schedule', array('type' => 'post', 'action' => 'search', 'class'=> 'form-inline'));?>
<div class="form-group">
<div class="form-group" style="padding: 0 0;">
<span style="font-size:12px; display: block;">Company</span>
<?php

echo $this->Form->select('company', $companyarray, array('id' => 'company','escape' => false,'class'=> 'form-control input-sm', 'style'=>'width:160px'));
?>
</div>
<div class="form-group" id="gigselect" style="padding: 0 10px;">
<span style="font-size:12px; display: block;">Gig</span>
<?php

echo $this->Form->select('gig', $searchgigarray, array('id' => 'gig','escape' => false,'class'=> 'form-control input-sm','style'=>'width:160px'));
?>
</div>


<div class="form-group" style="padding: 0 0;">
<span style="font-size:12px; display: block;">From</span>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
<input id="from" name="from" type="text" value="<?php echo $backdate;?>" class="form-control input-sm" style="width:120px">
</div>
</div>
<div class="form-group" style="padding: 0 10px;">
<span style="font-size:12px; display: block;">To</span>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-calendar"></i></span>
<input id="to" name="to" type="text" value="<?php echo $frontdate;?>" class="form-control input-sm" style="width:120px">
</div>
</div>
<div class="form-group" style="padding: 0 0; margin-top:15px;">
<div class="input-group">
<?php echo $this->Form->submit('OK',array('class'=>'btn btn-success','style'=>"height: 2em;")); ?>
</div>
</div>
</div>
<?php echo $this->Form->end() ?>

Answer

The reason why the error did not occur when you submit the form is because the $_POST variables exist in the server but if you click the page button IE <a href="search?page=2">2</a> the $_POST will gone

That's why this occurs

Notice (8): Undefined index: to [APP/Controller/Admin/ScheduleController.php, line 257]

to fix that change your method to GET

<?php echo $this->Form->create('Schedule', array('type' => 'get', 'action' => 'search', 'class'=> 'form-inline'));?>
<div class="form-group">
         <div class="form-group" style="padding: 0 0;">
  <span style="font-size:12px; display: block;">Company</span>
  <?php

                        echo $this->Form->select('company', $companyarray, array('id' => 'company','escape' => false,'class'=> 'form-control input-sm', 'style'=>'width:160px'));
                    ?>
</div>
<div class="form-group" id="gigselect" style="padding: 0 10px;">
   <span style="font-size:12px; display: block;">Gig</span>
  <?php

                        echo $this->Form->select('gig', $searchgigarray, array('id' => 'gig','escape' => false,'class'=> 'form-control input-sm','style'=>'width:160px'));
                    ?>
</div>


<div class="form-group" style="padding: 0 0;">
<span style="font-size:12px; display: block;">From</span>
<div class="input-group">
        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
        <input id="from" name="from" type="text" value="<?php echo $backdate;?>" class="form-control input-sm" style="width:120px">
 </div>
</div>
<div class="form-group" style="padding: 0 10px;">
    <span style="font-size:12px; display: block;">To</span>
    <div class="input-group">
            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
            <input id="to" name="to" type="text" value="<?php echo $frontdate;?>" class="form-control input-sm" style="width:120px">
     </div>
</div>
<div class="form-group" style="padding: 0 0; margin-top:15px;">
    <div class="input-group">
       <?php echo $this->Form->submit('OK',array('class'=>'btn btn-success','style'=>"height: 2em;")); ?> 
    </div>
</div>
</div>
<?php echo $this->Form->end() ?>

In this way when you submit your form data will be written in to your url like
search?page=1&company=value&gig=value&etc...

and use $this->request->query instead to get the $_GET variables using cakephp

public function search() {

        $this->viewBuilder()->template('allschedule');

        $scheduleto = date('Y-m-d', strtotime($this->request->query['to']." +1 days"));
        $schedulefrom =date('Y-m-d', strtotime($this->request->data['from']));

        if($this->request->query['company'] != 'all' && $this->request->query['gig'] == 'all')
        {
            $searchfilter = array(
                    'Schedule.shift_from > ' => $schedulefrom,
                    'Schedule.shift_from < ' => $scheduleto,
                    'Job.company' =>$this->request->query['company'],
                    'Schedule.status !=' =>0,
                    );
        }
        elseif($this->request->query['company'] == 'all' && $this->request->query['gig'] != 'all')
        {
            $searchfilter = array(
                    'Schedule.shift_from > ' => $schedulefrom,
                    'Schedule.shift_from < ' => $scheduleto,
                    'Schedule.gig_id' =>$this->request->query['gig'],
                    'Schedule.status !=' =>0,
                    );

        }
        elseif($this->request->query['company'] != 'all' && $this->request->query['gig'] != 'all')
        {
            $searchfilter = array(
                    'Job.company' =>$this->request->query['company'],
                    'Schedule.gig_id' =>$this->request->query['gig'],
                    'Schedule.shift_from > ' => $schedulefrom,
                    'Schedule.shift_from < ' => $scheduleto,
                    'Schedule.status !=' =>0,
                    );

         }
        else
        {
            $searchfilter = array(
                    'Schedule.shift_from > ' => $schedulefrom,
                    'Schedule.shift_from < ' => $scheduleto,
                    'Schedule.status !=' =>0,
                    );

        }


    $this->paginate = [
        'fields' => ['User.name', 'Schedule.shift_from', 'Schedule.id', 'Schedule.shift_to', 'Schedule.shift_description', 'Schedule.checkin', 'Schedule.checkout', 'Schedule.start', 'Schedule.stop', 'Schedule.comment', 'Schedule.attested'],
        'conditions' => $searchfilter,
        'contain' => ['Job','User'],
        'order' => [
            'shift_from' => 'ASC'
        ],
        'limit' => 30  //'limit' 
    ];
    $schedules = $this->paginate($this->Schedule);

        $this->set(compact('schedules'));
        $this->set('_serialize', ['schedules']);

}

Hope this helps.