最新版 |
編集中の文章 |
行10: |
行10: |
| $BlogPost = ClassRegistry::init('Blog.BlogPost'); | | $BlogPost = ClassRegistry::init('Blog.BlogPost'); |
| $posts = $BlogPost->find('all', array( | | $posts = $BlogPost->find('all', array( |
− | 'conditions' => array_merge($BlogPost->getConditionAllowPublish()), | + | 'conditions' => array_merge($BlogPost->getConditionAllowPublish()) ), |
| 'order' => array('BlogPost.posts_date DESC'), //公開日順にソート | | 'order' => array('BlogPost.posts_date DESC'), //公開日順にソート |
| 'limit' => 1000, //取得記事数 | | 'limit' => 1000, //取得記事数 |
行151: |
行151: |
| | | |
| </syntaxhighlight> | | </syntaxhighlight> |
− | | + | [[Category:サンプル]] |
− | | + | |
− | ## 応用:複数ブログをページングするサンプル(トップページ例)
| + | |
− | | + | |
− | <syntaxhighlight lang="php">
| + | |
− | <?php
| + | |
− | // 複数のブログ記事から一覧を取得し、ページングさせるサンプル
| + | |
− | $BlogPost = ClassRegistry::init('Blog.BlogPost');
| + | |
− | | + | |
− | // ページング対応
| + | |
− | $allCount = 0;
| + | |
− | $viewCount = 10; // 表示件数
| + | |
− | $nowPage = 1;
| + | |
− | $firstPage = null;
| + | |
− | $lastPage = null;
| + | |
− | $prevPage = null;
| + | |
− | $nextPage = null;
| + | |
− | $conditions = array_merge($BlogPost->getConditionAllowPublish(), array(
| + | |
− | 'BlogPost.blog_content_id' => array(1,2) // 表示させたいブログコンテンツIDを指定
| + | |
− | ));
| + | |
− | $allCount = $BlogPost->find('count', array(
| + | |
− | 'conditions' => $conditions,
| + | |
− | 'cache' => false
| + | |
− | ));
| + | |
− | if (!empty($this->request->params['named']['page'])) {
| + | |
− | $nowPage = $this->request->params['named']['page'];
| + | |
− | }
| + | |
− | | + | |
− | if ($allCount > $viewCount) {
| + | |
− | $firstPage = 1;
| + | |
− | $lastPage = ($allCount % $viewCount == 0) ? intval($allCount / $viewCount) : intval($allCount / $viewCount) + 1;
| + | |
− | if ($nowPage > $firstPage) {
| + | |
− | $prevPage = $nowPage -1;
| + | |
− | } else {
| + | |
− | $firstPage = null;
| + | |
− | }
| + | |
− | if ($nowPage < $lastPage) {
| + | |
− | $nextPage = $nowPage +1;
| + | |
− | } else {
| + | |
− | $lastPage = null;
| + | |
− | }
| + | |
− | }
| + | |
− | | + | |
− | $posts = $BlogPost->find('all', array(
| + | |
− | 'conditions' => $conditions,
| + | |
− | 'order' => array('BlogPost.posts_date DESC'),
| + | |
− | 'limit' => $viewCount,
| + | |
− | 'offset' => ($nowPage - 1) * $viewCount,
| + | |
− | 'cache' => false
| + | |
− | ));
| + | |
− | ?>
| + | |
− | | + | |
− | <!-- 一覧を表示 -->
| + | |
− | | + | |
− | <?php /* トップページの場合の前へ、次へを表示するサンプル */ ?>
| + | |
− | <?php if ($prevPage) : ?>
| + | |
− | <?php $this->BcBaser->link("<前", "/index/page:".$prevPage); ?>
| + | |
− | <?php endif; ?>
| + | |
− | <?php if ($nextPage): ?>
| + | |
− | <?php $this->BcBaser->link("次>", "/index/page:".$nextPage); ?>
| + | |
− | <?php endif; ?>
| + | |
− | | + | |
− | </syntaxhighlight>
| + | |