Jump to:

22996 Posts in 11620 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Number of site displays

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

Page: 1 2 3
Go to End
Author Topic: 1740 Views
  • sebastiankozub
    Avatar
    Community Member
    40 Posts

    Number of site displays Link to this post

    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

  • bartvanirsel
    Avatar
    Community Member
    88 Posts

    Re: Number of site displays Link to this post

    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.

  • sebastiankozub
    Avatar
    Community Member
    40 Posts

    Re: Number of site displays Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Number of site displays Link to this post

    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.

  • sebastiankozub
    Avatar
    Community Member
    40 Posts

    Re: Number of site displays Link to this post

    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.

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Number of site displays Link to this post

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

  • sebastiankozub
    Avatar
    Community Member
    40 Posts

    Re: Number of site displays Link to this post

    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!

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Number of site displays Link to this post

    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.

    1740 Views
Page: 1 2 3
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.