gpt4 book ai didi

php - 无法在 cakePHP 中对数据进行排序和分页

转载 作者:搜寻专家 更新时间:2023-10-31 20:45:52 24 4
gpt4 key购买 nike

首先让我知道我是 cakePHP 的完全初学者。我在 cakePHP 中创建了一个名为 students 的表,我只想有可排序的标题,所以我使用了分页器帮助类。不幸的是,我收到以下警告,我只是不明白为什么,而且排序不起作用。

Warning (2)
: array_filter() expects parameter 1 to be array, null given
[CORE\Cake\View\Helper\PaginatorHelper.php, line 395]

Warning (2)
: array_merge() [
function.array-merge
]: Argument #1 is not an array [CORE\Cake\View\Helper\PaginatorHelper.php, line 395]

这是我的 Controller

enter code here
<?php
class StudentsController extends AppController{
public $helpers = array('Html', 'Form', 'Session', 'Paginator');
public $components = array('Session', 'Paginator');



public function index(){
$this->set('students', $this->Student->find('all'));
}

public function view($id = NULL){
$this->Student->id = $id;
$this->set('student', $this->Student->read());
}

public function add(){
if($this->request->is('post')){
$this->Student->create();
if($this->Student->save($this->request->data)){
$this->Session->setFlash('The student has been added.');
$this->redirect(array('action' => 'index'));
}else{
$this->Session->setFlash('Unable to add the student');
}
}
}

public function edit($id = NULL){
$this->Student->id = $id;
if($this->request->is('get')){
$this->request->data = $this->Student->read();
}else{
if($this->Student->save($this->request->data)){
$this->Session->setFlash('The student has been edited');
$this->redirect(array('action' => 'index'));
}else{
$this->Session->setFlash('Unable to edit the student');
}
}
}

public function delete($id = NULL){
if($this->request->is('get')){
throw new MethodNotAllowedException;
}else{
if($this->Student->delete($id)){
$this->Session->setFlash('The Student has been succesfully deleted');
$this->redirect(array('action' => 'index'));
}
}
}
}

?>

这是我的观点,实际上是我的索引

<h1>Welcome to the Students Page</h1>

<?php echo $this->Html->link('Add Student', array('controller' => 'students', 'action' => 'add')); ?>

<table>
<tr>
<th><?php echo $this->Paginator->sort('id', 'Matriculation Number'); ?></th>
<th>Last Name</th>
<th>First Name</th>
<th>Course Of Studies</th>
<th>Email</th>
<th>Remark</th>
</tr>
<?php
foreach($students as $student):
?>
<tr>
<td><?php echo $student['Student']['id']?></td>
<td><?php echo $student['Student']['last_name']?></td>
<td><?php echo $student['Student']['first_name']?></td>
<td><?php echo $student['Student']['course_of_studies']?></td>
<td><?php echo $student['Student']['email']?></td>
<td><?php echo $student['Student']['remark']?></td>
<td><?php echo $this->Html->link($student['Student']['id'], array('controller' => 'students', 'action' => 'view', $student['Student']['id'])); ?>
<td><?php echo $this->Html->link('Edit', array('controller' => 'students', 'action' => 'edit', $student['Student']['id']));?></td>
<td><?php echo $this->Form->postLink('Delete', array('controller' => 'students', 'action' => 'delete', $student['Student']['id']), array('confirm' => 'Are you sure ?')); ?></td>
</tr>
<?php endforeach; ?>
<?php unset($student); ?>
</table>

我也用过下面的

<?php echo $this->Paginator->sort('id', 'Matriculation Number', array('model' => 'Student')); ?>
<?php echo $this->Paginator->sort('Student.id', 'Matriculation Number'); ?>

我仍然得到同样的错误。我在这里错过了一些非常简单的东西吗???因为我一直在谷歌上搜索几个小时,但没有运气......

最佳答案

您实际上并没有使用分页器组件,因此它没有设置助手使用的所需请求数据。将您的索引函数更改为看起来像

public function index(){
$this->set('students', $this->paginate());
// or $this->set('students', $this->Paginator->paginate());
}

关于php - 无法在 cakePHP 中对数据进行排序和分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13407225/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com