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'); ?>
RSSの取得
D:/test/cakephp/app/controllers/pages_controller.php を作成
<?php class PagesController extends AppController { public $uses = null; public function rss() { App::import('Core', 'Xml'); $xml = new XML("http://d.hatena.ne.jp/bobchin/rss"); $xml = Set::reverse($xml); $items = $xml['RDF']['Item']; $this->set(compact('items')); } }
D:/test/cakephp/app/views/pages/rss.ctp を作成
<?php if(!empty($items)): ?> <table> <?php echo $html->tableHeaders(array_keys($items[0]));?> <?php foreach($items as $item): ?> <?php echo $html->tableCells($item); ?> <?php endforeach; ?> </table> <?php endif; ?>
配列のpaginateはどうすべか。
小細工で対応。
D:/test/cakephp/app/models/rss.php を作成
<?php class Rss extends AppModel { public $name = 'Rss'; public $useTable = false; private $paginateData = array(); public function setPaginateData($array) { $this->paginateData = $array; return $this; } public function find($conditions = null, $fields = array(), $order = null, $recursive = null) { if (low($conditions) == 'count') { return count($this->paginateData); } else { $page = ($fields['page'] < 1)? 1: $fields['page']; $limit = ($fields['limit'] < 1)? 1: $fields['limit']; $index = ($page-1)*$limit; return array_slice($this->paginateData, $index, $limit); } } }
D:/test/cakephp/app/controllers/pages_controller.php を作成
<?php class PagesController extends AppController { public $uses = array('Rss'); public $helpers = array('Paginator'); public $paginate = array( 'Rss' => array( 'limit' => 5, ), ); public function rss() { App::import('Core', 'Xml'); $xml = new XML("http://d.hatena.ne.jp/bobchin/rss"); $xml = Set::reverse($xml); $items = empty($xml['RDF']['Item'])? array(): $xml['RDF']['Item']; $this->Rss->setPaginateData($items); $items = $this->paginate('Rss'); $this->set(compact('items')); } }
D:/test/cakephp/app/views/pages/rss.ctp を作成
<?php echo $this->renderElement('paginate'); ?> <table> <?php echo $html->tableHeaders(array_keys($items[0]));?> <?php foreach($items as $item): ?> <?php echo $html->tableCells($item); ?> <?php endforeach; ?> </table> <?php echo $this->renderElement('paginate'); ?>
1.hello world
D:/test/cakephp/app/.htaccess と D:/test/cakephp/app/webroot/.htaccess に以下を追記
<IfModule mod_rewrite.c> RewriteBase /test/cakephp RewriteEngine On
D:/test/cakephp/app/controllers/pages_controller.php を作成
<?php class PagesController extends AppController { public $uses = null; public function display() { } }
D:/test/cakephp/app/views/pages/display.ctp を作成
Hello, World!
2. calculator
D:/test/cakephp/app/controllers/pages_controller.php を作成
<?php class PagesController extends AppController { public $uses = null; public function display() { $result = ''; if (!empty($this->data['Culc'])) { /* eval じゃない方がいいな $list = array('add' => '+', 'subtract' => '-', 'multiply' => '*', 'divide' => '/'); foreach ($list as $k => $m) { if (isset($this->data['Culc'][$k])) { $arg1 = intval($this->data['Culc']['arg1']); $arg2 = intval($this->data['Culc']['arg2']); eval(sprintf('$result = %s %s %s;', $arg1, $m, $arg2)); break; } } */ $arg1 = intval($this->data['Culc']['arg1']); $arg2 = intval($this->data['Culc']['arg2']); switch (low($this->data['Culc'])) { case 'add' : $result = arg1 + arg2; break; case 'subtract': $result = arg1 - arg2; break; case 'multiply': $result = arg1 * arg2; break; case 'divide' : $result = arg1 / arg2; break; } } $this->set(compact('result')); } }
D:/test/cakephp/app/views/pages/display.ctp を作成
<?php echo $form->create('Page', array('url'=>'/pages/display')); ?> ARG1: <?php echo $form->text('Culc.arg1'); ?> ARG2: <?php echo $form->text('Culc.arg2'); ?> <?php echo $form->submit('add', array('name'=>'data[Culc][add]', 'div'=>false)); ?> <?php echo $form->submit('subtract', array('name'=>'data[Culc][subtract]', 'div'=>false)); ?> <?php echo $form->submit('multiply', array('name'=>'data[Culc][multiply]', 'div'=>false)); ?> <?php echo $form->submit('divide', array('name'=>'data[Culc][divide]', 'div'=>false)); ?> <br/> <br/> <br/> <br/> <?php if (isset($result)): ?> Result: <?php echo h($result); ?> <?php endif; ?> <?php echo $form->end(); ?>
3. guestbook
バリデータとかいれてないけど。
バリデーションを追加。
DB作成
CREATE DATABASE `guestbook` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci; CREATE TABLE IF NOT EXISTS `guests` ( `id` int(11) NOT NULL auto_increment, `name` text collate utf8_unicode_ci, `mail` text collate utf8_unicode_ci, `contnet` text collate utf8_unicode_ci, `url` text collate utf8_unicode_ci, `created` timestamp NULL default NULL, `updated` timestamp NULL default NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
D:/test/cakephp/app/config/database.php を編集
var $default = array( 'driver' => 'mysql', 'persistent' => false, 'host' => 'localhost', 'login' => 'username', 'password' => 'password', 'database' => 'guestbook', 'prefix' => '', );
D:/test/cakephp/app/models/guest.php を作成
<?php class Guest extends AppModel { public $name = 'Guest'; // 追記 public $validate = array( 'name' => array( 'rule' => array('notEmpty'), ), 'mail' => array( 'notEmpty' => array( 'rule' => array('notEmpty'), ), 'email' => array( 'rule' => array('email'), ), ), 'content' => array( 'rule' => array('notEmpty'), ), 'url' => array( 'notEmpty' => array( 'rule' => array('notEmpty'), ), 'url' => array( 'rule' => array('url'), ), ), ); }
D:/test/cakephp/app/controllers/pages_controller.php を作成
<?php class PagesController extends AppController { public $uses = array('Guest'); public function guestbook() { if (!empty($this->data['Guest'])) { if ($this->Guest->save($this->data)) { $this->redirect('/pages/guestbook'); return; } } $guests = $this->Guest->find('all', array( 'order' => 'created DESC', )); $this->set(compact('guests')); } }
D:/test/cakephp/app/views/pages/guestbook.ctp を作成
<?php echo $form->create('Page', array('url'=>'/pages/guestbook')); ?> <!-- 変更 <table> <tr> <td>お名前:<?php echo $form->text('Guest.name'); ?></td> </tr> <tr> <td>メール:<?php echo $form->text('Guest.mail'); ?></td> </tr> <tr> <td>本文:<?php echo $form->textarea('Guest.content'); ?></td> </tr> <tr> <td>URL:<?php echo $form->text('Guest.url'); ?></td> </tr> <tr> <td><?php echo $form->submit('登録'); ?></td> </tr> </table> --> <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 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; ?>