We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28042 ☆ A M B ☆
    • 24,524 Posts
    Give it a try. Nothing is written in stone around here. Just make sure to test for corner conditions, like if there is only one entry.
      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
      • 87
      • 51 Posts
      I gave it a try but still no luck.
        • 19843
        • 13 Posts
        Works Great! I use it for displaying promo offers, company highlights, FAQ’s, testimonials, etc. It’s so easy to manage the random strings with them all displayed in their own document under the site tree menu.

        The only problem I had was where I placed the snippet in my template, it displayed [ ! random?quoteid=74 ! ] because I didn’t know to take the spaces out of the code.

        Thanks smiley

        ~Mandy
          • 3749
          • 24,544 Posts
          Quote from: 2wistd at Jan 21, 2008, 08:56 PM

          upon inspection...this line doesn’t seem right to me.

          $random = (rand(1, $limit) - 1);

          seems like it would need to be $random = (rand(1, $limit - 1));
          because the other line seems like it is subtracting the 1 from the whole statement (1, $limit), rather then $limit?

          Am I right in this assumption?

          I don’t think so. $limit should be the number of items. $limit<1 doesn’t change it, it’s just a test. If you have 4 items, $limit will be 4. rand(1,$limit) will return a number from 1-4 and subtracting 1 will give a number from 0-3 -- as it should. The test Sottwell suggests should confirm that.

          Another useful test might be to add:
          print_r($resource);


          It will be a little hard to read, but you can see if the first and last quotes are there and whether any blank ones are coming back.

          Bob

            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
            I always use
            echo "<pre>";
            print_r($resource);
            echo "</pre>";
            

            in fact I often add a function "checkArray($resource)" that contains those lines to anything I’m doing so all I need to do is call it with the name of whatever I want to display. That might make a nice utility function to add to the MODx api, actually.
              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
              • 11041
              • 82 Posts
              This may not be the right thread in which to post, and apologies in advance.

              I have seen a few of the random* snippets in the repository, however what I am looking for perhaps extends a little beyond what is currently available.

              I am wanting to randomly (or in some sort of order) display a set of sponsiored ad links. (Gif, jpg, png etc image linked to the persons site).
              Now I thought that random ad would do this, but when I read through the description it mentioned only linking to other documents within the modx system, which is off course not what I am wishing to achieve.

              Has anyone got any thoughts or ideas if this can be done and how? Perhaps it is being planned already?

              Regards,

              Tom.
                • 29076
                • 615 Posts
                How could I show content AND the image in an image TV connected to each document in the random folder?
                  I think, thererfor I am! But what I am, and why...?
                  • 3749
                  • 24,544 Posts
                  Quote from: goldeagle at Mar 01, 2008, 01:10 AM

                  This may not be the right thread in which to post, and apologies in advance.

                  I have seen a few of the random* snippets in the repository, however what I am looking for perhaps extends a little beyond what is currently available.

                  I am wanting to randomly (or in some sort of order) display a set of sponsiored ad links. (Gif, jpg, png etc image linked to the persons site).
                  Now I thought that random ad would do this, but when I read through the description it mentioned only linking to other documents within the modx system, which is off course not what I am wishing to achieve.

                  The snippet returns the content of a random document in the folder. If you put the URL of an ad in the content field of each document there, you can show them on your page by putting the snippet call in the img tag:

                  <img src="[!RandomQuotes? &quoteid=`123` !]" alt ="random advertisement">
                    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
                    • 3749
                    • 24,544 Posts
                    Quote from: Sylvaticus at Mar 16, 2009, 07:43 AM

                    How could I show content AND the image in an image TV connected to each document in the random folder?
                    You’d have to either create a tpl chunk for the snippet, or just hard-code both the image and text into the snippet code, something like this:

                    // RandomQuote
                    // Snippet to produce by sottwell.
                    // Posted by web_designer.
                    // Dated 25th Sepember 2006.
                    
                    $resourceparent = $quoteid;  // the folder that contains quote documents
                    $output = '';  // initialise the quote variable
                    $resource = $modx->getActiveChildren($resourceparent,'createdon', 'DESC', $fields='content');
                    $limit=count($resource);
                    if($limit<1) {
                       $output .= "No entries found.<br />";
                       return $output;
                    }
                    $random = (rand(1, $limit) - 1);
                    
                    $output .= '<img src="' . $resource[$random][['imageTVName'] . '">';
                    $output .= '<p>' . $resource[$random]['content'] . '</p>'; // get a random quote
                    return $output;


                    This assumes that the "image TV" contains the URL of the image, not the image itself.


                      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
                      • 3749
                      • 24,544 Posts
                      BTW,

                      I haven’t tested it, but I think array_rand() would be a better choice for selecting the random array member:

                      Replacing this:

                      $
                      random = (rand(1, $limit) - 1);

                      with

                      $random = array_rand($resource);


                      And, while I’m thinking about it, what is probably better all around is a snippet that would rotate through the ads instead of picking one at random. I think there is such a snippet but, if not, it should be simple to write.
                        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