如何将cakephp默认分页放入数组
How to put cakephp default pagination to array
我正在使用 cakephp 2.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
有近百个元素,我想在此页面上设置分页。那么在这种情况下是否可以使用 cakephp 默认分页?谢谢:)
您需要实现 cakephp 自定义分页。
CakePHP uses two method to manage pagination queries, that are paginate and paginateCount, they are used to get page data and total
record count respectively. To have pagination work with your custom
queries, we will need to implement both above functions in our model
file where you want to have pagination to work with custom queries.
You can implement in your behavior file as well. Let’s see how we can
have this implemented.
//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'));
}
Note : MyPages should be MyPage, because Cakephp model name should be singular.
您的视图文件如下所示:
----- 'view.ctp' file ------
//some other code
foreach ($resultArray as $result){
echo $result['subject'];
echo $result['body'];
}
我正在使用 cakephp 2.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
有近百个元素,我想在此页面上设置分页。那么在这种情况下是否可以使用 cakephp 默认分页?谢谢:)
您需要实现 cakephp 自定义分页。
CakePHP uses two method to manage pagination queries, that are paginate and paginateCount, they are used to get page data and total record count respectively. To have pagination work with your custom queries, we will need to implement both above functions in our model file where you want to have pagination to work with custom queries. You can implement in your behavior file as well. Let’s see how we can have this implemented.
//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'));
}
Note : MyPages should be MyPage, because Cakephp model name should be singular.
您的视图文件如下所示:
----- 'view.ctp' file ------
//some other code
foreach ($resultArray as $result){
echo $result['subject'];
echo $result['body'];
}