ページ分け機能はコントローラーを以下のようにします。

samples_controller.php

<?php
define('LIMIT',2);

class SamplesController extends AppController
{
    var $scaffold;

    //index
    function index($page=1)
    {
        $count = $this->Sample->findCount();
        $this->set('count', $count);

        $before = "";
        if($page >= 2){
            $before = $page - 1;
        }
        $this->set('before', $before);

        $after = "";
        if($page*LIMIT < $count){
            $after = $page + 1;
        }
        $this->set('after', $after);

        $this->set('samples', $this->Sample->findAll(null,null,null,LIMIT,$page));
    }
}
?>

続いて、ビューを以下のようにします。

index.thtml

全<?php echo $count ?>件
<?php
    if ($before <> "" ) {
        echo $html->link("[前ページ]","/samples/index/{$before}");
    }
    if ($after <> "" ) {
        echo $html->link("[次ページ]","/samples/index/{$after}");
    }
?>

Paginationのやり方もできます。


,
この記事をはてなブックマークする