Jump to:

22998 Posts in 11620 Topics by 2827 members

General Questions

SilverStripe Forums » General Questions » Problem adding counter hit to the template page

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
Go to End
Author Topic: 1505 Views
  • phamo
    Avatar
    Community Member
    2 Posts

    Problem adding counter hit to the template page Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Problem adding counter hit to the template page Link to this post

    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.

  • phamo
    Avatar
    Community Member
    2 Posts

    Re: Problem adding counter hit to the template page Link to this post

    Thank you, yeah! it works now.

  • haantje72
    Avatar
    Community Member
    69 Posts

    Re: Problem adding counter hit to the template page Link to this post

    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.

  • toby08
    Avatar
    Community Member
    1 Post

    Re: Problem adding counter hit to the template page Link to this post

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

  • MitraX
    Avatar
    Community Member
    20 Posts

    Re: Problem adding counter hit to the template page Link to this post

    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

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