We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • I am making 2 getresources calls to generate galleries(li and img tags) that are interactive with each other. I would like to limit and randomize each of them, but need to make sure that the same 5 resources get returned in each one. Any ideas?
      • 3749
      • 24,544 Posts
      One way might be to write a snippet that produces a random number based on the number of images - 5 and writes that value to a TV.

      Call the snippet uncached once at the top of the page, then use the TV value in the &offset property of getResources. The images will be in order from the offset value -- I don't think you can randomize the 5, but they'll be taken from a random part of the table.

      Another way, if it would work for you, would be to use the &toPlaceholder property to have the results written to a placeholder, then use that placeholder in both places.
        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
      • Bob, thanks for the thought starter... I ended up putting my 2 getresources calls in a snippet and then passed a simple php variable with the 3 pre-generated random id numbers...

        Outer call from template:
        [[!randomauthorgenerator? &rset=`[[!getResources? &parents=`99` &limit=`3` &sortby=`rand()` &tpl=`@INLINE [[+id]],`]]`]]

        Inside the snippet:

        <?php
        echo '
        <div id="auther-slider-thumb" class="auther-slider-thumb">


        [[!getResources? &resources=`'.$rset.'` &limit=`3` &includeContent=`1` &includeTVs=`1` &processTVs=`1` &tvPrefix=`` &tpl=`AuthorImg`]]


        </div>
        <ul class="auther-slider">
        [[!getResources? &resources=`'.$rset.'` &limit=`3` &includeContent=`1` &includeTVs=`1` &processTVs=`1` &tvPrefix=`` &tpl=`AuthorDetail`]]

        </ul>
        ';
          • 3749
          • 24,544 Posts
          I'm glad you got it sorted.

          You might consider turning things around by getting the resource list inside the snippet without the overhead of another getResources call (untested).

          [[!randomauthorgenerator? &parents=`99`]]



           $c = $modx->newQuery('modResource');
           $c->limit(3);
           $c->where(array('parent' => $parents));
           $c->sortby('RAND()');
          
           $docs = $modx->getCollection('modResource', $c);
           $rsetArray = array();
           foreach($docs as $doc) {
                $rsetArray[] = $doc->get('id');
           }
           $rset = implode(',', $rsetArray());
          
           return '
          <div id="auther-slider-thumb" class="auther-slider-thumb">
          
          
          [[!getResources? &resources=`'.$rset.'` &includeContent=`1` &includeTVs=`1` &processTVs=`1` &tvPrefix=`` &tpl=`AuthorImg`]]
          
          
          </div>
          <ul class="auther-slider">
          [[!getResources? &resources=`'.$rset.'` &includeContent=`1` &includeTVs=`1` &processTVs=`1` &tvPrefix=`` &tpl=`AuthorDetail`]]
          
          </ul>
          ';
          
          
          

            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