Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将cakephp默认分页设置为数组_Php_Arrays_Cakephp_Pagination - Fatal编程技术网

如何将cakephp默认分页设置为数组

如何将cakephp默认分页设置为数组,php,arrays,cakephp,pagination,Php,Arrays,Cakephp,Pagination,我使用的是CakePHP2.5.6,我想在我的视图文件中显示数组。我尝试将cakephp默认分页设置为这个数组,但它不起作用。下面是我的示例代码 ---'my_pages' controller----- public function view(){ //some other code $resultArray = $this->MyPages->getResults(); $resultArray = $this->Paginator->

我使用的是CakePHP2.5.6,我想在我的视图文件中显示数组。我尝试将cakephp默认分页设置为这个数组,但它不起作用。下面是我的示例代码

---'my_pages' controller-----

public function view(){
    //some other code
     $resultArray = $this->MyPages->getResults();
     $resultArray = $this->Paginator->paginate('resultArray');
     $this->set(compact('resultArray','rightPanel'));
}
和我的视图文件

----- 'view.ctp' file ------
//some other code

foreach ($resultArray as $result){
   echo $result['subject'];
   echo $result['body'];
}

在我的例子中,这个
$resultArray
有将近100个元素,我想在这个页面上设置分页。那么在这种情况下是否可以使用cakephp默认分页??谢谢:)

您需要实现自定义分页

CakePHP使用两种方法来管理分页查询,即paginate和paginateCount,它们用于获取页面数据和总计 分别记录计数。使分页与您的自定义项一起工作 查询时,我们需要在模型中实现上述两个功能 要对其进行分页以处理自定义查询的文件。 也可以在行为文件中实现。让我们看看怎么做 我们已经实施了这一点

您的控制器文件如下所示:

注意:MyPage应该是MyPage,因为Cakephp模型名称应该是单数

您的视图文件如下所示:

您也可以阅读

谢谢:),当我给出您的答案时,它起作用了。但在查看页面时,每页的项目都无法正常工作,我尝试了许多方法,但似乎无法正常工作。使用调试器可以显示分页限制和其他变量是否正确。但当前页面显示9。不明白为什么会这样,你知道为什么会这样吗。
//in MyPages model add

public function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) {    
    $recursive = -1;

    // Mandatory to have
    $this->useTable = false;
    $sql = '';

    $sql .= "SELECT * FROM table_name limit ";

    // Adding LIMIT Clause
    $sql .= (($page - 1) * $limit) . ', ' . $limit;

    $results = $this->query($sql);

    return $results;
}
public function paginateCount($conditions = null, $recursive = 0, $extra = array()) {

    $sql = '';

    $sql .= "SELECT * FROM table_name";

    $this->recursive = $recursive;

    $results = $this->query($sql);

    return count($results);
}
---'my_pages' controller-----

public function view(){
    // Do not forgot to set this, not sure why
    $this->MyPages->recursive = 0;
    // Setting up paging parameters
    $this->paginate = array('MyPages'=>array('limit'=>5));
    // Getting paginated result based on page #
    $this->set('resultArray', $this->paginate('MyPages'));
}
----- 'view.ctp' file ------
//some other code

foreach ($resultArray as $result){
  echo $result['subject'];
  echo $result['body'];
}