4. parse and paginate

とりあえずpaginate。

D:/test/cakephp/app/controllers/pages_controller.php を作成

<?php

class PagesController extends AppController {

    public $uses = array('Guest');
    public $helpers = array('Paginator');
    public $paginate = array(
        'Guest' => array(
            'order' => 'created DESC',
            'limit' => 5,
        ),
    );

    public function guestbook()
    {
        if (!empty($this->data['Guest'])) {
            if ($this->Guest->save($this->data)) {
                $this->redirect('/pages/guestbook');
                return;
            }
        }
        
        $guests = $this->paginate('Guest');
        
        $this->set(compact('guests'));
    }
}

D:/test/cakephp/app/views/elements/paginate.ctp を作成

<?php echo $paginator->counter(array('format'=>'<%count% 件中 %start% から %end% までを表示>')); ?>
<br/>
ページ:<?php echo $paginator->numbers(); ?>

D:/test/cakephp/app/views/pages/guestbook.ctp を作成

<?php echo $form->create('Page', array('url'=>'/pages/guestbook')); ?>
<table>
  <tr>
    <td>お名前:
      <?php echo $form->error('Guest.name', 'お名前を入力してください。'); ?>
      <?php echo $form->text('Guest.name'); ?>
    </td>
  </tr>
  <tr>
    <td>メール:
      <?php echo $form->error('Guest.mail', array(
                                'notEmpty' => 'メールを入力してください。',
                                'email' => 'メールアドレスを正しく入力してください。',
                             )); ?>
      <?php echo $form->text('Guest.mail'); ?>
    </td>
  </tr>
  <tr>
    <td>本文:
      <?php echo $form->error('Guest.content', '本文を入力してください。'); ?>
      <?php echo $form->textarea('Guest.content'); ?>
    </td>
  </tr>
  <tr>
    <td>URL:
      <?php echo $form->error('Guest.url', array(
                                'notEmpty' => 'URLを入力してください。',
                                'url' => 'URLを正しく入力してください。',
                             )); ?>
      <?php echo $form->text('Guest.url'); ?>
    </td>
  </tr>
  <tr>
    <td><?php echo $form->submit('登録'); ?></td>
  </tr>
</table>
<?php echo $form->end(); ?>

<?php echo $this->renderElement('paginate'); ?>

<?php foreach($guests as $guest): ?>
<table>
  <tr>
    <td>
      <?php echo h($guest['Guest']['content']); ?>
      <br/>
      From: <?php echo h($guest['Guest']['name']); ?>

<?php if(!empty($guest['Guest']['url'])): ?>
      <a href="<?php echo h($guest['Guest']['url']); ?>"><?php echo $html->image('house.gif'); ?></a>
<?php endif; ?>

<?php if(!empty($guest['Guest']['mail'])): ?>
      <a href="mailto:<?php echo h($guest['Guest']['mail']); ?>"><?php echo $html->image('mail.gif'); ?></a>
<?php endif; ?>
      <br/>

	  <?php echo h(date('Y/m/d H:n', strtotime($guest['Guest']['created']))); ?>
    </td>
  </tr>
</table>
<?php endforeach; ?>

<?php echo $this->renderElement('paginate'); ?>