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

Using VAR in function in SS File?


Go to End


6 Posts   1759 Views

Avatar
SalvaStripe

Community Member, 89 Posts

6 April 2009 at 11:00pm

Edited: 07/04/2009 12:05am

hello,
i have a function, that contains an array.
this function puts out a complete "select" tag with options and makes ONE option "selected".. getting from Session.

here some code:

	function ShowTheUserDataLiefergebiet($GetThis) {
		$data = array(
			'1' => "Bitte Auswählen",
			'2' => "Town1",
			'3' => "Town2",
			'4' => "Town3",
			'5' => "Town4"
		);
		$output = "
    <select name=\"Liefergebiet\" id=\"Liefergebiet\">";
	for($i = 1;$i <= count($data); $i++) {
		$selected = "";
		if(Session::get("Liefergebiet") == $i) {
			$selected = "selected=\"selected\"";
		}
		$output .= "<option ".$selected." value=\"".$i."\" >".$data[$i]."</option>";
	}
		$output .= "
    </select>
		";
        
		if(isset($GetThis)) {
			return $data[$GetThis];
		} else {
			return $output;
		}
	}

maybe its not that clear at the first view. i use this funtion in 2 different ways.

when i call the function like this:

$ShowTheUserDataLiefergebiet(2)

then i get the Value Name.. so here it's "Town 1".
in the "session data", there is "Liefergebiet" a number, because i want to make selected.. and because the value in the session is a number, i use this fuction to output the Name Value and not the number..

    <% control ShowTheUserData %>
            <p>$Name</p>
            <p>$Email</p>
            <p>....</p>
            <p>$Top.ShowTheUserDataLiefergebiet(2)</p>
    <% end_control %>

sooo... to output the $Liefergebiet value from the session, i do "$Liefergebiet" in ss-file.
but thing like THIS does not work.. :(

<p>$Top.ShowTheUserDataLiefergebiet($Liefergebiet)</p>

but this works:
<p>$Top.ShowTheUserDataLiefergebiet(2)</p>

soo, at first i did this.. the following code works:

            <p>Liefergebiet: 
                <% if Liefergebiet = 1 %>
                    $Top.ShowTheUserDataLiefergebiet(1)
                <% end_if %>
                <% if Liefergebiet = 2 %>
                    $Top.ShowTheUserDataLiefergebiet(2)
                <% end_if %>
                <% if Liefergebiet = 3 %>
                    $Top.ShowTheUserDataLiefergebiet(3)
                <% end_if %>
                <% if Liefergebiet = 4 %>
                    $Top.ShowTheUserDataLiefergebiet(4)
                <% end_if %>
                <% if Liefergebiet = 5 %>
                    $Top.ShowTheUserDataLiefergebiet(5)
                <% end_if %>
            </p>

but isn't there a better way?
help please :D

Avatar
bummzack

Community Member, 904 Posts

7 April 2009 at 6:47pm

Hi

To keep things clean (and to stick to the MVC Pattern), I encourage you to not write your HTML output in your controller methods whenever possible.
This is what I would do:
Create a function in your controller that outputs your data as DataObjectSet. In the following example, the function is called MyData.
As you can see, I've added a "Selected" entry to each item. This will determine which item is selected. Further below I'm getting the "LiefergebietIndex" (that would be a function that returns the current session value) and use it to set the desired item to "Selected".

function MyData()
{
	$data = array(
		array(
			'Key' => 1,
			'Value' => 'Test 1',
			'Selected' => false
		),
		array(
			'Key' => 2,
			'Value' => 'Test 2',
			'Selected' => false
		),
		array(
			'Key' => 3,
			'Value' => 'Test 3',
			'Selected' => false
		),
	);
	
	$data[$this->LiefergebietIndex()]['Selected'] = true;
	
	return new DataObjectSet($data); 
}

In your template you would write something like this:

<% if MyData %>
<select name="Liefergebiet" id="Liefergebiet">
	<% control MyData %>
		<% if Selected %>
		<option value="$Key" selected="selected">$Value</option>
		<% else %>
		<option value="$Key">$Value</option>
		<% end_if %>
	<% end_control %>
</select>
<% end_if %>

Your "ShowTheUserDataLiefergebiet" function does two things in one. I consider that as bad programming style though. Why not create a function for the options (eg. like MyData described above) and one to get the name for an index.
And if you're doing things like in your "ShowTheUserDataLiefergebiet" function, please check $GetThis right after the definition of the data and return the result if $GetThis is set. Otherwise you'll always build the HTML output, just to throw it away (because when you set $GetThis, it isn't needed).

Avatar
SalvaStripe

Community Member, 89 Posts

7 April 2009 at 8:00pm

hey cool, i think thats a good code.

i have a question..
instead of writing a function that gives me the Liefergebiet value from the session, i can write the Session var quite here, not?

   $data[$this->LiefergebietIndex()]['Selected'] = true; 

would be:

   $data[Session::get("Liefergebiet")]['Selected'] = true; 

or is there a reason to write functin for this.

greetz from germany :D

Avatar
bummzack

Community Member, 904 Posts

7 April 2009 at 8:14pm

Yes, you could of course directly use the session value. But if you do so, you'll have to make sure, the session variable is actually set and has a value in the allowed range.
The advantage of the function is, that you can use the value in different places and also do checks if the value actually exists. For example:

function LiefergebietIndex()
{
	// check if the session variable actually exists. 
	// otherwise return 0 (or 1.. whatever you prefer)
	if(!isset($_SESSION['Liefergebiet']))
		return 0;

	// get the session variable as integer
	$val = intval($_SESSION['Liefergebiet']);

	// check against a upper bound (max)
	$max = 5; // here we define max, but this would be the length of your data -1.
	return $val > $max ? $max : $val;
}

Avatar
SalvaStripe

Community Member, 89 Posts

7 April 2009 at 8:22pm

Edited: 07/04/2009 8:47pm

hm, my problem is, that i want to output "Test 1", "Test 2".. and not 1,2,3.. etc..

i did used this one function in 2 different ways, because i difined the array in this function.
when i write a new function to output the names with the value of the session, then i have to edit the arrays in 2 functions, when i have to edit something.

i tried to create this array as a global var, but there were just errors :(

can i change your code like this? :

<% if MyData %>
<select name="Liefergebiet" id="Liefergebiet">
   <% control MyData %>
      <% if Selected %>
      <option value="$Value" selected="selected">$Value</option>
      <% else %>
      <option value="$Value">$Value</option>
      <% end_if %>
   <% end_control %>
</select>
<% end_if %>

And in Function "MyData" make the selected like this:

for($i = 0 ; $i < count($data) ; $i++ ) {

    if($data[$i]['Value'] == Session::get("Liefergebiet")) {

        $data[$i]['Selected'] = true;

    }

}

then i can get everytime in my module get the name easy with "Session::get("Liefergebiet")"
AND the 'make selected code' will keep MVC, right?

hmm xD

Avatar
bummzack

Community Member, 904 Posts

7 April 2009 at 8:54pm

You don't have to define your array as a global variable. A static one will do just fine.

protected static $myData = array(
	// data as seen in previous code examples
);

Later in your functions you access the static array like this: self::$myData
You should be aware that if you modify this array (eg. setting Selected to true), the changed value will persist for subsequent calls during the same page-request. What you could do is to convert the array to the DataObjectSet and apply changes on the DataObjectSet instead of the array.