We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36926
    • 701 Posts
    Hi Any suggestions on the best way on how I could randomly pick a resource ID every day to be used with say getResources or getResourceField.

    For example. I have 1-50 resource IDs.
    So one day resource id=30 is picked and used for every user for the whole day.
    [[getResourceField? &id=`30`]]
    


    Then the next day an new random resource ID is set, let say 15 then the getResoruceField call would be:
    [[getResourceField? &id=`15`]]
    


    I'm not that worried if by chance the same ID appears in a row, but it needs to stay the same for every user for the whole day.

      • 38723
      • 165 Posts
      You could use the RAND() sortby option on getResources, like so:
      [[getResources? &parents=`CONTAINER ID` &limit=`1` &sortby=`RAND()` &tpl=`CHUNKNAME`]]


      Which should return one resource from under a particular parent container page at random from all the results possible.

      MODX RTFM docs for getResources:
      http://rtfm.modx.com/display/ADDON/getResources
        • 36926
        • 701 Posts
        Hi thanks for the reply.

        The problems not so much getting a random resource, it's getting this on a daily basis and for the same ID to be used throughout the day. Then the next day another another random ID. Thinking possibly cronManager maybe an option, but wondering if anyone else has implemented something similar.



          • 25663 MODX Staff
          • 12,272 Posts
          You could also do this with caching as well using the getCache snippet as Jason outlines here. That way you get the best of randomization and of caching.

          http://modx.com/blog/2012/10/29/optimization-with-getcache-and-custom-cache-partitions/
            Ryan Thrash, MODX Co-Founder
            Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
            • 38723
            • 165 Posts
            Quote from: rethrash at Jan 11, 2013, 12:16 PM
            http://modx.com/blog/2012/10/29/optimization-with-getcache-and-custom-cache-partitions/

            This looks really useful. And because it offers &element=`getResources` I guess that means you can pass ANY of getResources' options to it, like sortby, sortdir etc?
              • 18373 ☆ A M B ☆
              • 3,141 Posts
              Quote from: pdincubus at Jan 11, 2013, 12:39 PM
              Quote from: rethrash at Jan 11, 2013, 12:16 PM
              http://modx.com/blog/2012/10/29/optimization-with-getcache-and-custom-cache-partitions/

              This looks really useful. And because it offers &element=`getResources` I guess that means you can pass ANY of getResources' options to it, like sortby, sortdir etc?
              Yes.

              This a snippet I use on my website for a simple "read a random article" link:

              $ids = array();
              $ids = $modx->getChildIDs(6); //6 is the ID of my blog category
              $c = $modx->newQuery('modResource');
              $c->select(array('id'));
              $c->where(array('isfolder' => false, 'published' => 1, 'deleted' => 0, 'id:IN' => $ids));
              
              $c->limit(1);
              $c->sortby('RAND()');
              foreach ($modx->getIterator('modResource',$c) as $res) {
                return $modx->makeUrl($res->get('id'));
              }


                Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                • 4172
                • 5,888 Posts
                perhaps this can give you an idea, how to get something on a dayli basis, here it is a dayli MIGX-record:

                https://forums.modx.com/thread/80966/daily-images?page=2#dis-post-446678
                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                  • 36926
                  • 701 Posts
                  Hey Guys,

                  Thanks for the replies, really useful info here. The getCache snippet looks like the a great option. Going to give that a go on Monday.

                  Thanks again for the suggestions.

                  B
                    • 36926
                    • 701 Posts
                    Ok finally set some time aside to have a play with getCache, thinking it was gonna take some time as decided to use with custom snippet. 5mins and it's doing exactly what I want.

                    Thanks again for the suggestions.
                      • 25663 MODX Staff
                      • 12,272 Posts
                      So did you use getCache, or did you write a custom snippet (not totally clear and sorry for my confusion :/ )?
                        Ryan Thrash, MODX Co-Founder
                        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me