1

I just started learning PHP,cakePHP and I am unable to sort my page. My Controller is as follows;

public $helpers = array('Html', 'Form', 'Flash', 'Paginator');
public function index() 
{
    $this->Post->recursive = 0;
    $this->paginate = array ('limit' => 5);
    $this->set('posts', $this->paginate());
    $this->set('posts', $this->Post->find('all'));
    echo $this->Paginator->sort('id');
}

Paginate is working fine, but for sort I get a fatal error as follows

Call to undefined method PaginatorComponent::sort()

could not find anything related to the error as well. It might be really basic but it will really help me to learn more, and I want to learn more! Any help will be appreciated. Thank you in advance!

Update:

I added the following code in my index view,

<th><?php echo $paginator->sort('Id', 'id'); ?></th>

But now I am getting the following error Call to a member function sort() on null

4

2 回答 2

2

我相信您正在尝试设置分页默认值。这样做的正确方法是在您的控制器中定义:

public $paginate = array(
    'order' => array(
        'Post.id' => 'desc'
    )
);

或在行动中:

$this->paginate = array (
   'limit' => 5,
   'order' => array(
        'Post.id' => 'desc'
    )
);

您的索引操作应如下所示:

public function index() {
    $this->Post->recursive = 0;
    $this->paginate = array (
       'limit' => 5,
       'order' => array(
           'Post.id' => 'desc'
       )
    );
    $this->Paginator->settings = $this->paginate;
    $this->set('posts', $this->paginate());
}

在您看来,CakePHP 2.x 的正确语法是

<?php echo $this->Paginator->sort('id'); ?>
于 2015-12-04T13:46:31.440 回答
0

在我的索引()

$this->paginate = array ('limit' => 5);

后来在我使用的索引视图中

<?php echo $this->Paginator->sort('id','Id'); ?>

语法:this->Paginator->sort('列名','标签名');

而不是普通的html代码。它奏效了!

于 2015-12-07T03:29:08.390 回答