I am currently facing a little problem. let me explain : I develop a database under cakphp 2.9 I use a custom search function, the problem is that when I click on the next page I have an error that appears (the first page is displayed without worries) this is my controller page : 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);
}
}
this is my view file
<?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"> </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>
now this is this the error i got when a click on next page
Notice (8): Undefined variable: joueurs [APP/View/Elements/resume_search_club.ctp, line 174]
Warning (2): Invalid argument supplied for foreach() [APP/View/Elements/resume_search_club.ctp, line 174]
I've been looking for a solution for several days, so please I really need your help on this one
Thanks in advance
Look closely at what your code is doing, when POST data is present, it will fetch data from the database in a paginated form, and set it as a view variable for use in your templates. Consequently it won't do any of that if no POST data is present, and that will of course be the case when you follow a (pagination) link, hence you'll receive those errors, as the data isn't being queried and the view variable isn't being set.
Long story short, use GET data (ie the query string) to pass the search term instead, either use GET data in the first place, ie change your form to use GET instead of POST, or use the PRG (Post-Redirect-Get) pattern to transform your POST request to a GET request. Also you should have some sort of fail-safe so that things don't error out when the search term is missing, or the view variable isn't set.
Simple example with PRG:
Also make sure that the search term is being integrated in the URLs generated by the paginator helper, if it's not being included by default, you can modify the paginator options and force it into the URL, like this: