We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    Quote from: Sylvaticus at Mar 17, 2009, 08:36 AM
    $resource = $modx->getAllChildren($resourceparent,'createdon', 'DESC', $fields='pagetitle,content');
    


    Not sure if this will work, but try changing that to
    $resource = $modx->getAllChildren($resourceparent,'createdon', 'DESC', $fields='pagetitle,content,qouteimage');


    If that doesn’t work (and I don’t think it will), you’ll have to use something like this: http://wiki.modxcms.com/index.php/API:getTemplateVarOutput

    $modx->getTemplateVarOutput(array("qouteimage"),$resource,0);




    BTW, you have quoteimage misspelled (qouteimage), which is OK as long as you misspelled it the same way everywhere (including the name of your TV).  wink
      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
      • 407
      • 10 Posts
      Thinks its to do with freindly urls as if i use the documetn alias it bering in content form pages instead of the documents called 0,1,2,3.

      Thanks for all your help, any other ideas how to get this to work?
        • 407
        • 10 Posts
        Got it working, thanks for all yoiur help! grin
          • 3749
          • 24,544 Posts
          Quote from: adamchivers at Mar 18, 2009, 06:56 AM

          Got it working, thanks for all yoiur help! grin

          Can you tell us your solution so others can see it?
            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
            • 11681
            • 98 Posts
            This post replies to BobRay’s request to publish a working RandomQuotes solution.

            First off, I renamed it in the singular, as "RandomQuote," because it returns just one quotation at a time. Do as you like, but this name seems to me more appropriate semantically.

            Second, I took BobRay’s advice (See above post.) to pick a random selection from the Resources rather than explicitly calculate a random index into them. The Snippet works either way, but I prefer BobRay’s semantics.

            Third, I also took BobRay’s advice to replace "getActiveChildren" with "getAllChildren." The former results in the Snippet returning "No entries found" if the child Resources are not checked as "published."

            So here is the resulting Snippet code:
            <?php
            // RandomQuote
            // Pulls one quotation at random from Resources in a Folder.
            // Original by sottwell.
            // Posted by web_designer.
            // Dated 25th Sepember 2006.
            // Modified 28th February 2010 by Halfnium.
            
            $resourceparent = $quoteid;  // the folder that contains quote documents
            $output = '';  // initialise the quote variable
            $resource = $modx->getAllChildren($resourceparent,'createdon', 'DESC', $fields='content');
            $limit=count($resource);
            if($limit<1) {
               $output .= "No entries found.<br />";
               return $output;
            }
            $random = array_rand($resource);
            $output .= $resource[$random]['content']; // get a random quote
            return $output;
            ?>
            

            Use: As the Snippet’s original author advised (http://modxcms.com/extras/package/368),

            * Create a new Snippet. Name it RandomQuote. Past the code above into it.

            * Create a new Folder to serve as parent for the quotation Resources. Non-check the "Show in menu" box.

            * Create any number of quotation Resources as children of the above Folder. Settings for these:

            • Template: Blank
            • Show in menu: Non-checked
            • Log visits: Non-checked
            • Searchable: Non-checked
            • Cacheable: Non-checked
            • Published: See TIP below.
            • Others as you please, and there is no need to provide names etc.
            * Call RandomQuote in your Page in uncached form, as
            [!RandomQuote? &quoteid=`123`!]

            where "123" means the ID of the Folder containing the quotation Resources.

            TIP: Verifying the format of a quotation newly added to several already in the &quoteid Folder is a pain, since it can take many clicks of the browser’s reload button to bring the new Resource around in the random rotation. In that case, temporarily modify RandomQuote by substituting "getActiveChildren" for "getAllChildren." (As mentioned above, this results in non-published quotations no longer being found.) Create a new quotation and check it off as "published." Upon rendering your Page, only that new quotation becomes available, allowing you to see it immediately.

            Once you have verified the new quotation’s format, make it disappear by non-checking it as "published."

            Once you’ve added all your quotation Resources to the &quoteid folder, switch the method back to "getAllChildren."
              I looked just like that in 1964.
              • 26931
              • 2,314 Posts
              halfnium, thanks for sharing!
                • 3749
                • 24,544 Posts
                Thanks for reporting back.
                Your code is very solid and readable and I’m definitely not suggesting you change it, but I got to thinking how much it could be compressed. I think this will work, but haven’t tested it:

                <?php
                $resource = $modx->getAllChildren($quoteid,'createdon', 'DESC', $fields='content');
                empty(count($resource))? $return "No entries found.<br />" : return $resource[array_rand($resource)]['content'];
                ?>
                


                But seriously, one change you might actually consider is to add a parameter: &publishedOnly, since some people might want to be able to take individual quotes out of action by unpublishing docs.

                $publishedOnly = isset($publishedOnly)? $publishedOnly : false; // have $publishedOnly default to false
                if ($publishedOnly) {
                   $resource = $modx->getActiveChildren($quoteid,'createdon', 'DESC', $fields='content');
                } else {
                   $resource = $modx->getAllChildren($quoteid,'createdon', 'DESC', $fields='content');
                }


                Of course there are always multiple ways to do things in MODx and resources have a lot of overhead that you don’t really need here, so two other ways to go come to mind, especially if you have a lot of quotes:

                1. Put all the quotes in *one* chunk or resource, get them with getChunk or getField, put them into an array with explode() and then select one with array_rand(). The chunk or resource would be cached so the performance should be acceptable (compared to retrieving all children and then retrieving a resource). EZfaq works that way.

                2. Put the quotes in a custom DB table where you can pull one at random with a query. This has the advantage of making the quotes searchable in a variety of ways. You can see that in action here: http://wordsmatter.softville.com/quotes.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
                  • 11681
                  • 98 Posts
                  Good ideas!

                  RandomQuotes was not my code in the first place. I was just trying to get it to work for me. (Since the original post dated back to 2006, I had my doubts.) It was enough of a struggle that I thought I would document a solution for others’ use.
                  <rant>
                  The way I figure it, in the long run, don't expect to find useful stuff or get your
                  questions answered if you don't give your solutions back.
                  </rant>

                  -- Halfnium
                    I looked just like that in 1964.