We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28042 ☆ A M B ☆
    • 24,524 Posts
    Yes, but you'd only be looping through the desired results, not looping through all TV values looking for the ones you want. Big performance boost.
      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
      • 3749
      • 24,544 Posts
      If I'm understanding things, the original code is one query -- most likely faster. Adding the 'value' criterion means a new query for each potential value unless there's only one possible value (but less memory used at one time). Which one I'd use would depend on the number of resources and the number of possible values, (and whether speed or low memory usage was more important).

        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
        Yes, I could see fetching all of them if more than one value was going to be used. I'm presuming that the results is cached in some way, so that the same result set can be used repeatedly to get the various groups of values as needed.
          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
          • 38359
          • 93 Posts
          I'm trying to use exside's code but I can't get it to work, there seems to be a problem in this bit:

          if ( $tvval === $query ) {
                  // if the requested value is the same as the value of that tv, add it to the array
                  $resources[] += $tvresource->get('contentid');
              }


          Can anyone see the problem? Is contentid the correct name?

            • 3749
            • 24,544 Posts
            There were a couple of typos in the code (nice to see someone besides me doing that wink ). Try this:

            <?php
            $query = $_POST['region']; // The value submitted by the selectbox/form
            $resources = array(); // all resource ids that have the requested value
            $tvresources = $modx->getCollection('modTemplateVarResource', array('tmplvarid' => 36)); // find all resouces that have a value in the tv with id = 36
            foreach ($tvresources as $tvresource) {
                $tvval = $tvresource->get('value'); // get the value of the tv, needed for the following check
                if ($tvval === $query) {
                    // if the requested value is the same as the value of that tv, add it to the array
                    $resources[] += $tvresource->get('contentid');
                }
            }
            // return a comma separated list of resource ids for getResources
            return implode(',', $resources);
            [ed. note: BobRay last edited this post 13 years, 6 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
              • 38359
              • 93 Posts
              Thanks BobRay, appreciate you taking the time.
                • 38359
                • 93 Posts
                Thanks everyone, that works great.
                  • 40045
                  • 534 Posts
                  Big LOL and thanks @Bob...I really tried to find what's wrong, but didn't, I even didn't see it when you posted the correct code, had to move both code pieces into my editor to have a look at it and see, if I would have done it there (instead of the forum textarea) I would have seen it, probably =D, sometimes just blind...look here:

                      if ( $tvval === $query ) ) {


                  and here

                  return implode(',' $resources);


                  just one little closing parenthesis too much, a comma missing...equals parse error =D...man...learn: don't write code out of your head in a plain textarea^^
                    • 3749
                    • 24,544 Posts
                    LOL, almost every time write code outside of PhpStorm, it's wrong. I almost always forget a ) because I'm so used to PhpStorm doing them for me.

                      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