We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 47898
    • 14 Posts
    Hello MODX community,

    i did some research in the forum but can't find a answer related to my problem:

    I tried to integrate a simple page counter (hit counter) using a numeric TV and a snippet call in the template that increases that TV. Here is my snippet code:

    <?php
    $pagecount = $modx->resource->getTVValue('PageCounter');
    $pagecount = intval($pagecount);
    
    //if tv empty yet or not numeric, set zo zero;
    if($pagecount == '' OR !is_numeric($pagecount)){$pagecount = 0;};
    
    //increase count
    $pagecount++;
    
    //set tv
    if (!$modx->resource->setTVValue('PageCounter', $pagecount)) {
        $modx->log(xPDO::LOG_LEVEL_ERROR, 'The page count could not be updated!');
    }
    
    //return count
    return $pagecount;
    


    In the template (where the numeric TV 'PageCounter' is assigned to) i placed this snippet call [[!CountPageview]] (thats the snippet you see above). As is understand, the exclamation mark should cause the snippet call to be uncached, but the TV is only updated once after every cache clear. Is this a bug or i am doing wrong? I found a similar issue on Github (https://github.com/modxcms/revolution/issues/8360) but there this problem is claimed as solved. I am running the latest version of Revo (2.2.14).

    If you have any suggestions whats the problem and how i can resolve ist, i would really much appreciate it!

    BTW: I am new to MODX and this is my first post on this forum – so let me say that i really love MODX and the great resources out there. Lovely!

    André [ed. note: digitalbricks last edited this post 12 years, 3 months ago.]
      • 4172
      • 5,888 Posts
      I think, this should work.
      Do you have put this snippet directly into your template, or is it nested somehow, for example in another chunk or something, which might be cached?
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 47898
        • 14 Posts
        Hello Bruno17,
        thanks for your fast reply!

        The snippet is placed directly in a template – no further nesting in a chunk or so.
        Thats really curious, i have another custom snippet for getting some values from the URL wich seems to work fine. But this snippet output is always cached.
          • 47898
          • 14 Posts
          I just did a simple test with a new snippet that returns the current timestamp (via PHP time()). When i call this snippet with the exclamation mark i get a new value on every page refresh – right as i expect from a such a uncached call. So the problem i described above seems to be directly connected with the code in the snippet. But i can't find what's wrong.
            • 20413
            • 2,877 Posts
              @hawproductions | http://mrhaw.com/

              Infograph: MODX Advanced Install in 7 steps:
              http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

              Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
              http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
              • 3749
              • 24,544 Posts
              I think something like this would solve it (and be a little faster as well):
              (untested)

              [[!Countpageview]]


              [[+pagecount]]



              <?php
              $tvId = 12; // set to the ID of your TV 
              
              $tvr = $modx->getObject('modTemplateVarResource', 
                  array(
                     'tmplvarid' => $tvId,
                     'contentid' => $modx->resource->get('id'),
                  )
              );
              
              $pagecount = $tvr->get('value');
               
              //if tv empty yet or not numeric, set zo zero;
              if(empty($pagecount) || (!is_numeric($pagecount))){
                  $pagecount = 0;
              };
               
              //increase count
              $pagecount++;
               
              //set tv
              $tvr->set('value', $pagecount);
              if (!$tvr->save()) {
                  $modx->log(xPDO::LOG_LEVEL_ERROR, 'The page count could not be updated!');
              }
               
              $modx->setPlaceholder('pagecount', $pagecount);
              


              You may have to make the placeholder uncached, but I don't think so:

              [[!+pagecount]]


              Be sure the snippet tag is above the placeholder tag.



                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
                • 47898
                • 14 Posts
                @mrhaw
                Thank you for pointing out to the other forum thread concerning this issue – you are right, thats the problem. But is it a bug or a feature? I can't image it is a feature because you always want to get the actual value of a TV, not a cached old one. If it is a bug: Do you know if this issue is already filed on github? As working with TVs is – in my opinion - a fundamental feature of MODX this problem should be fixed.

                @BobRay
                Thanks for your solution – this workaround works very well and i replaced my old code with you solution. But anyway, i think using the getTVValue() and setTVValue() methods would be an even straight forward way to achieve the wanted function. By the way: What is the advantage of using an additional placeholder instead of just returning the value (if a snippet has only one return value)? I think it's a litte more convenient if i have only on MODX tag inside my template (for this particular function) as of two.
                Additionally, Bob, let me say i am currently reading you book and i love it! Even if english is not my native language i like the read and the way you write - thanks a lot for this great guide!
                  • 20413
                  • 2,877 Posts
                  Quote from: digitalbricks at Jun 07, 2014, 05:58 AM
                  @mrhaw
                  Thank you for pointing out to the other forum thread concerning this issue – you are right, thats the problem. But is it a bug or a feature? I can't image it is a feature because you always want to get the actual value of a TV, not a cached old one. If it is a bug: Do you know i

                  I'd go with Bob's code. I don't think it's a bug based on:

                  $modx->resource->getCacheKey()

                  -> https://forums.modx.com/thread/70679/how-to-remove-the-cache-file-for-just-one-resource?page=2#dis-post-397401
                  -> https://forums.modx.com/thread/69839/using-modx--cachemanager--refresh-with-custom-components#dis-post-391711
                  -> https://forums.modx.com/thread/15204/change-template-on-the-fly#dis-post-82222
                    @hawproductions | http://mrhaw.com/

                    Infograph: MODX Advanced Install in 7 steps:
                    http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

                    Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
                    http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
                    • 3749
                    • 24,544 Posts
                    @digitalbricks: I used the placeholder because I was sure it would work and it lets you put the page count tag in more than one place if you want to or in a chunk. It also lets you let the page count accrue without showing the value.

                    Returning the pagecount directly would actually be a little faster, though, if it works for you. Did you try it? And did the placeholder work when called without the exclamation point?

                    I'll probably do a blog post on this eventually.
                      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
                      • 47898
                      • 14 Posts
                      @mrhaw
                      Thanks a lot for the links. I read through the posts but still find this behavior of getTVValue() with returning cached values a litte bit strange. But okay, maybe i will see the reason for this later when using MODX for a longer while.

                      @BobRay
                      Thanks for the clarification! In my special case i don't need multiple output and i only count pages where the counter is also visible – so i just return the value instead of using an additional placeholder. But i have tested the version with a placeholder before and yes, i also works without the exclamation point. Only the snippet call needs to be prefixed with a exclamation point.

                      But i found a little issue with your code: On resources where this tv was assigned to later and not edited again yet (tv empty and never saved), the get() and the set() methods ends in a php fatal error:
                      Fatal error: Call to a member function set() on a non-object in PATH/core/cache/includes/elements/modsnippet/47.include.cache.php on line 30

                      So i modified your code to this:
                      <?php
                      $tvId = 23; // set to the ID of your TV 
                       
                      $tvr = $modx->getObject('modTemplateVarResource', 
                          array(
                             'tmplvarid' => $tvId,
                             'contentid' => $modx->resource->get('id'),
                          )
                      );
                      
                      /*    if tv is empty/not edited yet (existing resource where the tv was attached later)
                      *	$modx->getObject() don't create a propper object and the get() and set() methods will
                      *	cause a PHP fatal error. So we check if $tvr was created properly – if thats not the cause
                      *	we will use the "old" function for setting an initial value of "1".
                      */
                      
                      if($tvr){
                      	$pagecount = $tvr->get('value');
                      	//if tv empty yet or not numeric, set zo zero;
                      	if(empty($pagecount) || (!is_numeric($pagecount))){
                      		$pagecount = 0;
                      	};
                      
                      	//increase count
                      	$pagecount++;
                      	  
                      	//set tv
                      	$tvr->set('value', $pagecount);
                      	if (!$tvr->save()) {
                      		$modx->log(xPDO::LOG_LEVEL_ERROR, 'The page count could not be updated!');
                      	}
                      } else {
                      	//set initial count to 1
                      	$pagecount = 1;
                      	 
                      	//set tv
                      	if (!$modx->resource->setTVValue('PageCounter', $pagecount)) {
                      		$modx->log(xPDO::LOG_LEVEL_ERROR, 'The page count could not be updated!');
                      	}
                      }
                      
                      return $pagecount;


                      That works great.