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

Problem with calling PHP function in .ss file


Go to End


2 Posts   993 Views

Avatar
Bobbybill123

Community Member, 1 Post

7 January 2016 at 3:21am

I have made a function inside the themes PHP file that i am using

public function numberAdd($start,$end) {
		for($x=$start;$x<=$end;$x++){
			echo '<option value=',$x,'>',$x,'</option>';
		}
	}

And I call it in the .ss file, trying to add it to a drop down menu with:
<select name="day">
<option value="">Day</option>
$numberAdd(1,31);
</select>

However when I open the page, all of the numbers that should be appearing as part of the list, are just appearing as plain text above all of the other stuff on the site, what am I doing wrong here?
Thanks

Avatar
beeonline

Community Member, 7 Posts

7 January 2016 at 2:06pm

You need to return a value with your function, rather than echoing the values, i.e. something like this:

        public function numberAdd($start, $end) {
                $optionsHTML = '';
                for ($x = $start; $x <= $end; $x++) {
                        $optionsHTML .= '<option value=' . $x . '>' . $x . '</option>';
                }
                return $optionsHTML;
        }