We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33982 ☆ A M B ☆
    • 476 Posts
    So I’m creating a site that is going to have a section on the homepage for the top 10 most viewed articles. Obviously I could update it daily myself by using something like Google Analytics or such to determine the most viewed pages. But I was wondering if there is a snippet that’s been created already that can handle that automatically versus someone like myself having to manually update it? Also on this subject (and maybe this ties in to this request), is there a snippet that can has a counter of how many times the page has been viewed and will output that number on the page itself (each time the page is viewed, the counter recognizes that)? I was going to add a video section to the website and I was wanting to have a view counter for each time a particular video has been viewed. I’m not sure if my first request would tie into my 2nd request being that the snippet (if it exists) would monitor the times the document has been viewed and output it in the top 10 most viewed in this case on the homepage or where ever else I want it to list them...if that makes sense.
      Precision Web Development ... SmashStack.com
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      http://modxcms.com/Page-Hit-Counter-878.html

      Although I believe you might need to make a custom snippet to query the table to get the top most viewed documents. The ViewPageHitCounter snippet should get you going in the right direction.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 33982 ☆ A M B ☆
        • 476 Posts
        Thanks so much for that info Susan, I really appreciate it...that definitely helps get me going in the right direction...thanks!
          Precision Web Development ... SmashStack.com
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Don’t forget to add your TopViewed snippet to the repository wink
            Studying MODX in the desert - http://sottwell.com
            Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
            Join the Slack Community - http://modx.org
            • 33982 ☆ A M B ☆
            • 476 Posts
            Quote from: sottwell at Aug 25, 2008, 06:12 AM

            Don’t forget to add your TopViewed snippet to the repository wink
            Yeh for sure. Since I don’t know a lick of PHP, will definitely have to figure this one out. But when I do, definitely will give that to the community.
              Precision Web Development ... SmashStack.com
              • 4310
              • 2,310 Posts
              Here’s a snippet to extend Susans "Page Hit Counter" module and plugin.
              Firstly the snippet takes 4 parameters:
              [!MostPopular? &excludeIDs=`1,2,3` &limit=`10` &table_prefix=`custom_` &tpl=`your_tpl`!]

              Fairly self explanatory, the &excludeIDs might be useful for 404 pages, Home page etc.
              Here’s the snippet code:
              <?php
              $table_prefix = (isset($table_prefix)) ? $table_prefix : 'modx_';
              $limit = (isset($limit)) ? $limit : 5;
              $excludeIDs = (isset($excludeIDs)) ? $excludeIDs : '0';
              $tpl = (isset($tpl)) ? $tpl : 'top_pages';
              $output = '';
              $sql = mysql_query('SELECT * FROM `'.$table_prefix.'page_hit_counter`, `'.$table_prefix.'site_content` WHERE '.$table_prefix.'page_hit_counter.page_id='.$table_prefix.'site_content.id AND '.$table_prefix.'site_content.id NOT IN('.$excludeIDs.')ORDER BY `page_count` DESC LIMIT '.$limit.'');
              $resultArray = $modx->db->makeArray( $sql );
              foreach($resultArray as $item)
              {
              $params['phc.count']=$item['page_count'];
              $params['phc.pageID']=$item['page_id'];
              $params['phc.title']=$item['pagetitle'];
              $params['phc.longtitle']=$item['longtitle'];
              $params['phc.description']=$item['description'];
              $output.=$modx->parseChunk(''.$tpl.'', $params, '[+', '+]');
              }
              return $output;
              ?>

              You will also need a chunk with whichever placeholders you want to use from :
              [+phc.count+] [+phc.pageID+] [+phc.title+] [+phc.longtitle+] [+phc.description+] 

              The default one named "top_pages" is here:
              <p>Total Hits = [+phc.count+] for <a href="[~[+phc.pageID+]~]" title="[+phc.title+]">[+phc.longtitle+]</a> - [+phc.description+]</p>

              I haven’t tested it much, but it seems to work okay.
              Report back with any problems!
              If it works out okay maybe Susan can add it to PHC family.

              David
                • 33982 ☆ A M B ☆
                • 476 Posts
                Wow David! Thanks a million for taking the time to do this...I am so grateful...thank you!

                I’m going to look over it the next week or two as I prepare for this up and coming project. I don’t know a thing about PHP (but I’m trying to learn), so I’m going to look over this and if I have any questions I can’t figure out, I’ll post them on this thread.

                Again, thank you so much!
                  Precision Web Development ... SmashStack.com
                  • 33982 ☆ A M B ☆
                  • 476 Posts
                  Hey David,

                  I just did a test run on a site I set up for testing out snippets and such...it works awesome with Susan’s Page Hit Counter.  I can not thank you enough! 

                  I’ll put it in the Repository.

                  See it in action here (just open this page, open a new tab and click around on the other pages and watch as it updates...pretty nifty!):

                  http://modxtest.aldriena.com/index.php?id=54
                    Precision Web Development ... SmashStack.com
                    • 4310
                    • 2,310 Posts
                    Glad it worked okay.
                      • 7231
                      • 4,205 Posts
                      cool grin
                        [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                        Something is happening here, but you don&#39;t know what it is.
                        Do you, Mr. Jones? - [bob dylan]