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'); ?>