We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6705
    • 79 Posts
    I’ve written a snippet which extracts and displays event data from a database. I now need to include a web link (to a second snippet) in the displayed information which, when selected by the user, would generate an iCalendar entry for the event. In other words, my first snippet would output something along the lines of:

    Date, Time.
    Event Title.
    Event Location.
    * [Get iCalendar entry]


    The link destination for "Get iCalendar entry" needs to include the eventid as a parameter, so that the second snippet can use this to look up the event in the database and then generate and return the appropriate iCalendar data.

    The link would be theoretically be something like: http://mysite.dom/makeical?event=eventid


    My question is: how do I create such a link that in effect calls my second (iCalendar) snippet, including the parameter that needs to be passed? Can I somehow call a snippet directly via a web link, and if so, how?

    Or can snippets only be accessed via an embedded [[snippet? &eventid=’whatgoeshere’]] call in a MODX page? In which case, would my link have to be something like [~modxpageid~]?event=eventid (and how would I get my web link parameter passed on from the page to the snippet)? However, it would seem to be a bit cumbersome to have to create an intermediate ’mini-page’ for every snippet that I want to call in this kind of way.

    Or, am I approaching this in the wrong way entirely? undecided
      Please don't PM me unless it's absolutely essential: if a technical question is worth asking, it's worth asking in public, so that others can share their experience, and so that all can learn from the answers.
      • 3749
      • 24,544 Posts
      I’m not completely clear on what you’re trying to do, but how about setting a $_SESSION variable in your first snippet and reading it in the second one?
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 6705
        • 79 Posts
        Thanks, BobRay. Sorry if I wasn’t clear in my question, I’ll try to rephrase it:

        One MODx page contains a snippet which extracts data from a database and outputs (something like) the following:

        Date, Time.
        Event Title.
        Event Location.
        * [Get iCalendar entry]

        When the reader views that page, they can (if they want) select the "Get iCalendar entry" link, which is intended to call another web page (a snippet, or PHP code, at any rate: perhaps snippet is not the right term here, but that’s the way to use PHP functions in MODx, isn’t it?) which will generate the iCalendar data for that event.

        The second snippet isn’t connected with the first snippet in any way, apart from one: the link address that the first snippet already generated (in the text output above) has to contain a parameter in the link, so that that value can be used by the second snippet (ie, a $_GET[’var’] parameter in PHP terms) so that the second snippet (when called via that link) knows which event it needs to look up in the database to create the iCalendar data for.


        However, this question may now be moot for me as I’ve now discovered that I can in fact create a real PHP file within the web file system, and therefore have the first snippet just create an ordinary web link pointing to that PHP file (eg, http://mysite.dom/makeical?event=eventid), bypassing snippets and MODx altogether as I won’t then actually need to use a MODx snippet for the second function in this case.

        This is a little odd, as I could swear that when I tried this approach earlier, MODx was intercepting the web address and returning a 404 (I thought because it was trying to look for such a page within the MODx pagelist, and obviously being unable to find it).. However, if it now works, that’s good enough for me in this instance.
          Please don't PM me unless it's absolutely essential: if a technical question is worth asking, it's worth asking in public, so that others can share their experience, and so that all can learn from the answers.
          • 4172
          • 5,888 Posts
          if you want to call different php-files with $_GET, you can have one snippet for example something like this:

          <?php
          
          $allowedfiles=array('php1','php2','php3');
          
          $phppath=$modx->config['base_path'].'assets/snippets/phppath/';
          $phpfile=$_GET['runphp'];
          if (in_array($phpfile,$allowedfiles)){
          include($phppath.$getphp.'.php'); 
          }
          
          ?>
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 3749
            • 24,544 Posts
            If I’m understanding you correctly, you just need to pass the text of the link from one snippet to another. There’s no reason to involve the URLs here, you just need to pass that piece of data.

            I think there are probably about 10 ways to do what you want in MODx. wink

            I’d just have the first snippet write the web address (link) to a $_SESSION variable:

            $_SESSION['link'] = 'http://link here';


            And then have the second one read it:

            $link = $_SESSION['link'];


            You could also just write the link as text to a small .txt file and use file_get_contents() in the second snippet.

            There’s also a way to append the variable to the end of the link (though I can’t remember the syntax at the moment).

            I’d advise against an external .php file. Friendly URLs my interfere with it and there are potential security risks. /YMMV
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 18988
              • 3 Posts
              This helped me a lot smiley no more headache since I read this post !

              Problem :
              - My template has a swf that calls a xml page. The xml page is a modx doc, calling a snippet to generate data.
              - The (template) swf calls the xml like this : datafeed = /index.php?id=XX&currentpage=[*id*]
              - But the snippet called in the xml doc could not get the value declared in the swf xml link "currentpage=XX", even with $_GET[’currentpage’] method.

              It works perfectly now :
              - my template calls a snippet that stores the currentpage value in $_SESSION[’currentpage’]
              - the flash calls its feed like this : datafeed = /index.php?id=XX
              - the snippet called in the xml doc gets the currentpage with $_SESSION[’currentpage’]

              It appears so clear to me now... wink