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,

    I’m writing a snippet that should output chunks randomly. These chunks contain snippet calls.

    This is the current state of the snippet [[randomVoicebox]]:

    <?php
    $voice = array('{{voicebox-1}}','{{voicebox-2}}','{{voicebox-3}}');
    $rand = rand(0, (count($voice)-1));  // Generate a random number between zero and the number of chunks minus one
    $voice = $modx->evalSnippets($voice); // Parse snippet tags in string
    $output = $voice[$rand]; // Output the random chunk
    return $output;
    ?>


    This is the Modx error I get:
    PHP error debug
    Error: preg_match_all() expects parameter 2 to be string, array given
    Error type/ Nr.: Warning - 2
    File: /var/www/vhosts/blah.co.uk/subdomains/indev/httpdocs/manager/includes/document.parser.class.inc.php
    Line: 786
    Line 786 source: preg_match_all(’~\[\[(.*?)\]\]~ms’, $documentSource, $matches);

    This is an example of one of the chunks (voicebox-1):

    [!getField? &docid=`1` &field=`voicebox-photo`!]
    [!getField? &docid=`1` &field=`voicebox-name`!]
    
    Using the: getField snippet here: http://modxcms.com/extras/package/?package=173 
    


    This is my snippet call in the template:

    [[randomVoicebox]]


    Modx version: 1.0.2

    Can anyone help out? Thanks.
      • 18913
      • 654 Posts
      My WAG is that this
      $voice = $modx->evalSnippets($voice); 

      should be something like
      $voice = $modx->evalSnippets($voice[$rand]); 


      Just a guess ...
      MattC
        • 17412
        • 270 Posts
        Thanks Matt.

        I’ve got it working like this:

        Snippet: (change: removed the evalSnippets function altogether)

        <?php
        $voice = array('{{voicebox-1}}','{{voicebox-2}}','{{voicebox-3}}');
        $rand = rand(0, (count($voice)-1));  /* generate a random number between zero and the number of chunks minus one */
        $output = $voice[$rand]; // output the random chunk
        return $output;
        ?>


        Chunk: (change: call getField snippet cached)

        [[getField? &docid=`1` &field=`voicebox-photo`]]
        [[getField? &docid=`1` &field=`voicebox-name`]]


        Snippet call: (change: call uncached)

        [!randomVoicebox!]
          • 3749
          • 24,544 Posts
          Try this for the snippet:

          <?php
          $voice = array('{{voicebox-1}}','{{voicebox-2}}','{{voicebox-3}}');
          return array_rand($voice);
          ?>


          It will be a little faster, and you can add members to the $voice array without rewriting the code.

            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
            • 17412
            • 270 Posts
            Nice one BobRay - thanks!
              • 32095
              • 15 Posts
              Okay, I’m using this snippet:

              <?php
              $voice = array('{{voicebox-1}}','{{voicebox-2}}','{{voicebox-3}}');
              return array_rand($voice);
              ?>


              The chunks: voicebox-1, voicebox-2 and voicebox-3 contain html, but the output I’m seeing in the page are intergers 0, 1 and 2.

              Any suggestions as to where I’ve gone wrong?

              Thank you,

              Rich
                http://www.websitedesignbywebd.co.uk"" target="_blank" rel="nofollow">North Wales Website Designer
                http://www.websitedesignbywebd.co.uk/ecommerce-website-design.html"" target="_blank" rel="nofollow">Ecommerce Website Design
                http://www.websitedesignbywebd.co.uk/cms-website-design.html"" target="_blank" rel="nofollow">CMS Website Design
                [URL=http://"http://blog.websitedesignbywebd.co.uk/"]webD&#39;s Blog: Website design, SEO and other ramblings
                • 4172
                • 5,888 Posts
                I think you have to use it like here:

                <?php
                $voice = array('{{voicebox-1}}','{{voicebox-2}}','{{voicebox-3}}');
                $randomkey=array_rand($voice);
                return $voice[$randomkey];
                ?>


                or something like that
                <?php
                $max=3;
                $chunkprefix='voicebox-';
                return '{{'.$chunkprefix.rand(1,$max).'}}';
                ?>
                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                  • 32095
                  • 15 Posts
                  Quote from: Bruno17 at Feb 01, 2010, 06:42 AM

                  I think you have to use it like here:

                  <?php
                  $voice = array('{{voicebox-1}}','{{voicebox-2}}','{{voicebox-3}}');
                  $randomkey=array_rand($voice);
                  return $voice[$randomkey];
                  ?>


                  Perfect.

                  Thank you so much to everyne that helped.

                  Rich
                    http://www.websitedesignbywebd.co.uk"" target="_blank" rel="nofollow">North Wales Website Designer
                    http://www.websitedesignbywebd.co.uk/ecommerce-website-design.html"" target="_blank" rel="nofollow">Ecommerce Website Design
                    http://www.websitedesignbywebd.co.uk/cms-website-design.html"" target="_blank" rel="nofollow">CMS Website Design
                    [URL=http://"http://blog.websitedesignbywebd.co.uk/"]webD&#39;s Blog: Website design, SEO and other ramblings
                    • 3749
                    • 24,544 Posts
                    Sorry, I was asleep at the switch. The array_rand() function does return the *key* of a random item. Thanks for the correction.
                      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