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.

General Questions

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

[FIXED] "Previous or Next Buttons" example not fully working


Reply

5 Posts   1658 Views

Avatar
Johan

2 September 2010 at 7:57pm (Last edited: 3 September 2010 8:51pm), Community Member, 49 Posts

Using the exact same code as on: http://doc.silverstripe.org/recipes:previousornext

The example on here worked beautifully for cycling through my News pages in the news section.

However when I use the same page template (ArticlePage.cc/php) again for the Publication and Events section the previous or next buttons fail to work. This is my site structure...

Seems odd when i have used exactly the same code as the example and same page template (ArticlePage.cc/php)

Avatar
TotalNet

3 September 2010 at 9:09am Community Member, 181 Posts

Hi Johan,

What do you mean by "fail to work"? Do the links show at all and if so where do they link to?

Have you put the code in your Page_Controller or ArticlePage_Controller?

This is a standard addition to my Page.php and using it without issue so I'm sure we can figure this out.

Avatar
SSadmin

3 September 2010 at 11:47am Community Member, 90 Posts

i have got this function works quite well for me?!
not sure why it fails on yours.
Maybe provide some code of your (ArticlePage.cc/php) and template .ss could be helpful.

Avatar
Johan

3 September 2010 at 7:23pm (Last edited: 3 September 2010 7:52pm), Community Member, 49 Posts

1. Only in the news pages do they work (using ArticlePage).

2. Publications and Events the paging does not show at all (again using ArticlePage).

Code ArticlePage.php:

<?php
/**
* Defines the ArticlePage page type
*/
class ArticlePage extends Page {
static $db = array(
      'Date' => 'Date',
      'Author' => 'Text',   
);
static $has_one = array(
);
static $icon = "themes/tutorial/images/treeicons/news";

   function getCMSFields() {
      $fields = parent::getCMSFields();
      
      $fields->addFieldToTab('Root.Content.Main', new TextField('Author'), 'Content');
      // $fields->addFieldToTab('Root.Content.Main', new DateField('Date'), 'Content');
      
      $datefield = new DateField('Date');
      $datefield->setConfig('showcalendar', true);
      $datefield->setConfig('showdropdown', true);
      $datefield->setConfig('dateformat', 'dd/MM/YYYY');
      
      $fields->addFieldToTab('Root.Content.Main', $datefield, 'Content');

      return $fields;
   }

}

class ArticlePage_Controller extends Page_Controller {

   function LatestNews($num=3) {
      $news = DataObject::get_one("NewsHolder");
      return ($news) ? DataObject::get("ArticlePage", "ParentID = $news->ID", "Date DESC", "", $num) : false;
   }

// ************ Previous or Next Buttons *************
   
public function nextPager() {
$where = "ParentID = ($this->ParentID + 0) AND Sort > ($this->Sort + 0 )";
$pages = DataObject::get("SiteTree", $where, "Sort", "", 1);
if($pages) {
foreach($pages as $page) {
return $page;
}
}
}
public function previousPager() {
$where = "ParentID = ($this->ParentID + 0) AND Sort < ($this->Sort + 0)";
$pages = DataObject::get("SiteTree", $where, "Sort DESC", "", 1);
if($pages) {
foreach($pages as $page) {
return $page;
}
}
}

}

?>

ArticlePage.ss

<div class="paging">
    <% if previousPager %>
    <p class="previous"><a href="$previousPager.URLSegment">Previous</a></p>
    <% end_if %>
    <% if nextPager %>
    <p class="next"><a href="$nextPager.URLSegment">Next</a></p>
    <% end_if %>
</div>

Many thanks.

Avatar
Johan

3 September 2010 at 8:50pm Community Member, 49 Posts

A programmer on IRC has found the issue.

The reason it fails is because all the articles have the same date.

Changed the dates now work.