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

Running a control loop a specified number of times


Reply


5 Posts   1091 Views

Avatar
RichMcNabb

Community Member, 34 Posts

28 September 2011 at 9:51am

Hi there,

On my Page.ss template I am trying to access information on my contact page and have the loop run twice (2). The loop works in that it displays they information however it shows more than 2 results.

Any help would be great.

Snippet from Page.ss
================
<% control Page(Contact) %>
<% control Offices(2) %>
<ul>
<li>$City</li>
<li>$EmailAddress</li>
<li>$Phone</li>
</ul>
<% end_control %>
<% end_control %>

ContactPage.php
================
<?php
/**
* Defines the ContactPage page type
*/

class ContactPage extends Page {

static $db = array(
);

static $has_one = array(
);

static $has_many = array(
"Offices" => "Office"
);

public function getCMSFields() {
$fields = parent::getCMSFields();

$Offices = new DataObjectManager(
$this,
'Offices',
'Office',
Office::$fields,
'getCMSFields_forPopup',
"`PageID` = $this->ID"
);

$fields->addFieldToTab('Root.Content.Offices', $Offices);

return $fields;
}

}

class ContactPage_Controller extends Page_Controller {

}
?>

Office.php
================
<?php

class Office extends DataObject {

static $db = array(
'Level' => 'Text',
      'StreetAddress' => 'Text',
      'POBox' => 'Text',
      'City' => 'Text',
      'Country' => 'Text',
'Phone' => 'Text',
      'EmailAddress' => 'Text',
'GoogleMap' => 'HTMLText',
);
static $has_one = array(
'Page' => 'ContactPage',
      'OfficeMapImage' => 'CustomImage',
);
static $fields = array(
'Level' => 'Level',
      'StreetAddress' => 'StreetAddress',
      'POBox' => 'POBox',
      'City' => 'City',
      'Country' => 'Country',
'Phone' => 'Phone',
      'EmailAddress' => 'EmailAddress',
      'GoogleMap' => 'GoogleMap',
);

public function getCMSFields_forPopup() {
$fields = new FieldSet();

$fields->push(new TextField('Level', 'Building floor level (optional)'));
      $fields->push(new TextField('StreetAddress','Street address'));
      $fields->push(new TextField('POBox', 'PO Box or private bag number'));
      $fields->push(new TextField('City'));
      $fields->push(new TextField('Country'));
$fields->push(new TextField('Phone'));
      $fields->push(new TextField('EmailAddress','Email address'));
$fields->push(new TextField('GoogleMap', 'Paste the Google map code below - use www.bit.ly so the code is short and validates'));
      $fields->push(new ImageField('OfficeMapImage', 'Location Map Image'));
      

return $fields;
}

}
?>

Avatar
SheaDawson

Community Member, 46 Posts

28 September 2011 at 3:51pm

I think I see what you are trying to do... limit the number of Office records displayed to 2? I'm not sure you can pass variables into Offices in the template, ie. Offices(2).

Maybe create a function on contact page the takes a $limit param

function MyOffices($limit = 2){
return $this->Offices('',"0,$limit");
}

the first argument for $this->Offices is the filter (which we don't need in this case)... the second is the limit (start at the first record, stop at the value of $limit, which defaults to 2)

and then use <% control MyOffices(2) %>

Avatar
RichMcNabb

Community Member, 34 Posts

29 September 2011 at 2:28pm

Hi Mr Squatch,

You are right in your guess - I would like limit the number of offices contained in the footer to 2. However on the contact page there can be as many offices as they want to add.

Effectively information should be being pulled into page.ss like this:

Page.php ---> ContactPage.php --->Office.php

I tried your code but got errors unfortunately - any ideas would be awesome. Thanks again :)

Avatar
SheaDawson

Community Member, 46 Posts

29 September 2011 at 5:55pm

Right... I would probably just to this

in mysite/Page.php

class Page extends SiteTree{

....

function MyOffices($limit = 2){
if($offices = DataObject::get('Office', $filter= '', $sort='', $join='', $limit="0,$limit"){
return $offices;
}

}
....

Then MyOffices will be available on any page template directly.

Example template control

<% control MyOffices(2) %>
<ul>
<li>$City</li>
<li>$EmailAddress</li>
<li>$Phone</li>
</ul>
<% end_control %>

I haven't tested that of course but it should work, let me know if you get any errors and what they are.

Avatar
B-Side

Community Member, 42 Posts

9 October 2011 at 7:46pm

Hi there

I have the same sort of an issue, however I am forced to use MS SQL for the database and this does not allow the LIMIT function.

Is there an alternative workaround for limiting results in either the template or the DataObjectSet?

Many thanks,

Jayne