Jump to:

3375 Posts in 999 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » pagination problem

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1054 Views
  • roterl
    Avatar
    Community Member
    44 Posts

    pagination problem Link to this post

    hi
    I'm tring to make pagination that will be set from the template instead of the code. so the theme designer will be able to select how much items to show per page.
    so for example in the ArticlesHolder it will be possible to do somehting like this:

    <% control NewsArticles(10) %>

    for 10 items per page.
    This is the code:

    ArticleHolder.php:

    <?php

    /**
    * Defines the ArticleHolder page type
    */
    class ArticleHolder extends Page {
       static $db = array(
       );
       static $has_one = array(
       );
       
       static $allowed_children = array('ArticlePage');
       static $icon = "themes/tutorial/images/treeicons/news";

    }

    class ArticleHolder_Controller extends Page_Controller {

       function init() {
          RSSFeed::linkToFeed($this->Link() . "rss");
          parent::init();
       }

       function rss() {
          $rss = new RSSFeed($this->Children(), $this->Link(), "The coolest news around");
          $rss->outputToBrowser();
       }

       /**
        * Get the latest 2 news articles, allowing us to get more
        *
        * @return DataObjectSet|false The DataObjectSet of the news articles you're looking at or boolean false if there aren't any news articles
        */
       function NewsArticles($itemsPerPage = 10) {
          if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1) {
             $_GET['start'] = 0;
          }

          $start = (int)$_GET['start'];
          $doSet = DataObject::get(
          $callerClass = "ArticlePage",
          $filter = "`ParentID` = '".$this->ID."'",
          $sort = "Date DESC",
          $join = "",
          $limit = "{$start},{$itemsPerPage}"
          );
          
          return $doSet ? $doSet : false;
       }

    }

    ?>

    ArticleHolder.ss:

    .....
                   <ul id="NewsList" >
                      <% control NewsArticles(5) %>
                         <div id="item" class="$EvenOdd">
                         <div class="newsTitle"><a href="$Link" title="<% _t('READ_MORE_ABOUT',"Read more about") %> &quot;{$Title}&quot;">$Title</a></div>
                         <div class="newsDate"><% _t('PUBLISH_DATE',"Publish at:") %> $Date.Nice <% _t('PUBLISHER',"by") %> $Author</div>
                         <!--
                         <li class="newsAuthor">$Author</li>
                         <li class="newsSummary">$Content.FirstParagraph</li>
                         <li class="newsReadMore"><a href="$Link" title="<% _t('READ_MORE_ON',"Read more on") %> &quot;{$Title}&quot;"><% _t('READ_MORE',"Read more") %> &gt;&gt;</a></li>
                          -->
                         </div>
                      <% end_control %>
                
                      <% if NewsArticles.MoreThanOnePage %>
                         <p>
                         <% if NewsArticles.PrevLink %>
                            <a href="$NewsArticles.PrevLink">&lt;&lt; <% _t('PREV',"Prev") %></a> |
                         <% end_if %>
                
                         <% control NewsArticles.Pages %>
                            <% if CurrentBool %>
                               <strong>$PageNum</strong>
                            <% else %>
                               <a href="$Link" title="><% _t('GO_TO_PAGE',"Go to page") %> $PageNum">$PageNum</a>
                            <% end_if %>
                         <% end_control %>
                
                         <% if NewsArticles.NextLink %>
                            | <a href="$NewsArticles.NextLink"><% _t('NEXT',"Next") %> &gt;&gt;</a>
                         <% end_if %>
                         </p>
                      <% end_if %>
                   </ul>
    .....

    The problem is the the NewsArticles function called twice. once at <% control NewsArticles(5) %> (which get the right results) and another time at <% if NewsArticles.MoreThanOnePage %> which return the wrong results.
    This mess up the pagination.

    any ideas?

    thanks,
    Rotem.

    1054 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.