Jump to:

3212 Posts in 847 Topics by 809 members

Template Questions

SilverStripe Forums » Template Questions » Counting hits

Moderators: martimiz, Howard, Sean, Ryan M., biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 2684 Views
  • Jeroen
    Avatar
    Community Member
    6 Posts

    Counting hits Link to this post

    Hello,

    I'm trying to count hits when a page has been loaded. Everytime the page is loaded, it should count +1.
    Here is my code, but I can't get it to work properly.

    This is where i define RadioHits
    public static $db = array(
    'RadioHits' => 'Int',
    );

    public function init()
    {
    parent::init();
    $this->RadioHits = $this->RadioHits +1;
    $this->write();
    }

    Does anyone got an idea how to get this to work? Thanks in advance!

  • Pigeon
    Avatar
    Community Member
    238 Posts

    Re: Counting hits Link to this post

    Hey Jeroen,

    I'm suprised that isn't working for you. I don't have much to suggest in terms of how to get it working as it currently it, as that should work, but you could make it a bit neater like so:

    public function init()
    {
    parent::init();
    $this->RadioHits++;
    $this->write();
    }

    However, i think it would be much more efficient to run some custom SQL to do the increment. Using the raw SQL function to do something like:

    DB::query("UPDATE Radio SET RadioHits=RadioHits+1 WHERE ID=1");


    (see http://doc.silverstripe.org/doku.php?id=sqlquery#raw_sql_with_dbquery)

  • ajshort
    Avatar
    Community Member
    242 Posts

    Re: Counting hits Link to this post

    This could fail if your methods/properties are in the wrong place. Is your init() method in the controller class, and the database field specification in the model?

  • Jeroen
    Avatar
    Community Member
    6 Posts

    Re: Counting hits Link to this post

    Thanks for the replies.
    I've looked into my database and saw that the hits not getting counted. It does on the live site, but it doens't write it down in the database.

    Here is my code:

    <?php

    class RadioStation extends RadioOverzichtNL {
       
       public static $db = array(
          'RadioTitel' => 'Text',
          'RadioStream' => 'Text',
          'RadioWebsite' => 'Text',
          'RadioHits' => 'Int',
          'RadioMustKnow' => 'HTMLText',
       );
       
       static $icon = 'themes/radioaan/images/icons/station';
       
       public static $has_one = array(
          'RadioLogo' => 'Image',
       );
       
       function getCMSFields()
       {
          /*@var $fields FieldSet*/
          $fields = parent::getCMSFields();
          $fields->addFieldToTab('Root.Content.Main', new TextField('RadioTitel', 'Titel van het radiostation'));
          $fields->addFieldToTab('Root.Content.Main', new TextField('RadioStream', 'URL naar radiostream'));
          $fields->addFieldToTab('Root.Content.Main', new TextField('RadioWebsite', 'Link naar website van het radiostation (Plaatsen met http://www.)'));
          $fields->addFieldToTab('Root.Content.Main', new TextField('RadioHits', 'Hoe populair is dit radiostation'));
          $fields->addFieldToTab('Root.Content.Main', new HtmlEditorField('RadioMustKnow', 'Must know van het radiostation. Dit kan zijn een leuke actie, programma, etc.'));
          $fields->addFieldToTab("Root.Content.Main", new ImageField('RadioLogo', 'Logo van het radiostation'));   
          $fields->removeFieldFromTab("Root.Content.Main","Content");
          return $fields;      
       }
       
    }

    class RadioStation_Controller extends RadioOverzichtNL_Controller {
       
       public function init()
    {
    parent::init();
    $this->RadioHits = $this->RadioHits +1;
    $this->write();
    }
       
    }

    ?>

  • Pigeon
    Avatar
    Community Member
    238 Posts

    Re: Counting hits Link to this post

    Hi Jeroen,

    Post the solution please 0:-)

  • Jeroen
    Avatar
    Community Member
    6 Posts

    Re: Counting hits Link to this post

    @Pigeon, I almost forgot to post it, haven't had much time lately!
    Well, thanks to Pigeon's help on the SilverStripe IRC channel, my problem got fixed.

    A solution which might be interested for others too when counting pagehits of a certain page.

    First of all add this at the top of your controller

    public static $db = array(
          'RadioHits' => 'Int', // Change RadioHits to anything else you want
       );

    Define the CMS textfield under the Main tab

    function getCMSFields()
       {
          /*@var $fields FieldSet*/
          $fields = parent::getCMSFields();
          // This is where you define the field to be shown in the CMS
    $fields->addFieldToTab('Root.Content.Main', new TextField('RadioHits', 'Hoe populair is dit radiostation'));
          return $fields;      
       }

    Write the pagehits to the database

    public function init()
    {
    parent::init();
    // This is where it sends the information to the database.
    // Make sure you edit the RadioStation_Live to the name of your pagetype, same goes for RadioHits
           DB::query("UPDATE `RadioStation_Live` SET RadioHits=RadioHits+1 WHERE ID=$this->ID");
    }

    Hope this could help others out with counting hits of a page!

  • ajshort
    Avatar
    Community Member
    242 Posts

    Re: Counting hits Link to this post

    That's a really bad solution - you should never directly to the live tables since it will lead to syncing problems when you re-publish a page and erase your data, as well as not reflecting the appropriate data on the draft site. Instead use

    $this->write();
    $this->doPublish();

    to publish data once it is written.

  • Jeroen
    Avatar
    Community Member
    6 Posts

    Re: Counting hits Link to this post

    Thanks for your reply ajshort

    Well when adding this it doesn't write down the hits in the DB

    $this->RadioHits++;
    $this->write();
    $this->doPublish();

    Another way might be to write the hits to both live as the normal page table in the DB. What do you think?

    2684 Views
Page: 1 2
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.