Jump to:

23487 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Show "x" number of comments?

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

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

Page: 1
Go to End
Author Topic: 1179 Views
  • Pix
    Avatar
    Community Member
    135 Posts

    Show "x" number of comments? Link to this post

    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!

  • IOTI
    Avatar
    Community Member
    189 Posts

    Re: Show "x" number of comments? Link to this post

    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

  • Pix
    Avatar
    Community Member
    135 Posts

    Re: Show "x" number of comments? Link to this post

    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.

    1179 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.