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

[HELP] Need to add a count records??


Go to End


2 Posts   1470 Views

Avatar
b0bro

Community Member, 38 Posts

11 August 2009 at 5:04pm

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 %>

Avatar
Hamish

Community Member, 712 Posts

11 August 2009 at 6:17pm

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 %>
	<h3>Title.XML</h3>
	<% if MultipleOf(3) %>
		<br />
	<% end_if %>
<% end_control %>