21278 Posts in 5728 Topics by 2599 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 1146 Views |
-
Problem adding counter hit to the template page

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
-
Re: Problem adding counter hit to the template page

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.
-
Re: Problem adding counter hit to the template page

12 January 2011 at 8:33pm
Thank you, yeah! it works now.
-
Re: Problem adding counter hit to the template page

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.
-
Re: Problem adding counter hit to the template page

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?
-
Re: Problem adding counter hit to the template page

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 this article
| 1146 Views | ||
|
Page:
1
|
Go to Top |


