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

return a foreach loop


Go to End


2 Posts   1009 Views

Avatar
SilverPeed

Community Member, 22 Posts

1 November 2014 at 8:50am

I'm working on a frontend form where i add candidates and add multiple attachments.
The code below works fine with Echo but i want to use return instead. Ofcourse with return the function stops and i end up with only one file returned.
How can i change the code so it will return an array of data or something.

public function showAttatchments(){
$UserId = $this->request->param('ID');
$objects = Bijlage::get()->filter(array(
'connectiemetKandidaatID' => $UserId
));
foreach($objects as $Bijlage ){
$Bestand = File::get()->byID($Bijlage->DocumentID);
echo $Bestand->Filename . $Bijlage->Title;
}
}

Avatar
martimiz

Forum Moderator, 1391 Posts

1 November 2014 at 11:13pm

You would do that by feeding the filtered list of Bijlages to your template, and let it do the looping. Someting like:

class Bijlage

private static $has_one  = array(
'YourPage' => 'SiteTree'
'Bestand ' => 'File'
);

Class YourPage

private static $has_many  = array(
'Bijlages' => 'Bijlage'
);

public function Attachements() {
return $this->Bijlages()->filter(array('connectiemetKandidaatID' => $UserId));
}

YourPage.ss

<% loop $Attachments %>
$Bestand.Filename $Title
<% end_loop %>