php - 具有多个搜索过滤器的 Cake PHP3 分页

标签 php cakephp pagination cakephp-3.0

在“我的搜索”中有四个过滤器。获得搜索结果后如何对搜索词进行分页?这意味着如果我转到第二页 分页搜索文件管理器表单数据将不会出现并显示以下错误

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

Controller 代码

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 数据丢失并且数据列表显示错误。

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

申报表

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

最佳答案

之所以提交表单时没有出现错误,是因为$_POST服务器中存在变量 但如果你点击页面按钮 IE <a href="search?page=2">2</a> $_POST将会消失

这就是为什么会发生这种情况

Notice (8): Undefined index: to [APP/Controller/Admin/ScheduleController.php, line 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而是得到 $_GET使用 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']);

}

希望这有帮助。

关于php - 具有多个搜索过滤器的 Cake PHP3 分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40840111/

相关文章:

cakephp - 设置默认模型条件

php - cakephp中通过外键显示数据

mysql - 需要有关 CakePHP 中的条件的一些帮助!

Linq 选择记录范围

php - 拉维尔 5.4 : execute a raw query and show data in view

php - 从数据库 php mysql 查询显示类别

php - 如何访问对象数据 php cakephp 3.1

amazon-web-services - 使用 DynamoDBMapper Java AWS SDK 进行分页

javascript - 检查是否存在任意 Controller / Action

php - CKeditor 表单将帖子添加到数据库时出现问题