Jump to:

10848 Posts in 2516 Topics by 1792 members

All other Modules

SilverStripe Forums » All other Modules » for loop

Discuss all other Modules here.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 1640 Views
  • digitalegg
    Avatar
    Community Member
    7 Posts

    for loop Link to this post

    Hi,

    I know this is probably really easy but I just can't find anything in the docs or here on the forums...

    I am just trying to do a simple for-loop inside a template, the number is coming from a variable in the database. This is what I want (done in php)

    <?php for( $x=0;$x < $num;$x++) { ?>
    Delegate <?php echo $x; ?> <input type="text" value="<?php echo $x; ?><br />
    <?php } ?>

    I have tried putting a function in my controller like this:

    function delegates(num) {
    for($x=0;$x < $num;$x++) {
    $data .="Delegate $x";
    }
    return $data;
    }

    then calling it like this in the template:

    <% control delegates( $num ) %><%end_control %>

    but this isn't working

    is there a way to do this?

    Cheers

    Mike

  • UncleCheese
    Avatar
    4085 Posts

    Re: for loop Link to this post

    You said the number is coming from the database, but I don't see any queries.

    One thing you could do..

    function Delegates()
    {
    $data = new ArrayData();
    for(...) {
    $data->push(new ArrayData(array('Num' => $x)));
    }
    return $data;
    }

    <% control Delegates %>$Num<% end_control %>

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