We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 20765
    • 90 Posts
    myfriendscallmebill Reply #1, 20 years, 1 month ago
    Hi: I have a set of three pages. When any one of the pages is first invoked, the URL will have a query string attached. Thereafter, as I flip between these three pages, I need to make the initial query string available to all each page. I thought the easiest way would be to grab the query string from the initially-invoked page, and attach it to the HREFs in the <A> tags that will let users flip between the pages. So I’m wondering if there’s a system variable I can put in a tag. This would let users flip between the three pages with a set of links on each page, while passing the initial query string back and forth between them:

    <a href="1.html?[(queryString)]>Go to page 1 of the set</a>
    <a href="2.html?[(queryString)]>Go to page 2 of the set</a>
    <a href="3.html?[(queryString)]>Go to page 3 of the set</a>

    Is there a way to do this?
      • 16429
      • 254 Posts
      Mmm... see if this works: use it like a snippet, call it after the urls, like

      <a href="1.html?[!PassGETS!]">Go to page 1 of the set</a>

      Eventually you have to play with caching (both the snippet or the page caching) to get it working perfectly.
      If it works smiley

      $param = '';
      foreach ($_GET as $key => $value) {
           $param .= '&' . $key . '=' . $value;
      }
      $gets = substr($param, 1);
      $output = '?'.$gets;
      return $output;
      
        kudo
        www.kudolink.com - webdesign (surprised?)

        [img]http://www.kudolink.com/kudolinkcom.png[/img] [sup]proudly uses[/sup] [img]http://www.kudolink.com/modx.png[/img]
        • 30223
        • 1,010 Posts
        Quote from: kudolink

        $param = '';
        foreach ($_GET as $key => $value) {
             $param .= '&' . $key . '=' . $value;
        }
        $gets = substr($param, 1);
        $output = '?'.$gets;
        return $output;
        


        That might well work but you could run into trouble when you turn off friendly urls. If you bother making a snippet for it you should make sure it works in all cases, for all urls like 1.html and index.php?id=1 or index.php?req=1.html otherwise you might end up with a query string like this index.php?id=15&id=39. You might also want to kill the q=3.html which is generated by the rewrite for friendly urls, just in case. And eh,.. what about the hardcoded urls? You should really use [~3~] (or whatever the id of the page you’re linking to is.

        Here’s one way to do it.
        Create a new snippet called passGets and paste the code below (without the <?php and ?> tags);
        <?php
        #snippet passGets
        //if no url then use current page (use any page you want instead)
        $url = isset($pageId)?$modx->makeUrl($pageId):$modx->makeUrl(1);
        //make a copy of $_GET so we don't destory anything
        $qry = $_GET;
        //remove sensitive vars
        unset($qry['id'],$qry['q']); //add any others that you might want to strip 
        //create new query string
        array_walk($qry,create_function ('&$v,$k','$v="$k=$v";'));
        $qry = @implode('&', $qry);
        if($qry) $url .= (strstr($url,'?'))?'&':'?';
        return $url .$qry;
        ?>
        


        Now when you create the links use a snippet call in this format:
        <a href="[[passGets? &pageId=`1`]]">Go to page 1 of the set</a>
        <a href="[[passGets? &pageId=`2`]]">Go to page 2 of the set</a>
        <a href="[[passGets? &pageId=`3`]]">Go to page 3 of the set</a>
        

        where 1, 2 and 3 are the actual id numbers of the page (which you can find in the document tree).
        This will work regardless if you have friendly urls on or not..

        Hope this helps

        Oops,.. you might want to use the [!passGets ...!] variant instead so they don’t get cached.


          • 16429
          • 254 Posts
          Perfect.
          I wrote those 7 lines in a hurry, but what TobyL says is right.
            kudo
            www.kudolink.com - webdesign (surprised?)

            [img]http://www.kudolink.com/kudolinkcom.png[/img] [sup]proudly uses[/sup] [img]http://www.kudolink.com/modx.png[/img]
            • 27226
            • 186 Posts
            Great tip, TobyL, Thanks!
              • 20765
              • 90 Posts
              myfriendscallmebill Reply #6, 20 years, 1 month ago
              kudolink and TobyL, thank you so much!

              I just set up a test with TobyL’s code, and found that it returned the page URL (domain + page) in addition to the query string, so I changed the last line of code:

              BEFORE = return $url .$qry;
              AFTER = return $qry;

              And I renamed the snippet QueryString, since that reflects what it produces. Now I can pass the query string back and forth at will between the three pages in my group with a set of links such as:

              <p><a href="307?[!QueryString!]">Flip to page 1</a></p>
              <p><a href="308?[!QueryString!]"> Flip to page 2</a></p>
              <p><a href="309?[!QueryString!]"> Flip to page 3</a></p>

              Where 307, 308 and 309 are the docIDs of the three pages.

              This all works splendidly on my test pages. Later today I’ll retrofit the website I’m developing and cross my fingers.

              Again, thank you both very much!
                • 30223
                • 1,010 Posts
                @myfriendscallmebill By all means, change it that way if you like but if you want to harness the power of modx, I’d advice you to stick to MODx’s way of writing your urls.

                <p><a href="307?[!QueryString!]">Flip to page 1</a></p> is not the way to do it... What if one day you decide to change your friendly urls settings? You have to change all those links. Use the proper MODx syntax and that will never be a problem (unless you actually delete a page off course)

                So leave the code as is and write the urls like this:
                <p><a href="[[PassGets? &pageId=`307`]]">Flip to page 1</a></p>

                This gives you much more flexibility and there’s nothing that’s more difficult to type is there? Why are you limiting yourself?