Click here to Skip to main content
11,567,926 members (45,510 online)
Click here to Skip to main content

Tagged as

Concrete5 - Display page views count

, 2 Jul 2013 CPOL 4.2K 1
Rate this:
Please Sign up or sign in to vote.
Sometimes it is required to display the number of page views of blog posts, discussions, or other pages. This is how to do it.

Display number of page views count

Firstly you need to create a page called page_counter.php and put the below code there:

defined('C5_EXECUTE') or die(_("Access Denied."));
 
Loader::model('page_statistics');
 
class PageCounter extends PageStatistics {
 
public static function getTotalPageViewsForPageID($cID = null, $date = null) {
    $db = Loader::db();
    if ($date != null) {
        return $db->GetOne("select COUNT(pstID) FROM PageStatistics where date = ? and cID = ?", 
                           array($date,$cID));
    } else {
        return $db->GetOne("select COUNT(pstID) FROM PageStatistics where cID = ?", array($cID));
    }
}
 
public static function getRegisteredPageViewsForPageID($cID = null, $date = null) {
    $db = Loader::db();
    if ($date != null) {
        return $db->GetOne("SELECT COUNT(pstID) FROM PageStatistics WHERE date = ? AND cID = ? AND uID != 0", 
                           array($date,$cID));
    } else {
        return $db->GetOne("SELECT COUNT(pstID) FROM PageStatistics WHERE cID = ? AND uID != 0", 
                           array($cID));
    }
}
 
public static function getVisitorsPageViewsForPageID($cID = null, $date = null) {
    $db = Loader::db();
    if ($date != null) {
        return $db->GetOne("SELECT COUNT(pstID) FROM PageStatistics WHERE date = ? AND cID = ? AND uID = 0", 
                           array($date,$cID));
    } else {
        return $db->GetOne("SELECT COUNT(pstID) FROM PageStatistics WHERE cID = ? AND uID = 0", array($cID));
    }
}
}

Then upload the page to the roots models folder. Here I've defined three different methods which count total page views, page views by registered users, and page views by visitors. After that you need to put the below code where you want to display the page view statistics.

Loader::model('page_counter');

echo 'Total Page Views: '.PageCounter::getTotalPageViewsForPageID($cobj->getCollectionID()).'';
 
echo 'Registered User ('.PageCounter::getRegisteredPageViewsForPageID($cobj->getCollectionID()).'), ';
 
echo 'Visitors ('.PageCounter::getVisitorsPageViewsForPageID($cobj->getCollectionID()).')';

Note: $cobj->getCollectionID() is the page ID, might be changed if any other variable is used instead of $cobj.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rony Sur
Software Developer (Senior) Frost & Sullivan Pvt Ltd
India India
I'm a Senior Software & Application developer as well as Web Designer from Kolkata, India. I'm currently working on a leading web based premier Software development and IT consulting company. I can approach helps my customers gain competitive edge over their peers. I generally favor open-source technologies such as PHP, MySQL, Smarty, JavaScript, Ajax and Jquery, and I have extensive experience using WordPress, Magento, Zen Cart, Os-commerce, Open Cart, CakePHP & Concrete5. I've an extensive knowledge in this web industry for more than 6 years.

You can contact me directly through email or Skype.
My email ID: rony.sur007{at}gmail{dot}com
Skype: rony.sur007

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150624.2 | Last Updated 2 Jul 2013
Article Copyright 2013 by Rony Sur
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid