【问题标题】:unable to passe page 2 by paginator on cakephp 2.9无法通过 cakephp 2.9 上的 paginator 传递第 2 页
【发布时间】:2020-05-18 12:38:43
【问题描述】:

我目前面临一个小问题。让我解释 : 我在cakphp 2.9下开发数据库我使用自定义搜索功能,问题是点击下一页时出现错误(第一页显示无忧) 这是我的控制器页面:UsersController.php

public function search_club()
{
    if (!empty($this->request->data)) {
        $info1 = trim($this->request->data['Joueur']['CLUB']);
        //debug($info1); die();
        $joueurs = $this->Joueur->find('all', [
            'conditions' => [
                'AND' => [
                    'LOWER(Joueur.CLUB) LIKE' => '%' . strtolower($info1) . '%',
                    'Joueur.STATU' => '1'
                ]
            ]
        ]);

        //debug($joueurs); die();
        $this->Joueur->recursive = 0;
        $this->paginate = [
            'limit' => 20,
            'conditions' => [
                'and' => [
                    'Joueur.STATU' => '1',
                    'LOWER(Joueur.CLUB) LIKE' => '%' . strtolower($info1) . '%'
                ]
            ]
        ];

        $joueurs = $this->paginate('Joueur');
        $this->set('joueurs', $joueurs);
    }
}

这是我的视图文件

<?php
 $paginator = $this->Paginator;

 foreach ($joueurs as $joueur):
    echo'<tbody>';
echo'<tr>';

        echo '<td class="hidden-480">
        <center>';
            echo $joueur['Joueur']['LICENCES'];
        echo'</center>
        </td> ';
        echo '<td class="hidden-480">
        <center>';
            echo $joueur['Joueur']['NOM'];
        echo'</center>
        </td> ';
        
        echo '<td class="hidden-480">
        <center>';
            echo $joueur['Joueur']['PRENOM'];
        echo'</center>
        </td> ';
        echo '<td class="hidden-480">
        <center>';
            echo $joueur['Joueur']['CLUB'];
        echo'</center>
        </td> ';
        echo'<td>
        <center>';
        echo $joueur['Joueur']['SEXE'];
        echo'</center>
        </td>';
        echo'<td>
        <center>';
        echo $joueur['Joueur']['NATIONALITE'];
        echo'</center>
        </td>';
        echo'<td>
        <center>';
        echo $joueur['Joueur']['PROVEN'];
        echo'</center>
        </td>';
         echo'<td>
        <center>
        <i class="ace-icon fa fa-eye-o">&nbsp;</i>';
        echo $this->Html->link('Modifier/Voir',
            array('controller' => 'users', 'action' => 'edit_play', $joueur['Joueur']['id']),array('target' => '_blank'));
        echo'</center>
        </td>'; 
        echo '<td class="hidden-480">
        <center>';
         echo $this->Form->postLink(__('Supprimer'), array('action' => 'delet_play', $joueur['Joueur']['id']), null, __('Etes vous sûr de vouloir supprimé cet élément ?', $joueur['Joueur']['PRENOM']));  
        echo'</center>
        </td> 
    </tr>
    </tbody>';
    endforeach;
    unset($joueur);

        echo'</table>


    </div>
        </div>
    </div>
</div>';
echo'
<div style="height:50px!important">

</div>';
 
// pagination section
    echo "<div class='paging' style='text-align:center'>";
 
        // the 'first' page button
        echo $paginator->first("Debut");
         
        // 'prev' page button, 
        // we can check using the paginator hasPrev() method if there's a previous page
        // save with the 'next' page button
        if($paginator->hasPrev()){
            echo $paginator->prev("Precedent");
        }
         
        // the 'number' page buttons
        echo $paginator->numbers(array('modulus' => 3));
         
        // for the 'next' button
        if($paginator->hasNext()){
            echo $paginator->next("Suivant");
        }
         
        // the 'last' page button
        echo $paginator->last("Fin");
     
    echo "</div>";("Fin");

  
     
  echo" </div>
    </div>";
 ?>
</div>

现在这是我点击下一页时遇到的错误

注意(8):未定义变量:joueurs [APP/View/Elements/resume_search_club.ctp,第 174 行]

警告 (2):为 foreach() 提供的参数无效 [APP/View/Elements/resume_search_club.ctp,第 174 行]

几天来我一直在寻找解决方案,所以我真的需要你的帮助

提前致谢

【问题讨论】:

  • 请贴出完整的控制器方法代码,不要剪掉(可能半途而废)。还要尝试正确格式化/缩进它,这有助于发现流量控制问题。
  • 这是完整的控制器方法代码,我只是忘记添加这些函数的末尾,我现在将更新......

标签: cakephp cakephp-2.9


【解决方案1】:

仔细查看您的代码在做什么,当 POST 数据存在时,它将以分页形式从数据库中获取数据,并将其设置为视图变量以在您的模板中使用。因此,如果不存在 POST 数据,它不会执行任何操作,当然,当您点击(分页)链接时,您会收到这些错误,因为数据没有被查询并且未设置视图变量。

长话短说,使用 GET 数据(即查询字符串)来传递搜索词,或者首先使用 GET 数据,即将表单更改为使用 GET 而不是 POST,或者使用 PRG (Post-Redirect-Get) pattern 来将您的 POST 请求转换为 GET 请求。此外,您应该具有某种故障安全功能,以便在缺少搜索词或未设置视图变量时不会出错。

使用 PRG 的简单示例:

public function search_club()
{
    // Request with POST form data
    $term = $this->request->data('Joueur.CLUB');
    if ($term) {
        // Redirect as GET query string data
        return $this->redirect([
            '?' => [
                'term' => $term
            ]
        ]);
    }

    $conditions = [
        'Joueur.STATU' => '1',
    ];

    $term = $this->request->query('term');
    if ($term) {
        $conditions += [
            'LOWER(Joueur.CLUB) LIKE' => '%' . strtolower($term) . '%'
        ];
    }

    $this->paginate = [
        'limit' => 20,
        'conditions' => $conditions,
    ];

    $this->Joueur->recursive = 0;
    $joueurs = $this->paginate('Joueur');
    $this->set('joueurs', $joueurs);
}

还要确保搜索词被集成到分页器助手生成的 URL 中,如果默认不包含,您可以 modify the paginator options 并将其强制到 URL 中,如下所示:

$this->Paginator->options([
    'url' => [
        '?' => [
            'term' => $this->request->query('term'),
        ],
    ],
]);

【讨论】:

  • 我刚刚注意到搜索没有发生,所有元素看起来好像没有过滤器,但是我确认分页工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-18
  • 1970-01-01
  • 2012-11-04
  • 2021-03-24
  • 2012-10-17
  • 2016-01-20
相关资源
最近更新 更多