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.

All other Modules /

Discuss all other Modules here.

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

for loop


Go to End


2 Posts   2831 Views

Avatar
digitalegg

Community Member, 7 Posts

30 May 2009 at 1:21am

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

Avatar
UncleCheese

Forum Moderator, 4102 Posts

30 May 2009 at 1:42am

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