We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37213
    • 25 Posts
    My searching friend Google can't help me this time, hopefully I can find the answer here.

    I'm looking for a plug-in or snippet that can show up the top 5 most read documents inside modx.

    I checked out the hit page counter but this one is only working for evolution. An other one is only counting the page and not the specific document.

    Thanks for your time.

    This question has been answered by BobRay. See the first response.

      • 32316
      • 387 Posts
      Google found this thread:
      http://forums.modx.com/thread/44100/page-hit-counter-for-modx-revolution

      This might be a good starting point (disclaimer: I have not used, or even looked carefully at this code)
      It would be fairly easy to just add to the page ID to the data stored in the table.
      Now you just need to retrieve the top five
      So you would have to create a query to count the total number of hits to each page, do a sort and take the top five ... simple!

      Or it probably would not be to hard to adapt an evo hit counter to work in revo
      http://modx.com/extras/package/pagehitcounter2

      [ed. note: whistlemaker last edited this post 12 years, 6 months ago.]
      • discuss.answer
        • 3749
        • 24,544 Posts
        It's easy to save the page hit count in a TV and update it with either a snippet in the template or a plugin tied to the OnWebPageComplete System Event. Unfortunately, it's tricky to do a query for the top five because TV values for specific resources are not stored in the site_content table, but in a separate table that contains only doc ID (contentid), TV ID (tmplvarid), and the value (value). The correct object for the query is modTemplateVarResource. Assuming that the TV is set and the TV's ID is 12 (untested):

        Your Tpl chunk might look something like this:

        <li><a href="[[~[[+id]]]]">[[+pagetitle]]</a></li>


        And the code to get the output:

        <?php
        $c = $modx->newQuery('modTemplateVarResource');
        $c->sortby('value', 'DESC');
        $c->limit(5);
        $c->where(array('tmplvarid' => 12));
        
        $tvrs = $modx->getCollection('modTemplateVarResource', $c);
        
        foreach($tvrs as $tvr) {
          $resourceId = $tvr->get('contentid');
          $resource = $modx->getObject('modResource', $resourceId);
          $fields = array(
                   'pagetitle' => $resource->get('pagetitle'),
                   'id' => $resourceId,
               );
          $output .= $modx->getChunk('MyChunk', $fields); 
        }
        return $output;
        


        However, if there is a resource field your are not using (e.g., introtext or description), you can save the page count there. Then the code gets somewhat simpler and faster:

        <?php
        $c = $modx->newQuery('modResource');
        $c->sortby('description', 'DESC');
        $c->limit(5);
        
        $resources = $modx->getCollection('modResource', $c);
        
        foreach ($resources as $resource) {
               $fields = array(
                   'pagetitle' => $resource->get('pagetitle'),
                   'id' => $resource->get('id'),
               );
              $output .= $modx->getChunk('MyChunk', $fields);
        }
        
        return $output;
        


        The code to set the TV with a snippet would look like this (assuming that the TV is called "pagecount"):

        <?php
        $pagecount = $modx->resource->getTVValue('pagecount') + 1;
        $tv = $modx->getObject('modTemplateVar', 12);
        $tv->setValue($modx->resource->get('id'), $pagecount);
        $tv->save();
        


        Using the description field, it would look like this:

        <?php
        $pagecount = $modx->resource->get('description') + 1;
        $modx->resource->set('description', $pagecount);
        $modx->resource->save();
        



        [ed. note: BobRay last edited this post 12 years, 5 months ago.]
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 37213
          • 25 Posts
          @whistlemaker: I used the resources that you said.

          @BobRay: thanks for your information, gone work on it today.
            • 37213
            • 25 Posts
            Finally I got time today to work on the most read top 5 document.

            Want thanks BobRay very much for his kindly help.

            I got this working, page is saved in database. Now I try to call the result to the page. my code is [[ !top5 ]] (my snippet call top5), the list bullet is showing up and via firebug I can see that the web link is created, when I'm using [[ +id ]] instead of [[ +pagetitle], the page id + web link is showing correctly.

            Who can help me with the last part of this issue. thanks guys.
            Love MODX more and more.. [ed. note: viewsonic last edited this post 12 years, 5 months ago.]
              • 3749
              • 24,544 Posts
              Sorry, mistake in my code (fixed above).

              This line:

              'pagetitle' => $tvr->get('pagetitle'),


              Should be:

              'pagetitle' => $resource->get('pagetitle'),
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 37213
                • 25 Posts
                This is awesome, everything working now. Thank you so much. [ed. note: viewsonic last edited this post 12 years, 5 months ago.]
                  • 3749
                  • 24,544 Posts
                  Glad I could help. Thanks for reporting back. smiley
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting