Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Show "x" number of comments?


Go to End


3 Posts   1886 Views

Avatar
Pix

Community Member, 158 Posts

9 November 2010 at 3:22pm

Edited: 09/11/2010 3:22pm

Hi All,

I'm really enjoying the flexibility and work flow of SilverStripe, awesome! I know I will have to code this somehow, but I was wondering if someone has implemented a way to show "x" number of comments with forward and back buttons, and maybe if they could share how they did it? Actually, I would like to be able to do something like this for a lot of things, such as news articles perhaps, so things don't scroll for miles on the page. I would appreciate any pointers.

Thanks!

Avatar
copernican

Community Member, 189 Posts

9 November 2010 at 6:42pm

Edited: 09/11/2010 6:43pm


I believe comments is setup to show 10 comments before beginning pagination, so you could edit the core file PageComment.php, the static variable $comments_per_page.

For creating pagination for other dataobjects you can do the following.

ReviewArticle.php

<?php

/*
* Custom Class for Review home page
*
*/

class ReviewHolder extends Page{
   
   public static $db = array();
   
   public static $has_one = array();
   
   public static $has_many = array(
   
         'Reviews' => 'Review'
   );
   
   public static $default_child = "Review";
   
   
   /**
   * Returns dataobject set containing reviews
   * @RETURN DataObjectSet
   **/
   function getAllReviews() {
         if(!isset($_GET['start']) || !is_numeric($_GET['start']) || (int)$_GET['start'] < 1){
         
         $SQL_start = 0;
         $get = "Review";
         $where = NULL;
         $sort = NULL;
         $join = NULL;
         $limit = "{$SQL_start}, 2";
         $reviews = DataObject::get($get, $where, $sort, $join, $limit);
         return $reviews;
         
      }else{
         $SQL_start = (int)$_GET['start'];;
         $get = "Review";
         $where = NULL;
         $sort = NULL;
         $join = NULL;
         $limit = "{$SQL_start}, 2";
         $reviews = DataObject::get($get, $where, $sort, $join, $limit);
         return $reviews;
      }
   }
   
   
}

class ReviewHolder_Controller extends Page_Controller{
   
}

?>

in the $limit field change the the number 2 to your desired number of objects to show.

and on your template you can do this
ReviewHolder.php

<% if getAllReviews %>
   <% control getAllReviews %>
<div class="review">
   <a href="reviews/$URLSegment">$GameCover.setWidth(160)</a>
   <h1><a href="reviews/$URLSegment">$Title.XML</a></h1>
   $Content.LimitCharacters(250)...<a href="reviews/$URLSegment">Read More</a>
</div>
<% end_control %>
<% if getAllReviews.MoreThanOnePage %>
   <p id="pagination">
<% if getAllReviews.PrevLink %>
   <a class="previous" href="$getAllReviews.PrevLink">Prev</a> |
<% end_if %>
<% control getAllReviews.Pages %>
      <% if CurrentBool %>
      $PageNum
      <% else %>
      <a class="pageNum" href="$Link" title="Go to page $PageNum">$PageNum</a>
<% end_if %>
<% end_control %>
      <% if getAllReviews.NextLink %>
   |<a class="next" href="$getAllReviews.NextLink">Next</a>
<% end_if %>
</p>
<% end_if %>
<% end_if %>

Hope that helps

Avatar
Pix

Community Member, 158 Posts

9 November 2010 at 7:25pm

Edited: 09/11/2010 7:41pm

Oh wow thanks IOTI! That is great. This is a awesome community thanks for the help and quick reply!

:0)

p.s. you know I didn't even realize that automatic pagination was built in, that is so cool. Thanks for pointing that out to me, I didn't have that many comments on my test page. I would really like to try the pagination with other data objects so that bit of code will come in really handy.