We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17412
    • 270 Posts
    Hi,

    The following snippet allow you to display random text/images inside a document, useful for quotes, etc.
    I need to vary some column content for a homepage and this content needs to be searchable - which the RandomQuote snippet cannot effectively do as it would appear outside of the containing document in a blank template.

    The code required for RandomQuote seems fairly straightforward assuming you know your php and modx-isms which I’m currently noobing.

    I just wondered if some kind soul could help me out here with some code for a snippet that can cycle between a few specified chunks, eg:

    {{chunk1}} {{chunkII}} {{chunkIII}}

    I’m sure this would be a useful snippet for many other souls and t’is the season to be jolly n all that smiley

    Thanks.

    // 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;
      • 31037
      • 358 Posts
      EDIT: I read your post to fast, this is not exactly what you asked for, but I’ll leave it here anyway...

      I don’t know if there exists some snippet for this, but if not I guess you could do something like this in a snippet:

      <?php
      //Snippet randomChunk
      //Uncle68 
      
      $randomChunkName = isset($randomChunkName) ? $randomChunkName : 'randomChunk'; //Set name or use default
      $noOfChunks = isset($noOfChunks) ? $noOfChunks : '0'; //Must set a value
      
      echo '{{' . $randomChunkName . rand(1, ($noOfChunks)) . '}}';
      ?>


      You could call the snippet like [!randomChunkName? &noOfChunks=`5`!] which would show chunks named randomChunk1, randomChunk3 and so on.

      Or call it like [!randomChunk? &noOfChunks=`5` &randomChunkName=`MyChunk`!] which gives the name MyChunk2, myChunk3 etc...

      Haven’t tested it but should (could) work I think. And note, I’m a php newbie, I’m sure there are better ways of doing this.
        • 17412
        • 270 Posts
        Excellent! That’s exactly what I was looking for - bloody works!

        I’d vote that it goes into the repository.

        Cheers Uncle!!
          • 31037
          • 358 Posts
          Ok, so it worked! (I’m a bit surprised) wink

          I don’t think it’s enough "complicated" to make into a repository item, it’s just a quick fix. But I’m glad it helped you! laugh
            • 17412
            • 270 Posts
            it’s bloody useful though
              • 10666
              • 68 Posts
              This should really be in the repository. Even if it’s a quite simple snippet , nothing like it exists in the repository in the random category , and it MUCH MORE comfortable to use then the snippets that draw the random content from folders/files. I’ve searched for more than a half an hour for something like this and I think it’s quite useful .
              (I can read and edit php code without very big problems , however writing snippets/php scripts from scratch is another story.)
              Also , "simple" is quality that most often we do not appreciate , so ,please put this in the repository .
              Thank you and good luck! wink
                • 17282
                • 283 Posts
                this is one of the single most useful snippets ive ever come across.
                  Everytime you use Flash ... a puppy dies .....
                  R.G Taylor
                  • 25663 MODX Staff
                  • 12,272 Posts
                  It actually is quite simple and tremendously useful. You really should add it to the repository. smiley
                    Ryan Thrash, MODX Co-Founder
                    Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                    • 9564
                    • 13 Posts
                    Hi there,

                    I’ve used a little self-made snippet to insert the page-introtext (the summary of the current document). If theres no introtext given, an image will be randomly included. The image-names are given through a chunk.
                    I think it’s also possible to let this snippet display various quotations, or what ever.

                    Thats the snippet:
                    <?php
                    /**
                     * Snippet RandomChunkLines
                     *
                     * @author Thomas Heuer <[email protected]>
                     */
                    
                    if($modx->documentObject['introtext'])
                    {
                    	return $modx->documentObject['introtext'];
                    }
                    else
                    {
                    	/* used functions */
                    	global $stripQuotationMarksDefined;
                    	if(!isset($stripQuotationMarksDefined))
                    	{
                    		function stripQuotationMarks($str)
                    		{
                    			$first = substr($str, 0, 1);
                    			$last = substr($str, -1);
                    			if( ($first == '"') || ($first == "'") )
                    				$str = substr($str, 1);
                    			if( ($last == '"') || ($last == "'") )
                    				$str = substr($str, 0, -1);
                    			return $str;
                    		}
                    		$stripQuotationMarksDefined = true;
                    	}
                    	
                    	/* get name of used chunk */
                    	$randomChunk = (isset($randomChunk)) ? stripQuotationMarks($randomChunk) : null;
                    	
                    	/* get the lines of the cunk as an array */
                    	$output = '';
                    	if(is_null($randomChunk))
                    	{
                    		$output .= 'no chunk found' . "\r";
                    	}
                    	else
                    	{
                    		$chunkLines = explode("\n", $modx->getChunk($randomChunk));
                    		$imgData = array_map("trim", explode("|", $chunkLines[array_rand($chunkLines)]));
                    		$output .= '<img src="/assets/images/' . $imgData[0] . '" alt="' . $imgData[1] . '" />';
                    	}
                    	return $output;
                    }
                    ?>
                    


                    An example chunk: {{introtext_images}}
                    pic1.jpg | Alternative text 1.
                    pic2.jpg | Alternative text 2.
                    pic3.jpg | Alternative text 3.
                    


                    And here the insert (in my template):
                    	<div id="introtext_content">
                    		[!RandomChunkLines? &randomChunk='introtext_images'!]
                    	</div>
                    



                      • 36451
                      • 264 Posts
                      Late to the party, but this snippet by Uncle is very useful. Thank you.