We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • Try using mt_rand() instead of rand().
      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
      • 4172
      • 5,888 Posts
      you can try to give each snippet-call an unique property in the snippet-tag:

      [[!mysnippet? &unique=`1`]]
      [[!mysnippet? &unique=`2`]]
      [[!mysnippet? &unique=`3`]]
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
      • Does it always return the same values, or just sometimes or even most of the time? I've frequently used similar code and get the same values several times especially if there aren't many values to choose between.

        The rand() function is not a true random number generator, it's what is called a pseudo-random number generator. It is "seeded" with a mathematically calculated value based on a timestamp (time()). So it's possible that if two calls to rand() are done closely enough together, they would have the same timestamp, and thus the same seed, generating the same pseudo-random number.

        It is possible to seed rand() yourself with the srand() function used before the rand() function, although many installations with suhosin block that function.

        Some interesting comments in this PHP doc page http://php.net/manual/en/function.srand.php
          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
        • Each tag must have a unique signature (name + properties), as indicated by @Bruno17, or all instances of the same tag will get replaced with the same output.
          • Correct, you can add unused properties to the tag to make the tag signature unique.
              • 42892
              • 44 Posts
              @opengeek how can i prevent my snippets to get the same signature without giving them individual parameters?

              I would like to give some html elements unique class names like
              <div class="container element-5d7a8d6f">...</div>
              and in the chunk it would look somethinig like this
              <div class="container [[!getUniqueElementId? &prefix=`element`]]">...</div>
              .

              Thanks smiley
              • Unfortunately, you need something unique in the snippet call to make it not match signature and use the already produced content. This is so tags can be placed in multiple places on the page without triggering redundant processing. In your case, you want it to be processed uniquely every time, and thus the signature of the tag cannot match. You can add an arbitrary unique property to the call, e.g.
                [[!getUniqueElementId? &prefix=`element` &i=`1`]]
                [[!getUniqueElementId? &prefix=`element` &i=`2`]]
                  • 42892
                  • 44 Posts
                  hmm... I got that, but isn't the ! exactly for these needs? So they are not cached ... or am I mixing up two different things here?

                  Also I took a look at the caching documentations. But I could not find out so far if using the cache manager would solve my problem. Do you know if this would.
                  • The parser replaces all instances of the same tag on a page after it returns from processing said tag the first time and records that content under the tag signature in the elementCache that is not stored with the Resource cache item. As a result, any additional occurrences of the same tag are replaced with the already processed content from that same request. The `!` simply prevents the tag results from being stored in the output of the Resource in the Resource cache which would prevent the tag from being executed on future requests.

                    This was implemented on purpose to prevent redundant processing of identical tags. In hindsight, I'm still unsure if it is a positive or negative feature and it would be nice if a future release had a configuration switch to prevent this, but I have not looked into the feasibility of doing this.
                      • 42892
                      • 44 Posts
                      Thanks for the explanation Jason. It made things more clear for me.

                      I agree that a simple setting or maybe another element prefix that indicates that the user wants to create a new signature for the element would be a nice thing ^^

                      And thanks a lot for the quick replies! wink