We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6506
    • 35 Posts
    Hi Bob Ray, thanks for the reply, seems a little clearer and probably a lot easier than I thought. I actually want to do both, count the number of resources that have a particular TV and pull back the resources that have a particular TV but am concentrating on the first one first.

    Based on your code I have

    <?php
    $tvId = 2;
    $value = 'Dubai';
    $total = $modx->getCount('modTemplateVarResource', array('tmplvarid' => $tvId, 'value' => $value));
    


    Yet this doesn't seem to pull anything back. I am trying to display the number of items that have the TV value of Dubai (and eventually other countries so Dubai, the UK, then Australia etc). This value will appear on multiple resources throughout the document tree.

    My template variable ID is 2 and is a listbox of countries. A particular resource will belong to particular country, so in my example I have selected a resource with country of Dubai. Adding in the TV [[*country]] to the content of the page pulls back the correct value so I know that is working.

    Based on the code above I should be able to have

    <ul>
    <li>Dubai ([[GetTVCount]])</li>
    </ul>


    which will go through all resources in my document tree and pull back the number that have the TV value of Dubai. Am I missing something as if the snippet is correct it should work and i get no errors.

    Thanks again for your help much appreciated. [ed. note: DannyFranks last edited this post 13 years, 8 months ago.]
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Don't forget to return $total; or the snippet won't output anything.
        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
        • 6506
        • 35 Posts
        Hi Susan, thanks for that, I can now get it pulling back the count. Silly mistake on my part. Now having issues with the listbox defaulting back to the first in list after a change has been made but I can sort that. Thanks for all your help and Bob Ray.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          I spent a good half-hour trying to figure out why it wasn't working for me, until I noticed a missing ; after the tvId definition 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
            • 6506
            • 35 Posts
            Hi Susan, Bob Ray, sorry to come back to this. The code below works great

            <?php
            $tvId = 2;
            $value = 'Abu Dhabi';
            //$value = $modx->getObject('modTemplateVar','country');
            $total = $modx->getCount('modTemplateVarResource', array('tmplvarid' => $tvId, 'value' => $value));
            return  $total;


            This pulls back and counts all the resources that have a value of Abu Dhabi. However I have a getResources call on the homepage that pulls back the page title of each country and links to a page that is filtered by the country TV. What I can't do is get it to run through each value and add it to the homepage.

            So my getResources calls the chunk below

            <li>
            <a href="[[~[[+id]]]]">[[+pagetitle]] ([[getTVCount?]])</a>
            </li>
            


            This outputs a list of countries

            <ul>
              <li> <a href="countries/abu-dhabi.html">Abu Dhabi (8)</a> </li>
              <li> <a href="countries/africa.html">Africa (8)</a> </li>
            <li>.....etc
            </ul>


            But as you can see my snippet will only ever pull back the $value set . Is it possible to put them in an array match them to the page title and count. Hope either of you or someone can help. Thanks
              • 4172
              • 5,888 Posts
              try this:

              <?php
              $tvId = 2;
              $total = $modx->getCount('modTemplateVarResource', array('tmplvarid' => $tvId, 'value' => $value));
              return  $total;
              


              [[getTVCount? &value=`[[+pagetitle]]`]]
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 42562
                • 1,145 Posts
                I have been looking for something simple like this to replace my tag/category system.
                This wonderful snippet can really be reusable, for any TV or value, within or without getResources.
                I made the TvId an external variable too.
                <?php
                $total = $modx->getCount('modTemplateVarResource', array('tmplvarid' => $TvId, 'value' => $TvValue));
                return  $total;
                [[getTVCount? &TvId=`2` &TvValue=`[[+pagetitle]]`]]
                  TinymceWrapper: Complete back/frontend content solution.
                  Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
                  5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
                  • 3749
                  • 24,544 Posts
                  @Bruno17: Brilliant, as always! 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
                    • 6506
                    • 35 Posts
                    The both work great, thank you very much for all the help, much appreciated. A nice reusable snippet for others also I hope.