CakePhp分页功能

来源:互联网 发布:布列斯特和约 知乎 编辑:程序博客网 时间:2024/05/22 06:59

CakePhp分页功能

1.Setting Up

download http://bakery.cakephp.org/articles/view/67
copy the code save as
 /app/controllers/components/pagination.php

download
http://bakery.cakephp.org/articles/view/68
copy the code save as
 /app/views/helpers/pagination.php

download
 http://bakery.cakephp.org/articles/view/69
copy the code save as
 /app/views/elements/pagination.thtml

If you can't download up, please give to your mailbox,I can give you.

2.Create/modify the Controller

<?php 
class PostsController extends AppController
{
    var $name = 'Posts'; // for PHP4 installs
    var $components = array ('Pagination'); // Added
    var $helpers = array('Pagination'); // Added

    function index() {    
        $criteria=NULL;
        list($order,$limit,$page) = $this->Pagination->init($criteria); // Added
        $data = $this->Post->findAll($criteria, NULL, $order, $limit, $page); // Extra parameters added
        
        $this->set('data',$data);
    }
}
?>

3.Create/modify the View
<h1>Paginated Posts Index</h1>
<table>
<?php
$pagination->setPaging($paging); // Initialize the pagination variables
$th = array (
            $pagination->sortBy('id'),
            $pagination->sortBy('title'),
            $pagination->sortBy('created')
); // Generate the pagination sort links
echo $html->tableHeaders($th); // Create the table headers with sort links if desired

foreach ($data as $output)
{
    $tr = array (
        $output['Post']['id'],
        $html->link($output['Post']['title'], "/Posts/View/{$output['Post']['id']}"),
        $output['Post']['created']
        );
    echo $html->tableCells($tr,array('class'=>'altRow'),array('class'=>'evenRow'));
}
?>
</table>
<? echo $this->renderElement('pagination'); // Render the pagination element ?>