We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8091
    • 14 Posts
    Hi there,
    I need some assistance - I have a PHP application to implement in my MODX site but my PHP programmer has gone home!
    The code is split into two Snippets that need to work together and also pull some very basic input from a form on the site.

    I thought I’d post here to get some advice as to the best way to progress. How difficult is it to implement PHP in Snippets for a non-PHP-programmer? Secondly, is there anyone out there that would be interested in assisting or implementing the code for me?

    Much appreciate your guidance.

    Thanks, DrRog
      • 10449
      • 956 Posts
      Quote from: DrRog at Jul 04, 2007, 04:29 PM

      How difficult is it to implement PHP in Snippets for a non-PHP-programmer?

      Well, how long is a rope? smiley

      PHP in modx snippets is just that: PHP. You might use the modx API, although that’s not required. It really depends on what you need to accomplish...
        • 8091
        • 14 Posts
        See your point!

        Here is a basic description. The calculator is an object and the get flight details is an untested function. The function takes a flight code as input via a form and then grabs the details of that flight via an xml webservice. Some of these details are then used by the calculator object which obviously does a calculation and provides the desired output. Below is the description left by my PHP programmer...

        Dose calculator:

        doseCalculator($startLat, $startLon, $endLat, $endLon, $timeDuration)

        so for example, get the flight details, lat long of start etc, and duration, and then pass it to a new object like so:


        // get the details from the user, this time in url fragment e.g.
        // http://www.urlwhatever.com/page.php?id=lh447 - xml webservice
        // addslashes as example
        $strFlight = addslashes($_GET[’id’]);

        // from a function
        $flightDetails = getFlightDetails($strFlight);

        // to a class
        $doseCalculator = new doseCalculator($flightDetails [0],$flightDetails[1] ,$flightDetails [2], $flightDetails[3], $flightDetails[4]);

        // or
        $doseCalculator = new doseCalculator(-80,10,80,50,50);

        // for debugging you can target the functions of the class
        // e.g. test the output of the thuleGLM
        $getThuleCalc = $doseCalculator->getThuleGLM($value);


        This will return the output of the calculation back to $doseCalculator as an object.

        As you may have guessed the final output is the dose received on a flight.

        Let me know if you need more details - I can post the snippets if required.
          • 13507
          • 7 Posts
          the code posted looks like it is fairly independent, to add this in a snippet shouldn’t pose a problem.

          It looks like you will need to include the class for the doseCalculator before your code, like this:
          include_once "path/to/class/file.php";
          


          Also in order for MODx snippets to correctly output the results you need to return it as the last thing in the snippet, something like this
          return $doseCalculator->renderHTML();
          


          hope this makes sense laugh
            • 8091
            • 14 Posts
            Thanks that helps smiley

            I’d also appreciate it if you could give me some pointers on how to set up a form that works with the function snippet.
            I need to know how to set up the single text area form such that it posts the input to the function snippet so that it in turn can call the webservice etc...

            Creating a form is no problem, but how do I get it to post the input to the snippet?

            Cheers

            Roger
              • 13507
              • 7 Posts
              Data from forms will be available in the PHP Global $_POST or $_REQUEST array(s), so if you have a hidden form element with a name of form_flightcode you would use the following to access it:
              $flightCode = $_REQUEST['form_flightcode'];
              


              That can go before your other code in the same snippet, that way you can access it

              I personally put snippets to handle forms in the same page as the form, that way I can check the data on the server side and output messages if the user hasn’t filled in the form correctly.

              So my templates go like this (in a VERY pseudo code way wink )
              <html>
              <head></head>
              <body>
              [!formsnippet!]
              ....
              Insert form html here.
              ....
              </body>
              </html>
              


              As a slight variant on the above, I usually get the snippet to write out the form if your post variables are empty or if there is an issue with the data received back.

              $myform <<<END
              <form>
              ....
              form stuff here.
              ....
              </form>
              END
              
              $flightCode = $_REQUEST['form_flightcode'];
              
              //If flightCode isn't set show the form
              if(empty($flightCode)) {
                   return $myform;
              } else {
                   //Do something amazing...
              }
              


              I hope these random ramblings help! smiley

              EDIT: Heliotrope has a great point, we don’t want to cache output from a snippet!, Im glad someone is awake laugh
                • 11975
                • 2,542 Posts
                Hi,

                I would advice you to call the snippet used to retrieve the post or get values uncached

                [!formsnippet!] (or else uncached the page where the snippet is called)

                or else the snippet could not access to them.

                :-)
                  Made with MODx : [url=http://www.copadel.com]copadel, fruits et l