21309 Posts in 5738 Topics by 2603 members
| Go to End | Next > | |
| Author | Topic: | 1472 Views |
-
Number of site displays

28 October 2010 at 7:34am
Hi,
I'm looking for code similar to http://silverstripe.org/general-questions/show/13811
but I'd like to count number of visitors for whole page, so if someone visit Page 1, Page 2 and Page 3 it will be counted as "one visit", and I'd like to diplay this number on every Page.Thanks
-
Re: Number of site displays

28 October 2010 at 9:14am
Hi,
Solution could be decorating your siteconfig with variable and using this for incrementing
on every hit but i think this will eventually slow down your site. -
Re: Number of site displays

30 October 2010 at 6:09am Last edited: 30 October 2010 6:12am
I had idea to add variable to Page beacuse every page in my site extends Page - the variable will be number of visits.
Then in Page_Controller I will add function silmilar to
function init() {
parent::init();
if( ?? cookie or session variable doesn't exist ?? ) {
?? create session or cookie ??
$this->Visits++;
$this->write();
}
}This one incrementing Visits every time when page is displayed and Session or Cookie does not exist
// then the query to return number of visits in template
function Visits() {
return DataObject::get(?? Visists ??);
}I'm begginer in SS and Sapphire.
Could anyone helped?
I'd like to anyone helped me with code between question marks
Thanks -
Re: Number of site displays

30 October 2010 at 2:48pm
You can use the Cookie class to save / read cookies http://api.silverstripe.org/2.4/sapphire/misc/Cookie.html. You could also use the session class but cookies might work better.
For the function to get the visits you won't need to use a DataObject::get() since you already have the page object. Using $Visits or $this->Visits (if you're in the PHP) will output the number.
-
Re: Number of site displays

2 November 2010 at 11:25am
yes, but the problem is that $Visits variable should be over the Page class, because I want to create site visits couter, not page visits counter. When I put $Visits in Page class it will count only for specific obect of Page class.
-
Re: Number of site displays

2 November 2010 at 11:49am
You could either make a database table (or use one like SiteConfig) which is global, an easier way would be to keep visits on the page instances and create a function like
function TotalVisits() {
return DB::query("SELECT SUM(Visits) FROM Page_Live")->value();
}So $TotalVisits will be the value for all the visitors and $Visits will still be the value for the individual page (in case you need to track it).
-
Re: Number of site displays

5 November 2010 at 4:36am
Thanks Willr,
I did like this:In class Page
public static $db = array(
'ShowInTopMenu' => 'Boolean',
'ShowInLeftMenu' => 'Boolean',
'ShowInLeftMenu2' => 'Boolean',
'visits' => 'Int'
);In class Page_Controller
function totalVisits()
{
return DB::query("SELECT SUM(visits) FROM page_live")->value();
}
public function init()
{
parent::init();Requirements::themedCSS('layout');
Requirements::themedCSS('typography');
Requirements::themedCSS('form');$this->visits++;
$this->write();
}And in template the $totalVisits variable show 0. I think that this line >> $this->visits++; << does not work because in database I noticed that whatever page I display value of visits is always 0.
Please, help where is the mistake.
Thanks! -
Re: Number of site displays

5 November 2010 at 10:19am
Its possible that $this->visits++; is saving to the record in the Page table (rather than Page_Live). So check out the Page table to see if it contains the values.
| 1472 Views | ||
| Go to Top | Next > |



