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

[HELP] Need to add a count records??


2 Posts   1008 Views


11 August 2009 at 5:04pm Community Member, 38 Posts

How do i put an if statment on every third record??

eg every third record i need to print some extra code like <br /> for example

Do I add a counter to the php(below)
function LatestExhibitions(){
return DataObject::get("ExhibitionPage", "ParentID = $this->ID", "`ExhibitionPage`.ID DESC", "", 9);

or to the control

<% control LatestExhibitions %>

<% end_control %>


11 August 2009 at 6:17pm Community Member, 712 Posts

A DataObject within a DataObjectSet has access to it's current iterator position through Pos().

Therefore, in ExhibitionPage add the following method:

function MultipleOf($mod, $offset = 1) {
   return ((($this->Pos() + $offset) % $mod) == 0);

Then, in your template, you can do (for example):

<% control LatestExhibitions %>
   <% if MultipleOf(3) %>
      <br />
   <% end_if %>
<% end_control %>