We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32716
    • 9 Posts
    I hope someone can help me with this. I’ve searched all over and can’t find a solution.

    I have resources that contain a TV called Cost.

    Cost is numeric.

    Lets say i have 5 resources which contain the follwoing Costs 20, 30, 20, 45, 35

    I want to use getResources to show all resources that have tv.cost > 20 and < 45

    By my example that should return 2 resources (costs 30 and 35)

    All straight forward so far.

    I wanted to use &tvFilters, but that doesn’t let me use the greater-than operator

    Next i wanted to use &where, but &where doesn’t seem to accept TV’s.

    Has anyone got any ideas? Ideally using getResources, as i want to avoid the api if possible.

    Many thanks in advance
      • 9207 ☆ A M B ☆
      • 2,475 Posts
      It looks like this isn’t yet supported. Bummer... but it’s coming in a future release:
      http://modxcms.com/forums/index.php/topic,60025.0.html

      Those queries can be inefficient due to the table structure.
        • 3749
        • 24,544 Posts
        I could be wrong, but I don’t think getResources will do that at this point. If you can limit the resources to search in some way, it might not be to bad to do something like this:

        <?php
        /* This assumes that the resources are under a particular container with an id of 12 */
        
        $resources = $modx->getCollection('modResource',array('parent'=>12));
        
        foreach ($resources as $resource) {
        
            $cost = $resource->getTVValue('cost');
        
            $matches = array();
            if ($cost > 20  && cost < 45) {
                $matches[] = $resource;
            }
        }
        
        foreach($matches as $match) {
           $output .= $modx->getChunk('itemTpl', $match->toArray());
        }
        return $output;


        If you can create an array containing all the possible resource IDs (say with $ids = $modx->getChildIds()), it might be faster to get the TV object and check all the values with $cost = $tv->getValue(), adding the matches to an array of ids, then get the matching resource with getCollection() and display them with same loop used above.

          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
          • 23510
          • 168 Posts
          I used your code and was trying to figure out why it wasn't working.

          The line

          $matches = array();
          


          is not in a good place. Its resetting the array for each iteration of the foreach loop, leaving you with an empty, or at best, a one-element array.
            • 9207 ☆ A M B ☆
            • 2,475 Posts
            Looks like the $matches line should have gone right before the first loop... move it to line 5 in Bob's example methinks...