We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26503
    • 620 Posts
    I'm trying to implement a page counter using a plugin [i.e. no need to add tags to resources]

    <?php
    
    $tvcount = 6;
    
    $tvtrack = 7;
    
    $id = $modx->resource->get('id');
    
    $resource = $modx->getObject('modResource', $id);
    
    $count = ($resource->getTVValue('hitcount') + 1);
    
    $modx->log(modX::LOG_LEVEL_ERROR, 'count = ' .  $count );
    
    if (!$resource->setTVValue('hitcount', $count)) {
    
        $modx->log(xPDO::LOG_LEVEL_ERROR, 'Error setting page count');
    
    }
    
    $modx->log(modX::LOG_LEVEL_ERROR, 'Count TV set');
    
    return;



    It works "once" pretty sure the plugin is getting cached or the TV values. [or more likely the resource is cached with it's TV values]

    how can I get the uncached values for the TVs & update them?
      *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

      Sean Kimball CLP, CLS.
      Technical Director / Sr. Developer | BigBlock Studios
      ._______________________________________________.
      Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
      27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
      phone/fax: 905-426-5525
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Does the value get changed in the database with each hit? Where, exactly, is it not changing where you expect it to? How are you using the value?
        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
        • 26503
        • 620 Posts
        Quote from: sottwell at Mar 17, 2015, 02:16 AM
        Does the value get changed in the database with each hit? Where, exactly, is it not changing where you expect it to? How are you using the value?

        It changes once in the database and viewing the resource in the manager. hit the page one or several times and it won't change, refresh the cache and it will increment one more time.

        eventually it will get used for "most viewed" resources...
          *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

          Sean Kimball CLP, CLS.
          Technical Director / Sr. Developer | BigBlock Studios
          ._______________________________________________.
          Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
          27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
          phone/fax: 905-426-5525
          • 32699 ☆ A M B ☆
          • 427 Posts
          Why not use the packages which already do this?

          http://modx.com/extras/package/hitspage

          <small class="text-center">Views: [[!HitsPage]]</small>


          That's all that is needed.

          <?php
          $id = $modx->resource->get('id'); 
          $resource = $modx->getObject('modResource', $id); 
          if ($resource instanceof modResource){
              $count = ($resource->getTVValue('hitcount')); 
              $resource->setTVValue('hitcount', ($count+1)); 
          }
          return true;
          


          Try this. [ed. note: wshawn last edited this post 11 years, 7 months ago.]
            Get your copy of MODX Revolution Building the Web Your Way http://www.sanitypress.com/books/modx-revolution-building-the-web-your-way.html

            Check out my MODX || xPDO resources here: http://www.shawnwilkerson.com
            • 26503
            • 620 Posts
            Because I didn't want the extra markup in the templates/resources for the tracking code - I need it to run from a plugin.


            Quote from: wshawn at Mar 17, 2015, 09:29 AM
            Why not use the packages which already do this?

            http://modx.com/extras/package/hitspage

            <small class="text-center">Views: [[!HitsPage]]</small>


            That's all that is needed.

            <!--?php
            $id = $modx--->resource->get('id'); 
            $resource = $modx->getObject('modResource', $id); 
            if ($resource instanceof modResource){
                $count = ($resource->getTVValue('hitcount')); 
                $resource->setTVValue('hitcount', ($count+1)); 
            }
            return true;
            


            Try this.
              *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

              Sean Kimball CLP, CLS.
              Technical Director / Sr. Developer | BigBlock Studios
              ._______________________________________________.
              Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
              27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
              phone/fax: 905-426-5525
              • 26503
              • 620 Posts
              I wound up doing this:

              <?php
              
              $tvcount = 6;
              
              $tvtrack = 7;
              
              $id = $modx->resource->get('id');
              
              $resource = $modx->getObject('modResource', $id);
              
              // should we be tracking?
              if($resource->getTVValue('trackpage') == 0){
              
              	//$modx->log(xPDO::LOG_LEVEL_ERROR, 'Not tracking this page!');
              
              	return;
              
              }
              
              $sql = "select * from modx_site_tmplvar_contentvalues where `tmplvarid` = '$tvcount' and `contentid` = '$id';";
              
              $results = $modx->query($sql);
              
              if(!is_object($result)){$newcount = 1;}
              
              while ($result = $results->fetch(PDO::FETCH_ASSOC)) {
              
              	   //$modx->log(modX::LOG_LEVEL_ERROR, 'result = ' . print_r($result, TRUE));
              
              	   $tvid = $result['id'];
              
              	   $newcount = ($result['value'] + 1);
              
              }
              
              if (!$resource->setTVValue('hitcount', $newcount)) {
               
                  //$modx->log(xPDO::LOG_LEVEL_ERROR, 'Error setting page count');
               
              }
              
              return;




              The interesting part about this is that the tv I'm using to turn page tracking on or off defaults to "on" == "1", the default values do not turn up in the modx_site_tmplvar_contentvalues table, it only gets a record in the table if it is something ~other~ than default. Just a little 'gotcha'










                *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

                Sean Kimball CLP, CLS.
                Technical Director / Sr. Developer | BigBlock Studios
                ._______________________________________________.
                Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
                27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
                phone/fax: 905-426-5525
                • 32699 ☆ A M B ☆
                • 427 Posts
                Quote from: sean69 at Mar 17, 2015, 09:46 AM
                I wound up doing this:

                <!--?php
                
                return;


                The interesting part about this is that the tv I'm using to turn page tracking on or off defaults to "on" == "1", the default values do not turn up in the modx_site_tmplvar_contentvalues table, it only gets a record in the table if it is something ~other~ than default. Just a little 'gotcha'

                Depends on what is being sent. It if is a 0 then intval($var) is more reliable.

                It appears the definition of the Template Variable could be an issue. For tracking the hits, it should be a string.

                Anyway, glad you have it working.

                I opted to go for a plugin which already handed it all and I just put the call in the templates.

                  Get your copy of MODX Revolution Building the Web Your Way http://www.sanitypress.com/books/modx-revolution-building-the-web-your-way.html

                  Check out my MODX || xPDO resources here: http://www.shawnwilkerson.com
                  • 26503
                  • 620 Posts
                  Quote from: wshawn at Mar 17, 2015, 10:05 AM


                  Depends on what is being sent. It if is a 0 then intval($var) is more reliable.

                  It appears the definition of the Template Variable could be an issue. For tracking the hits, it should be a string.

                  Anyway, glad you have it working.

                  I opted to go for a plugin which already handed it all and I just put the call in the templates.


                  My point on that one is that if you tried to query the modx_site_tmplvar_contentvalues table for the trackpage TV you would get no results if it was still default.
                    *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

                    Sean Kimball CLP, CLS.
                    Technical Director / Sr. Developer | BigBlock Studios
                    ._______________________________________________.
                    Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
                    27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
                    phone/fax: 905-426-5525
                    • 3749
                    • 24,544 Posts
                    Yes, that's always been the case. If the TV is set to it's default value, there's no modTemplateVarResource object for it. It's bitten me several times. It saves space in the DB, but it slows things down and makes the code to deal with TV values much more complicated.
                      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
                      • 28042 ☆ A M B ☆
                      • 24,524 Posts
                      Isn't there an API for getting a TV's value that does fall back to the TV default if it's empty?
                        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