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

Problem adding counter hit to the template page


Reply


6 Posts   1740 Views

Avatar
phamo

Community Member, 2 Posts

12 January 2011 at 7:07pm

Hi
I am new to SilverStripe. I need to put a simple counter hit on the front page for my friend website. I have read the tutorial as how to create simple counter page at http://www.sslearn.info
I have created the PageCounter.php in /mysite/code/ as instructed.
[code taking from sslearn.info]
<?
class PageCounter extends DataObject{
static $db = array(
'Counter' => 'Int',
);
static $has_one = array(
'Page' => 'Page',
);
}
?>
[/code taking from sslearn.info]

adding counter function and code to output the content in /mysite/code/Page.php
insert in within the init() function of Page_Controller class
[code taking from sslearn.info]
$pagecounter = DataObject::get_one("PageCounter","PageID='$this->ID'");
if(!$pagecounter){
$pagecounter = new PageCounter();
$pagecounter->PageID=$this->ID;
}
$pagecounter->Counter = $pagecounter->Counter+1;
$pagecounter->write();
[/code taking from sslearn.info]

insert in within Page_Controller class
[code taking from sslearn.info]
public function pagecount(){
$pagecounter = DataObject::get_one("PageCounter","PageID='$this->ID'");
return $pagecounter->Counter;
}
[/code taking from sslearn.info]

I have rebuild the db and flush the page, everything is fine. In my Page.ss I tried to use the function above
<% control Counter %>
$Counter
<% end_control %>
There is nothing return. Hope that someone could help me here.
Many thanks in advance,

Phamo

Avatar
Willr

Forum Moderator, 5513 Posts

12 January 2011 at 8:14pm

Not sure if you missed any code but <% control Counter %> doesn't seem to point to any functions. However it looks like pagecount() outputs the counter so you would use $pagecount in the template.

Avatar
phamo

Community Member, 2 Posts

12 January 2011 at 8:33pm

Thank you, yeah! it works now.

Avatar
haantje72

Community Member, 69 Posts

18 February 2011 at 7:56am

Since an new serverupdate i couldnt build the db with the pagecounter. Databases were not builded. The PageCounter.php code was the problem <? should be <?php

good working code:

PageCounter.php
____________________________

<?php
class PageCounter extends DataObject {
static $db = array(
'Counter' => 'Int'
);
static $has_one = array(
'Page' => 'Page'
);
}
?>

____________________________
added in page.php (code was OK)
replace from: class Page_Controller
till: parent::init();
____________________________

class Page_Controller extends ContentController {

public static $allowed_actions = array (
   );
   
public function init() {
$pagecounter = DataObject::get_one("PageCounter","PageID='$this->ID'");
if(!$pagecounter){
$pagecounter = new PageCounter();
$pagecounter->PageID=$this->ID;
}
$pagecounter->Counter = $pagecounter->Counter+1;
$pagecounter->write();

parent::init();
      
// Note: you should use SS template require tags inside your templates
// instead of putting Requirements calls here. However these are
// included so that our older themes still work
Requirements::themedCSS("layout");
Requirements::themedCSS("typography");
Requirements::themedCSS("form");
}

_______________________________
second added in page.php right before the end:
}
?>
_______________________________

public function pagecount(){
$pagecounter = DataObject::get_one("PageCounter","PageID='$this->ID'");
return $pagecounter->Counter;
}

________________________________
Then my Themes/ Page.ss added just where i wanted to show
________________________________

$pagecount

________________________________

All works great now.

Avatar
toby08

Community Member, 1 Post

10 March 2011 at 12:35am

Based on your solution, is there a way to list the 5 most popular pages with page title and link?

Avatar
MitraX

Community Member, 20 Posts

10 May 2012 at 3:03am

Based on your solution, is there a way to list the 5 most popular pages with page title and link?

For displaying the most popular posts check out [url=http://www.inforbiro.com/blog-eng/silverstripe-most-popular-articles/]this article[/url]