Jump to:

23493 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Running a control loop a specified number of times

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: 1036 Views
  • RichMcNabb
    Avatar
    Community Member
    34 Posts

    Running a control loop a specified number of times Link to this post

    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;
    }

    }
    ?>

  • SheaDawson
    Avatar
    Community Member
    45 Posts

    Re: Running a control loop a specified number of times Link to this post

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

  • RichMcNabb
    Avatar
    Community Member
    34 Posts

    Re: Running a control loop a specified number of times Link to this post

    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

  • SheaDawson
    Avatar
    Community Member
    45 Posts

    Re: Running a control loop a specified number of times Link to this post

    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.

  • B-Side
    Avatar
    Community Member
    42 Posts

    Re: Running a control loop a specified number of times Link to this post

    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

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