We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19889
    • 616 Posts
    Hello

    I was wondering whether there is a snippet that would allow me to randomly show a quote on a page.

    Preferably, I’d like to have all the quotes in one chunk like:

    quote 1
    quote 2
    quote 3
    quote 4

    etc.

    I think this is the easiest way to maintain it.

    thank you in advance for pointing me to the right direction.
      • 3749
      • 24,544 Posts
      I know there’s a random quotes snippet on the forums, but I can’t find it.

      If you separate the quotes with a delimiter ( e.g. #) all you need is something like this:

      $quotes = $modx->getChunk('quotes);
      
      $quoteArray = explode('#',$quotes);
      
      return (array_rand($quoteArray));
        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
        • 19889
        • 616 Posts
        Thanks - really appreciate it

        found a snippet that keeps the quotes in an external file that offers some features on how often to change the quote and stuff like that - basically nice but I still rather have the quotes in modx
          • 19889
          • 616 Posts
          Quote from: BobRay at Apr 01, 2009, 10:52 PM

          I know there’s a random quotes snippet on the forums, but I can’t find it.

          If you separate the quotes with a delimiter ( e.g. #) all you need is something like this:

          $quotes = $modx->getChunk('quotes);
          
          $quoteArray = explode('#',$quotes);
          
          return (array_rand($quoteArray));


          I’m getting a parsing error with the following content in my chunk:
          Quote 1#
          Quote 2#
          Quote 3#
          Quote 4


          what am I missing?
            • 3749
            • 24,544 Posts
            Sorry, typo on my part---I left out a quote mark:

            $quotes = $modx->getChunk('quotes);
            should be
            $quotes = $modx->getChunk('quotes');
              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
              • 19889
              • 616 Posts
              figured it out but that just returns an index number an not the text - just reading the array_rand on php.net but I didn’t figure it out yet how to return the text.

              got it:

              $quotes = $modx->getChunk('Quotes');
              $quoteArray = explode('#',$quotes);
              
              $rand_keys = array_rand($quoteArray);
              return $quoteArray[$rand_keys];


              thanks again
                • 3749
                • 24,544 Posts
                Sorry, I forgot that array_rand() returns a key to the associative array.

                Glad you got it sorted. smiley
                  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
                  • 34162
                  • 1 Posts
                  Quote from: BobRay at Apr 01, 2009, 10:52 PM

                  I know there’s a random quotes snippet on the forums, but I can’t find it.
                  If you still need it, the RandomQuotes snippet can be found here
                    • 30552
                    • 75 Posts
                    Actually, for having all the quotes in a single document (as opposed to multiple documents with one quote each, which can get SUPER tedious), I’ve found the Random Paragraph snippet (http://modxcms.com/extras.html?view=package/view&repository=10&package=360) to be just perfect.

                    Just have each quote in a paragraph tag on your quotes page, and the snippet will pull a random one. No need for 300 different pages if you have 300 different quotes then smiley