We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7455
    • 2,204 Posts
    I am working on www.eljakim.nu

    and if you look at the home page you see a picture behind a frame you see a title below the frame and som info in the left column
    i need a way to have thos 3 items (paired they belong togetter) to be random (or atleast alternate) like the random image snipped does but now it must be done in a way that those 3 items that need to be togetter (name below the picture is name of the picture and text is a intro of the article aubout this picture) stay togetter when the change happends.
    the way to enter those info could be done by making a new document containing those tv’s and maybe make a snippet that randomly or in order shows a nother page from a list of pages (maybe a folder containing those pages like the news snippet?)
    that way it would be easy just greate a bunch of home pages and the random page snipped would do the rest.
    but is it posible to a snippet like that?

    I found a snippet in the eto libery that shows a page where you use that snipped but it only works on the content field.
    //                              
    // Snippet: Showdoc by VTX
    //                              
    // Usage: [[ShowDoc?id=x]]      
    // x = the document to show     
    //                                
    
    $output = '';
    
    $tbl = $this->dbConfig['dbase'].".".$this->dbConfig['table_prefix']."site_content";
    $sql = "SELECT content FROM $tbl WHERE $tbl.id = $id";
    $rs = $etomite->dbQuery($sql);  
    $resource = $etomite->fetchRow($rs);
    
    $output = $resource['content']; 
    
    return $output;
    



    would be a cool option to have a snipped that can do that.
    maybe a snippet that get the information from a table by reading all the pages from a folder like above and then depending on the snippet call displays random content, something like [[DisplayTV?tvid=4]] where 4 could be the TVid in the database.

    I hope that someone could help me figure out this one
    Greets Dimmy
      follow me on twitter: @dimmy01
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Have the image, caption and the text in a document, and have the caption and the text in their own divs with an ID in the document (don’t use the WYSIWYG editor for these documents).

      <img src=assets/images/whatever.jpg alt="whatever" height="xxx" width="xxx" />
      <div id="caption" style="display:none;">Whatever</div><div id="text" style="display:none;">This is a picture of whatever.</div>


      With their display set to "none", they will still be in the source, but not shown at all. Have a bit of javascript and DOM take the text out of the "text" div and put it to the div in the side, and the text of the "caption" div to the span under the image (I do hope you can get this either on your own or from somebody else; I really hate working with javascript).

      Have a TV called from where you want the image displayed, and have it select the document randomly from a folder full of these.

      @DOCUMENT [!RandomDoc?folder=`44`!]

      // snippet RandomDoc
      // returns a random document id from 
      // a given folder
      // usage [!RandomDoc?folder=`xxx`!]
      // if $folder is not set, uses current doc ID as folder
      $folder = isset($folder)?$folder:$modx->documentIdentifier;
      $docs = $modx->getActiveChildren($folder);
      //return a random document id
      $randomDocId = rand(1, count($docs));
      return $docs[$randomDocID]['id']; 
      


      This is just off the top of my head; probably needs a bit of fixing. You might even be able to add the random ID selection code in the TV; I’m really not as familiar with them as I would like to be.

        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
        • 7455
        • 2,204 Posts
        OK thank I see what you meen, whith this snipped i have indeed a random page that i could display using div’s.

        I read it right that this will give a random pageid on each pagerefresh that number i could use to get tv’s from that id and disply them?

        what if i would make an empty template with only that snipped call in it (combining show doc and you part).
        will it produce a page from that folder?

        Dim
          follow me on twitter: @dimmy01
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Use the TV to place the (random) document’s content in your home page template. It gets its document ID from the snippet. Other than that, it’s no different from any other TV with the @DOCUMENT xxx value; it loads the content of the given document in the location where it’s placed in the template.

          You could use that snippet for any place you wanted a random document/folder ID; the snippet call would simply replace the normal document Id number that you would use.
            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
            • 4266
            • 70 Posts
            Quote from: sottwell at Oct 06, 2005, 02:40 AM

            You could use that snippet for any place you wanted a random document/folder ID; the snippet call would simply replace the normal document Id number that you would use.

            I need to insert random html-content on the site home page. So, I’ve tried to do that with your snippet, but failed. It seems I’m missing something.

            Please, could you clarify it in ABC-maner  rolleyes
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              You have to use a TV to get the content into your document. The snippet doesn’t display anything, all it does is return a random document ID that you can use in something that will display the contents of that random document. You could modify the snippet to make it actually echo the content of the random document...hmm.

              // snippet RandomDoc
              // returns a random document id from 
              // a given folder
              // usage [!RandomDoc?folder=`xxx`!]
              // if $folder is not set, uses current doc ID as folder
              $folder = isset($folder)?$folder:$modx->documentIdentifier;
              $docs = $modx->getActiveChildren($folder);
              //return a random document id
              $randomDocId = rand(1, count($docs));
              $contentRow = $modx->getPageInfo($randomDocId,1,'content');
              echo $contentRow['content'];
              return;
              


              might be interesting to try out.

              Anyway, if you use a TV with the original snippet...

              Create a TV with the default value as mentioned below, @DOCUMENT [!RandomDoc!]. I’m not sure what kind of "widget" you would need, if any.

              Assign it to the template you’ll be using.

              Then use the TV in the template or directly in the document where you want to get the content of the random document displayed.

                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
                • 4266
                • 70 Posts
                Quote from: sottwell at Oct 06, 2005, 09:15 AM

                // snippet RandomDoc
                // returns a random document id from 
                // a given folder
                // usage [!RandomDoc?folder=`xxx`!]
                // if $folder is not set, uses current doc ID as folder
                $folder = isset($folder)?$folder:$modx->documentIdentifier;
                $docs = $modx->getActiveChildren($folder);
                //return a random document id
                $randomDocId = rand(1, count($docs));
                $contentRow = $modx->getPageInfo($randomDocId,1,'content');
                echo $contentRow['content'];
                return;
                


                might be interesting to try out.


                As I understand, this modified snippet can display the content of random documents without using TV. Am I right?
                I’ve tried to use it, but it displays content of all documents on the site, not documents of the specified folder (in my case folder=`17`).

                When I’m trying to use the original snippet with TV as you have desribed, nothing happens huh. See attached sreenshots below. Could you tell me what I miss?
                  • 28042 ☆ A M B ☆
                  • 24,524 Posts
                  Ok, got another project finished, I’ll poke and dig at this and see if I can get it to actually do something. I’ll work on the snippet-only model; I’m a firm believer in the KISS principle. Will get back to you as soon as I have something; I think I see the bug in the code already.
                    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
                    • 28042 ☆ A M B ☆
                    • 24,524 Posts
                    Here ya’ go. Just drop this wherever you want the content to show up.

                    // snippet RandomDoc
                    // returns content from a random document 
                    // from a given folder
                    // usage [!RandomDoc?folder=`xxx`!]
                    // if $folder is not set, uses current doc ID as folder
                    $folder = isset($folder)?$folder:$modx->documentIdentifier;
                    $docs = $modx->getActiveChildren($folder);
                    // get a random document id
                    $rnd = rand(1, count($docs));
                    $rnd -=1; // because we need to start at 0 in the array
                    $randomDocId = $docs[$rnd]['id'];
                    // get the content of the random doc
                    $contentRow = $modx->getPageInfo($randomDocId,1,'content');
                    echo $contentRow['content'];
                    return;
                      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
                      • 4266
                      • 70 Posts
                      Yes, yes!!!  grin It works!

                      Sottwell, BIG thank you! wink