We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20407
    • 82 Posts
    Thought I’d share something useful I discovered with getResources. Not sure if this should be posted here or in the getResources forum; too bad you can’t cross-post or use tags...

    The site I’m working on has a bunch of testimonials or quotes from happy customers. I wanted the ability to show a random one in the sidebar. You can do something like this with the RandomChunk add-on, but it requires a comma-separated list of chunks. Instead I put each quote in a separate resource and found a way to select one at random for the sidebar and also be able to list them all on a ’Testimonials’ page.

    Since the sortby param is passed to the SQL you can use the MySQL ’RAND()’ function:
    [[!getResources? &parents=`27` &limit=`1` &tpl=`testimonial` &includeContent=`1` &sortby=`RAND()`]]

    The above will select a random resource that is a child of resource id 27. To list all the resources in random order, just change the limit to zero.
      • 36926
      • 701 Posts
      Quote from: MrFussyfont at Sep 30, 2010, 02:21 AM

      Thought I’d share something useful I discovered with getResources. Not sure if this should be posted here or in the getResources forum; too bad you can’t cross-post or use tags...

      The site I’m working on has a bunch of testimonials or quotes from happy customers. I wanted the ability to show a random one in the sidebar. You can do something like this with the RandomChunk add-on, but it requires a comma-separated list of chunks. Instead I put each quote in a separate resource and found a way to select one at random for the sidebar and also be able to list them all on a ’Testimonials’ page.

      Since the sortby param is passed to the SQL you can use the MySQL ’RAND()’ function:
      [[!getResources? &parents=`27` &limit=`1` &tpl=`testimonial` &includeContent=`1` &sortby=`RAND()`]]

      The above will select a random resource that is a child of resource id 27. To list all the resources in random order, just change the limit to zero.

      Nice tip, thanks
        • 19534
        • 183 Posts
        That’s a nice tip. Thank you!
          Add-On to easily manage your multilingual sites: Babel
          • 10474
          • 6 Posts
          great tips.. thanx
            • 7253
            • 97 Posts
            Neat, thanks. This should also work for Ditto, I think. Also, probably MySQL only, not that that’s relevant yet.
              • 34123
              • 103 Posts
              Excellent tips !!!

              Adopted !!
                Configuration Apache + Modx + MSSQL 2008
                ===============================
                Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.8
                OS : Windows 2008 R2
                SGBDR : Microsoft SQL Server 2008 (express)
                • 14349
                • 44 Posts
                Another fun thing to add.

                If you are calling rand() multiple times on the same page (Lets say you have to sidebars displaying randomly generated adds) to keep from showing the SAME randomly generated adds (rand() will return the same random number everytime the page is rendered - thus resulting in both of your random get resources calls returning the same item)

                SIMPLY add rand()+1 to the other getresource call

                • You could also get around this by adding a random property that is different in each call. MODX caches (even non-cacheable) output from Snippets that have the same tag signature on the same Resource. So these two calls would always return the output of the first call even though you are expected dynamic data to be produced by a randomization function:
                  [[!Snippet? &prop1=`rand()`]]
                  [[!Snippet? &prop1=`rand()`]]
                  

                  To get around this, simply add any property to the second call to make it's tag signature different.
                  [[!Snippet? &prop1=`rand()`]]
                  [[!Snippet? &prop1=`rand()` &instance=`2`]]
                  

                    • 36066
                    • 3 Posts
                    I'm trying to pull off the same thing MrFussyfont is doing. I'm finding the getResources and &sortby=`RAND()` to have pretty unexpected behavior. For example, these two calls in the same chunk always return the same resource:

                              [[!getResources? &parent=`2012` &tpl=`home_quote` &includeContent=`1` &sortby=`RAND()` &limit=`1` &instance=`2`]]
                              [[!getResources? &parent=`2012` &tpl=`home_quote` &includeContent=`1` &sortby=`RAND()` &limit=`1` &instance=`3`]]
                    


                    (I tried adding the 'instance' property as opengeek suggested but behavior didn't change)

                    Also, those calls above are always cached, ie: they pick one resource from the parent directory and render that on every page request until I clear the site's cache.

                    Anyone know what's going wrong in this case or where I could learn more about how getResources, &sortby=`RAND()`, and modX caching work together?

                    Thanks!
                    • Quote from: stcorbett at Oct 20, 2011, 04:15 PM

                      (I tried adding the 'instance' property as opengeek suggested but behavior didn't change)

                      Also, those calls above are always cached, ie: they pick one resource from the parent directory and render that on every page request until I clear the site's cache.

                      Anyone know what's going wrong in this case or where I could learn more about how getResources, &sortby=`RAND()`, and modX caching work together?

                      Thanks!
                      As described previously in this thread, if the tag signature is exactly the same as another tag on the same Resource, the value will come from the cache. The tag signature has to be unique.