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

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

    Brief Description:
    A small clean snippet for displaying random text from a pre-defined folder
      Web Development, Web Hosting & Search Engine Marketing by:

      Vitalized | UK
      w. www.vitalized.co.uk

      Website Design | Search Engine Marketing (SEM) | UK MODx web hosting, secure, fast & 100% MODx compatible

      Vitalized | Australia
      w. www.vitalized-australia.com.au

      Website Design | Search Engine Marketing (SEM) | Australian MODx web hosting, secure, fast & 100% MODx compatible
      • 33149
      • 28 Posts
      I see I’m the only person to have problems with this snippet. That’s not a good sign.

      I created a new snippet called "ClientQuote".

      I pasted the following code into it.

      <?php
      // 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 .= $resource[$random]['content']; // get a random quote
      return $output;
      ?>


      I created a new container called "Random Quotes". I tried it both published and non published.

      Here are the setting for the container (ID is #5)-
      Page data
      General
      Title: Random Quotes
      Long title: (Not set)
      Description: (Not set)
      Type: Document
      Document’s alias: (Not set)
      Keywords: (Not set)

      Changes
      Created: 13/01/07 21:49:54 (Admin)
      Edited: 15/01/07 09:42:11 (Admin)

      Status
      Status: Published
      Publish date: (Not set)
      Un-publish date: (Not set)
      Cacheable: No
      Searchable: No
      Menu index: 4
      Show in menu: No
      Web access: Public
      Manager access: Public

      Markup/structure
      Uses template:
      Edit using rich text editor: Yes
      Document is container: Yes

      I then put it in my template header2 chunk like this -
      <div id="head2">
           <div class="head2left">
           </div>
           <div class="head2right">
      <h2>Welcome to</h2>
      <h3><a href="[~[(site_start)]~]">[(site_name)]</a></h3>
      <p>
      [[!ClientQuote?quoteid=`5`!]]
      </p>
           </div>
      </div>
      


      I created 2 quote documents (tree as follows)-
      Random Thoughts
      Home (1)
      Random Quotes (5)
      Catie Curtis - Silhouette (7)
      Default - Movin On (6)
      Photos (2)
      Music (3)
      Forum (4)

      Can you tell what I did wrong?

      The quotes are not displaying.
        • 2954
        • 40 Posts
        I also can’t get this to work. Any help?
          • 31654
          • 238 Posts
          Make sure the call looks like this:

          [!ClientQuote? &quoteid=123!]


          You don’t need `` on the doc call. Also make sure the folder is published (but "show in menu" is unticked)
            Web Development, Web Hosting & Search Engine Marketing by:

            Vitalized | UK
            w. www.vitalized.co.uk

            Website Design | Search Engine Marketing (SEM) | UK MODx web hosting, secure, fast & 100% MODx compatible

            Vitalized | Australia
            w. www.vitalized-australia.com.au

            Website Design | Search Engine Marketing (SEM) | Australian MODx web hosting, secure, fast & 100% MODx compatible
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            [[!ClientQuote?quoteid=`5`!]]

            You’ve got redundant brackets there. It should be
            [[ClientQuote? &quoteid=`5`]]

            or
            [!ClientQuote? &quoteid=`5`!]

            While it’s true that the backticks `` aren’t necessary in this case, it’s a good habit to get into for cases when it does become necessary (options with spaces in them, for example)
              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
              This works for me but I notice that sometimes I get no result from my quotes. It just shows up blank. I currently have 4 quotes linked up and about 30% of the time it shows nothing.
              Any suggestions on why this is?

              Thank you
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                Arrays count starting from 0, so the first item would be $item[0], and if you had five items the last item would be $item[4]. When you base something on a count of the items (in this case $count=5), then use that count value, you have to subtract one from it, otherwise you’ll be trying to access $item[5] when there isn’t one.
                  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 dont see an array in the snippet code anywhere, and no where in the call do I actually say how many quotes I have.
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    $resourceparent = $quoteid;  // the folder that contains quote documents
                    $output = '';  // initialise the quote variable
                    $resource = $modx->getActiveChildren($resourceparent,'createdon', 'DESC', $fields='content'); // get array of children
                    $limit=count($resource); // count how many in the array
                    if($limit<1) {
                       $output .= "No entries found.<br />";
                       return $output;
                    }
                    $random = (rand(1, $limit) - 1); // make a random number between 1 and $limit, subtract 1 to allow for array counting from 0 not 1
                    $output .= $resource[$random]['content']; // grab a random document's content from the $resource array
                    return $output;

                    Try putting "echo $random" right after the line that defines it, then reload your page a few times to see what it’s doing. It should vary between 0 and 1 less than the number of documents you have.
                      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
                      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?