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

Number of site displays


Go to End
Reply


20 Posts   2139 Views

Avatar
sebastiankozub

Community Member, 40 Posts

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

Avatar
bartvanirsel

Community Member, 94 Posts

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.

Avatar
sebastiankozub

Community Member, 40 Posts

30 October 2010 at 6:09am

Edited: 30/10/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

Avatar
Willr

Forum Moderator, 5513 Posts

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.

Avatar
sebastiankozub

Community Member, 40 Posts

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.

Avatar
Willr

Forum Moderator, 5513 Posts

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).

Avatar
sebastiankozub

Community Member, 40 Posts

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!

Avatar
Willr

Forum Moderator, 5513 Posts

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.

Go to Top