We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 13740
    • 86 Posts
    I have 2 ditto calls in the same template which pull a single item at random from the same parent. I understand that sometimes they will duplicate, but it’s always duplicating. Is this a bug, or expected behaviour?

    Thanks,
    Krys
      • 16278
      • 928 Posts
      What do your Ditto calls look like? Do they have the &id parameter set?
      smiley KP
        • 13740
        • 86 Posts
        here it is:

        [!Ditto? &startID=`60` &tagData=`quotesfrom` &tagDelimiter=`,` &tagMode=`onlyTags` &tpl=`testimonial` &tags=[*quotesfrom*]  &total=`1` &randomize=`1` !] 


        Every time I refresh the page, I get different quotes, but always duplicated.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Since they both use the same startId (you really should use &parents instead) they’ll both be applying the same algorithm for selecting a random entry from the same array of results. Try changing the sort direction on one of them, then they’ll be starting from different array items. There is a reason why most of the programming algorithms for randomization are called "pseudo-random"!
            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
            • 16278
            • 928 Posts
            As an explicit way of distinguishing one call from the other, you could give each call an &id parameter, e.g &id=`first` and &id=`second`. This will encourage Ditto to create a separate results resource for each.

            I tried a little experiment with a simplified version of your Ditto call, and found that in fact any difference, even adding a dummy parameter like &fred to one of them, produced a difference in results. But &id would be the orthodox way.
            wink KP
              • 4172
              • 5,888 Posts
              if you want different outputs from multiple snippet-calls you need different parameters, that can also be dummy-parameters.

              why?
              see function evalSnippets in /manager/includes/document.parser.class.inc.php this line:

              $documentSource= str_replace("[[" . $snippetName . $currentSnippetParams . "]]", $executedSnippets[$i], $documentSource);
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 16278
                • 928 Posts
                And, of course, though you now have two Ditto calls, each with their own results, there’s no guarantee that the two "random" results won’t be the same...

                So what you could do with is a snippet that generates a list of valid document IDs in random order and feeds two of them to the Ditto calls using the &documents parameter instead of &startID or &parents, (e.g. by getting them into an array and using PHP’s array_rand or shuffle functions to set placeholders).
                grin KP
                  • 13740
                  • 86 Posts
                  Thanks for all the help - those changes fixed it!

                  Writing the suggested snippet is sadly beyond my skillset...