We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37828
    • 11 Posts
    Oh yeah BobRay.
    I am desperate now because all this scripts are not solving my problem...
    ... for me this thing still remain a nightmare and for you is probably just one another challange. smiley [ed. note: rebuut last edited this post 14 years, 6 months ago.]
      • 3749
      • 24,544 Posts
      Sorry rebuut, I thought we'd solved it, but please don't double post.


      What tag are you using to display the content on the page?

      It should be:

      [[!RandomChild? &folder=`12`]]


      Where RandomChild is the name you've given the snippet and the folder is a resource that actually has children.



      ------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using.
      MODX info for everyone: http://bobsguides.com/modx.html
        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
        • 37828
        • 11 Posts
        Hello Bob,
        thanks again your interest for helping me.
        My modx version: MODX Revolution 2.2.0-pl2 (traditional)
        My snippet name:
        ClientQuote
        and the call is:
        [[!ClientQuote? &folder=`10`]]
        a partially working snippet code is:
        $folder = isset($folder)? $folder:0;
        $criteria = $modx->newQuery('modResource');
        $criteria->where(array(
        'deleted' => 0,
        'published' => 1,
        'parent' => $folder,
        ));
        $criteria->sortby('RAND()');
        $child = $modx->getObject('modResource', $criteria);
        $index = rand(0, count($messages)-1);
        $output = $messages[$index]['content'];
        return !empty($child) ? $child->id : x;
        When I use this script i have a randomly displayed resurce ID-s.
        In my case:
        I have a Container Resource ID: 10 with 5 childs numbered: ID 11-12-13-14-15.
        The result displayed on page with the snippet call is only the id-s of child pages(of course) without any content...
        ...but I need the contents and in case of invalid target container have"x"
        [ed. note: rebuut last edited this post 14 years, 6 months ago.]
          • 3749
          • 24,544 Posts
          As I said above, you're setting the $output variable in line 11 above, but you're not returning it, so it has no effect (and the value is incorrect). In line 12, your're returning the ID, so that's what you're seeing.

          Replace the last two lines with this one line:

          $return !empty($child) ? $child->getContent() : 'No Child found');


          So the whole snippet will look like this:

          <?php
          $folder = isset($folder)? $folder:0;
          $criteria = $modx->newQuery('modResource');
          $criteria->where(array(
              'deleted' => 0,
              'published' => 1,
              'parent' => $folder,
          ));
          $criteria->limit(1);
          $criteria->select($modx->getSelectColumns('modResource','modResource','','',array('id','content')));
          $criteria->sortby('RAND()');
          $child = $modx->getObject('modResource', $criteria);
          $index = rand(0, count($messages)-1);
          return !empty($child) ? $child->getContent() : 'No Child found';



          I added the limit() and select() lines, which should speed things up. Comment them out if it doesn't work.


          ------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using.
          MODX info for everyone: http://bobsguides.com/modx.html [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
            • 37828
            • 11 Posts
            Thanks Bob, the result is same:
            HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request...
              • 3749
              • 24,544 Posts
              Did you try commenting out the limit() and select() lines?


              ------------------------------------------------------------------------------------------
              PLEASE, PLEASE specify the version of MODX you are using.
              MODX info for everyone: http://bobsguides.com/modx.html
                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
                • 37828
                • 11 Posts
                THX Bob, i managed this problem in another way. I return to 2.0.8. .
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  This one actually works:
                  <?php
                  $folder = isset($folder)? $folder:0;
                  $criteria = $modx->newQuery('modResource');
                  $criteria->where(array(
                      'deleted' => 0,
                      'published' => 1,
                      'parent' => $folder,
                  ));
                  $criteria->limit(1);
                  $criteria->select($modx->getSelectColumns('modResource','modResource','','',array('id','content')));
                  $criteria->sortby('RAND()');
                  $child = $modx->getObject('modResource', $criteria);
                  $index = rand(0, count($messages)-1);
                  return (!empty($child)) ? $child->getContent() : 'No Child found';
                    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
                    I can't see any difference between that and mine above. What's your secret?
                      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
                      The last line - yours
                      $return !empty($child) ? $child->getContent() : 'No Child found');

                      The last line - mine (the parentheses around the conditional is actually irrelevant)
                      return (!empty($child)) ? $child->getContent() : 'No Child found';

                      Figured out from reading my PHP error log when the snippet was white-screening my whole page. First reported an unexpected !, so I noticed and removed the $ in the return keyword, then it reported an unexpected ), which I removed at the end of the line.
                        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