2016-11-28 64 views
2

在我的搜索中有四個過濾器。獲得搜索結果後,如何與搜索條件分頁?它的意思是如果我去 分頁搜索過濾器表單數據的第二頁將不會出現,並顯示以下錯誤蛋糕PHP3分頁與多個搜索過濾器

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數據丟失,數據列表顯示錯誤。

我怎樣才能得到帶過濾器的分頁結果列表?

文件管理器表

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

你可以在這裏包括你的表格 – Beginner

+0

'$ searchfilter'值不是來自$ _GET'。它來自下面 'search?page = 4&sort = Schedule.shift_from&direction = ASC' – Miuranga

+0

實際上,如果您單擊頁面按鈕$ _POST數據將不再存在,這就是爲什麼您必須使用$ _GET而不是$ _POST – Beginner

回答

3

爲什麼當你提交表單是因爲$_POST變量在服務器 存在沒有發生錯誤,但如果你點擊頁面按鈕,IE <a href="search?page=2">2</a>$_POST將原因走了

這就是爲什麼發生這種情況

通知(8):未定義指數:對[APP /控制器/管理/ 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']); 

} 

希望這會有所幫助。

+0

謝謝,它解決了主要問題 – Miuranga