3212 Posts in 847 Topics by 809 members
| Go to End | Next > | |
| Author | Topic: | 2684 Views |
-
Counting hits

27 October 2009 at 12:14pm
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!
-
Re: Counting hits

27 October 2009 at 1:04pm
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) -
Re: Counting hits

27 October 2009 at 3:24pm
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?
-
Re: Counting hits

27 October 2009 at 8:26pm Last edited: 27 October 2009 8:38pm
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();
}
}?>
-
Re: Counting hits

29 October 2009 at 11:29am Last edited: 29 October 2009 11:30am
@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!
-
Re: Counting hits

29 October 2009 at 11:37am
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.
-
Re: Counting hits

29 October 2009 at 12:00pm
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 | ||
| Go to Top | Next > |



