We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 25847
    • 62 Posts
    This is an auto-generated support/comment thread for Quote Generator.

    Use this forum to post any comments about this addition or any questions you have regarding its use.

    Brief Description:
    This snippet was designed to generate a random quote pulled from a flat file database.
      • 3749
      • 24,544 Posts
      Looks like a nice snippet. Thanks for your contribution.


      I see that the snippet call didn’t show up in your instructions in the repository, most likely because MODx thinks it’s a real snippet and can’t find it. Let me mention a little trick I learned from RThrash. Put an empty comment between the brackets of your snippet call to keep MODx from interpreting it as a snippet:

      [<!-- -->[Snippet call here ]<!-- -->] 

      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
        • 25847
        • 62 Posts
        Hey Bob. Thanks for the tip! I will be sure to keep that in mind in the future.
          • 29387
          • 40 Posts
          Nice snippet, thanks. Seems to work fine, except for the occasional blank line -- formatting items display fine, but only blanks are lifted from the database -- looks like this, when that happens:

          " " --


          instead of how it’s supposed to look:

          "Quote" -- Reference


          How can I fix that?
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Sounds like an array indexing error; arrays are indexed from 0 to count - 1. If you have six items, you can get items 0 through 5; if you use the count() function then try to use 6, you’ll get a blank since there is no item #6 in the array.
              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
              • 29387
              • 40 Posts
              Hmmm, the code seems to be okay in that regard. Early on it counts the number of quotes with:

              $numQuotes = count ($lines);

              and then it uses that result to select a random number with:

              if ($quoteType == 0)
              {
              $ranNum = rand(1, $numQuotes); // Quote changes on browser reload
              }

              and then outputs the quote and reference with

              $line = $lines[$ranNum-1];
              $element = explode("::",$line);
              $theQuote = $element[0];
              $theRef = $element[1];
              $output = outputLine($theQuote, $theRef);
                • 4749
                • 623 Posts
                @tcochran:
                I’m having the same issue. Did you have any luck resolving it?
                  The MODx has you...
                  Utah Web Design
                  • 29387
                  • 40 Posts
                  I changed the parameters on the rand function to the following:

                  $ranNum = rand(0, $numQuotes-1);

                  I’d seen an old comment online that the "rand() max value is always 1 greater than it should be." I don’t know if that’s true or not, but so far, it seems to be working okay.

                  Of course, the results ARE random, so it’s hard to say for sure. :~)
                    • 29387
                    • 40 Posts
                    Correction to earlier post: Change to code should be:

                    $ranNum = rand(1, $numQuotes) -1;
                      • 3749
                      • 24,544 Posts
                      If you change

                      $line = $lines[$ranNum-1];


                      to

                      $line = array_rand($lines);


                      I think it would be faster and you wouldn’t need $count or any random number.
                        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