We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10165
    • 129 Posts
    Is there a simple way to grab the full current url params as is so I can add to other links on the page (retaining all the current params)

    i.e. if I have for instance product list page:
    search-results-page.html?&param1=Something+Here&param2=Something&param3=Somevalue etc..

    and I want links to the product page to be
    product-item-page.html?&param1=Something+Here&param2=Something&param3=Somevalue etc..

    I’m hoping this can be done without getting each var individually maybe?

    thanks all
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      You can use a snippet to get the $_SERVER[’REQUEST_URI’] then break it at the ? using explode() and then append the query string part onto your links.

      Be careful; if you are using friendly URLs you’ll need the first ?, but if you are not using friendly URLs the index.php?id=xx uses that ? so the first name=value pair will need to start with &.
        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
        • 10165
        • 129 Posts
        great thanks Susan, I’ll give it a go..
          • 10165
          • 129 Posts
          here’s how it ended up...
          <?php
          if(isset($_SERVER['REQUEST_URI'])) {
               $myURI = $_SERVER['REQUEST_URI'];
               $params = explode('?', $myURI);
               if($params[1] != '') {
                    $output = '?'.$params[1];
                    return $output;
              }
          }
          ?>

          thanks again for steering me in the right direction..
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            You should always have a return; statement at the very last line in a snippet just in case it gets that far. It probably won’t hurt anything as it is, but it’s a good habit to get into.
              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
              • 10165
              • 129 Posts
              good point, thanks Susan..
                • 4041
                • 788 Posts
                Just an idea, but if you are running this snippet in a bunch of places on the same page, it might be more efficient to use a placeholder setup like so:

                Call the snippet in the top of the template to set the placeholder.
                <?php
                if(isset($_SERVER['REQUEST_URI'])) {
                     $myURI = $_SERVER['REQUEST_URI'];
                     $params = explode('?', $myURI);
                     if($params[1] != '') {
                          //$output = '?'.$params[1];
                          //return $output;
                        $modx->setPlaceholder('uri_params', '?'.$params[1]);    // [+uri_params+]
                    }
                }
                ?>


                Then tack on the placeholder to the links: a href="[~XX~][+uri_params+]"
                  xforum
                  http://frsbuilders.net (under construction) forum for evolution
                  • 10165
                  • 129 Posts
                  great idea, thanks Breezer, certainly would be much more efficient as I’m using it in a chunk for a list of products.