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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Variables only displaying when page is refreshed


Reply


3 Posts   422 Views

Avatar
neilos

Community Member, 19 Posts

1 November 2012 at 11:30pm

Hi,

I asked this in a different section, but haven't received a reply in almost a week.
http://www.silverstripe.org/data-model-questions/show/21293

Since asking, I have modified it a bit, but still only works when refreshing the page.

I am trying to get a variable from various google location ads to change the phone number depending on the ?number=xxx from the ad.

Currently the number displays a default value unless the page is refreshed, then the variable is displayed.
I need it to display the number without the need for a refresh.

Is there a better way of doing this that will display the variable number without the need for a page refresh?

In page.ss

<% if phonenumber %>$phonenumber <% else %>0800 xxx xxx<% end_if %>


In page.php

public function Phonenumber() {
if (!isset($_SESSION['number']))
{
$phonenumber = $_GET['number'];
$_SESSION['number'] = $phonenumber;
}
return strip_tags (Session::get ('number'));
}


Any advice or pointers are massively appreciated.

Avatar
Willr

Forum Moderator, 5513 Posts

2 November 2012 at 11:24am

You might be better writing that function as

$number = Session::get('number');

if($get = $this->request->getVar('number')) {
$number = $get;
Session::set('number', $get);
}

return Convert::raw2xml($number);

Avatar
neilos

Community Member, 19 Posts

3 November 2012 at 12:54am

Willr, thanks. That has fixed all my woes.
Except the admin section is white screening with no error being thrown.

I created a different page type and put this function on there instead of page.php and the admin section is accessible again.

One more question, is it possible to use a field in the CMS that can be used as the variable?
I.E.

$fields->addFieldToTab('Root.Content.Main', new TextField('StaticNumber'), 'Content');

is this possible?