蛋糕PHP3分页与多个搜索过滤器

时间:2016-11-28 08:42:02

标签: php cakephp pagination cakephp-3.0

在我的搜索中有四个过滤器。获取搜索结果后,如何使用搜索字词进行分页?这意味着如果我去第二页 分页搜索文件管理器表单数据不会出现并显示以下错误

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

控制器代码

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文件

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

上面的代码生成了分页,但是当它在分页结果中转到下一页时,搜索POST数据丢失,数据列表显示错误。

如何使用过滤器获取分页结果列表?

Filer表格

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

1 个答案:

答案 0 :(得分:3)

提交表单时未发生错误的原因是服务器中存在$_POST变量 但如果您点击页面按钮IE <a href="search?page=2">2</a>$_POST将会消失

发生这种情况的原因

  

注意(8):未定义的索引:到[APP / Controller / Admin / ScheduleController.php,第257行]

修正将您的方法更改为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() ?>

通过这种方式,当您提交表单数据时,会将您的网址写入您的网址 search?page=1&company=value&gig=value&etc...

并使用$this->request->query代替使用cakephp获取$_GET变量

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']);

}

希望这有帮助。