We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34162
    • 1 Posts
    I have a "folder" wich cantain many documents.
    In home page I must show a random document. This random document must be one oh the last 10 inserted.
    I try many ditto call... But When i user &randomize ditto randomize from all documents in the folder...
    help me please.
      • 7231
      • 4,205 Posts
      You need to include &total=`10` to limit the number of document returned to 10 (the display param only limits the number displayed, so by using them together you can display 1 but return 10). Also in order to get the most recent you need to sortBy pub_date (I think that is the correct spelling) and have DESC and the sotrDir then randomize turned on.

      In other words add this to your Ditto call:
      &display=`1` &total=`10` &sortBy=`pub_date` &sortDir=`DESC` &randomize=`1`

      This is not tested but I think it should work, let us know what happens grin
        [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

        Something is happening here, but you don't know what it is.
        Do you, Mr. Jones? - [bob dylan]
        • 34162
        • 1 Posts
        Thank you...

        This is the ditto call:

        [!Ditto? &parents=`53` &display=`1` &total=`10` &sortBy=`pub_date` &sortDir=`DESC` &randomize=`1` &debug=`1`!]

        And this is the debug...

        Basic Info
        Version 2.0.2
        Summarize 1
        Total 5
        Sort By RAND()
        Sort Direction DESC
        Start 0
        Stop 1
        IDs 3868, 3871, 3876, 3890, 3906, 3912, 3917, 3920, 3922, 3924, 3928, 4097, 4106, 4107, 4132, 4135,
        4136, 4137, 4138, 4139, 4140, 4141, 4142, 4143

        Parameters
        debug 1
        display 1
        parents 53
        randomize 1
        sortBy pub_date
        sortDir DESC
        total 5

        I don’t underst whay ditto select all ID...
        I hae try queryLimit but the result is the same....

        So my problem is not solved...
        Any idea?
          • 25663 MODX Staff
          • 12,272 Posts
          Update to the latest Ditto 2.1 release available for now at ditto.modxcms.com (pending the release announcement text). I think 2.0.2 didn’t work so well in the randomization department.
            Ryan Thrash, MODX Co-Founder
            Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
            • 34162
            • 1 Posts
            Now I’m using 2.1 but the problem remain.

            Can Ditto select only if a TV is set to 1? If yes I can add a TV ad set to 1 only if it must be in my home and randomize this select...

            Can I have an exaple of ditto call with a TV?
            Thanks.
              • 7231
              • 4,205 Posts
              You would need a filter for that.

              Something like filter=`tvname,value,1` will remove all documents that have a value not equal to the value you set. I don’t know how the randomize works with filters.

              http://ditto.modxcms.com/tutorials/basic_filtering.html

              I found this interesting in the debig report:
              Sort By RAND()
              As far as I can tell this will cause the custom sortBy to be ignored so you can remove that from the call.

              You might also approach this another way. Great a snippet that returns an array of the last 10 published documents and then simply return a random ID from that array and use it in the &documents parameter to return a single document.

              &documents=`[!snippet!]`

              I will see if I can put something together quickly, shouldn’t be to hard (famous last words). grin
                [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                Something is happening here, but you don't know what it is.
                Do you, Mr. Jones? - [bob dylan]
                • 7231
                • 4,205 Posts
                Try this little snippet to generate the random document ID from the last 5 published posts in a given parent. It usses Ditto itself to generate the list:

                <?php
                // randomDoc
                $parent = isset($parent) ? $parent : $modx -> documentObject['parent'];
                $total = isset($total) ? $total : 5;
                // Use Ditto to generate document list
                $documents = $modx->runSnippet("Ditto", array(
                	"parents" => $parent,
                	"display" => $total,
                	"sortBy" => "pub_date",
                	"sortDir" => "DESC" ,
                	"tpl" => "@CODE [+id+]," ));
                
                // transform the ditto output into an array	
                $documents = explode( ',', $documents);
                
                // generate the random number from 0 to total minus 1 to compensate for the array begining at 0
                $y = rand(0, ($total-1));
                
                // output the random document ID
                return $documents[$y];
                ?>
                Notice that the parent and total parameters can be set in the snippet call if needed or personalized directly in the snippet code.

                Then you can use it in a ditto call like this to return a random document:
                [!Ditto? &documents=`[[randomDoc]]`!]
                Remember the alternating snippet cache settings.

                There are many other ways to do this, but this is all I got. I personally like the simplicity of using snippets within wrapper snippets. You could take this one step further and add the seccond snippet call within the snippet itself and simplify the call altogether. Hope this helps grin
                  [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                  Something is happening here, but you don&#39;t know what it is.
                  Do you, Mr. Jones? - [bob dylan]
                  • 34162
                  • 1 Posts
                  Wonderfull! I can use dito to call a snippet output... !! This solve anyproblem! THANKS!! Great support!