We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44877
    • 21 Posts
    I thought I'd be able to understand this but apparently I am missing something. How would I go about getting this script for a calculator into a working snippet. I keep getting a parsing error but it works fine when I test it off of MODX.

    <?php
    function selected( $val = "")
    {
        $units = isset( $_POST['units'] ) ? $_POST['units'] : null;
        echo ( $units == $val ) ? "CHECKED='CHECKED'" : "";
    }
    $units = isset( $_POST['units'] ) ? $_POST['units'] : null;
    $dia = isset( $_POST['dia'] ) ? $_POST['dia'] : null;
    $depth = isset( $_POST['depth'] ) ? $_POST['depth'] : null;
    $L = isset( $_POST['L'] ) ? $_POST['L'] : null;
    $num = isset($_POST['num']) ? $_POST['num'] : null;
    $waste = isset($_POST['waste']) ? $_POST['waste'] : null;
    $knownvolume = isset($_POST['knownvolume']) ? $_POST['knownvolume'] : null;
    $hw = isset($_POST['hw']) ? $_POST['hw'] : null;
    //$denom = ( ( $units == "US" ) ? 12 : 100 );
    $tubecoverage = ( ( $units == "US" ) ? 18.59 : 304.65);
    $onegalcoverage = ( ( $units == "US" ) ? 231 : 3785.41);
    $twogalcoverage = ( ( $units == "US" ) ? 462 : 7470.82);
    $fivegalcoverage = ( ( $units == "US" ) ? 1155 : 18927.06);
    
    $measurements = 'in or cm';
    if( $units == 'US' ){
        $measurements = 'in';
    }
    if($units == 'Metric')
    {
        $measurements = 'cm';
    }
    
    if($units == 'US')
    $length = 'feet';
    else if($units == 'Metric')
    $length = 'meters';
    else 
    $length = 'feet or meters';
    
    $tubes = ($tubes);
    if($knownvolume == TRUE)
    $tubes = (($knownvolume / $tubecoverage) * ($waste + 1) * 1.05);
    else 
    $tubes = (((($dia / 2) * ($dia / 2) * pi()) / $tubecoverage) * ($waste + 1) * 1.05);
    
    $onegal = ($tubes);
    if($knownvolume == TRUE)
    $onegal = (($knownvolume / $onegalcoverage) * ($waste + 1) * 1.05);
    else 
    $onegal = (((($dia / 2) * ($dia / 2) * pi()) / $onegalcoverage) * ($waste + 1) * 1.05);
    
    $twogal = ($tubes);
    if($knownvolume == TRUE)
    $twogal = (($knownvolume / $twogalcoverage) * ($waste + 1) * 1.05);
    else 
    $twogal = (((($dia / 2) * ($dia / 2) * pi()) / $twogalcoverage) * ($waste + 1) * 1.05);
    
    $fivegal = ($tubes);
    if($knownvolume == TRUE)
    $fivegal = (($knownvolume / $fivegalcoverage) * ($waste + 1) * 1.05);
    else 
    $fivegal = (((($dia / 2) * ($dia / 2) * pi()) / $fivegalcoverage) * ($waste + 1) * 1.05);
    
    ?>
    <form method='post' action=''>
    <table border='0' width='500px' cellpadding='2' cellspacing='1' class="calculator">
    <tr class="calcheading"><td><label><input type="radio" name="units" <?php selected("US")?> value="US" />S.A.E.</label>
    <label><input type="radio" name="units" <?php selected("Metric")?> value="Metric" />Metric</label> </td></tr>
    
    <tr class="calcheading"><td colspan="2"><strong>Cicular Volume Area</strong></td></tr>
    
    <tr class="calcrow"><td>Known Volume (<?php echo $measurements ?>):</td><td align="center"><input type='text' name='knownvolume' value="<?php echo $knownvolume ?>"/></td></tr>
    
    <tr class="calcrow2"><td>Dia (<?php echo $measurements ?>):</td><td align="center"><input type='text' name='dia' value="<?php echo $dia ?>"/></td></tr>
    
    <tr class="calcrow"><td>Length (<?php echo $measurements ?>):</td><td align="center"><input type='text' name='L' value="<?php echo $L ?>"/></td></tr>
    
    <tr class="calcrow"><td>Waste Variance % (Please add in decimal form):</td><td     align="center"><input type='text' name='waste' value="<?php echo $waste ?>"/></td></tr>
    
    <tr class="submit"><td colspan="2"><input type='submit' value='Calculate'/></td></tr>
    
    <tr class="calcrow">
    <td><i>10.3 oz Tube(s) (CS-1500):</td>
    <td align="center"><input type="text" value="<?php echo round($tubes, 2)?>"/></td></i>
    </tr>
    <tr class="calcrow">
    <td><i>1 Gallon Bucket(s) (CS-1000):</td>
    <td align="center"><input type="text" value="<?php echo round($onegal, 2)?>"/></td></i>
    </tr>
    <tr class="calcrow">
    <td><i>2 Gallon Bucket(s) (CS-1800):</td>
    <td align="center"><input type="text" value="<?php echo round($twogal, 2)?>"/></td></i>
    </tr>
    <tr class="calcrow">
    <td><i>5 Gallon Bucket(s) (CS-1000/CS-1500):</td>
    <td align="center"><input type="text" value="<?php echo round($fivegal, 2)?>"/></td></i>
    </tr>
    </table>
    </form>
    • Snippets should not be mixed HTML and PHP. You should collect the output into a variable, then return the output.

      In this case, the HTML structure for the output could be loaded into placeholders, then a chunk used to format the output, in the same manner as most MODx snippets such as Wayfinder and getResources.

      Or if you want it fast-and-dirty, just convert that HTML output into something like this:
      $output = '<form method="post" action="[[~[[*id]]]]">';
      ...
      $output .= '<tr class="calcrow"><td>Known Volume (' . $measurements . '):</td><td align="center"><input type='text' name='knownvolume' value="' .  $knownvolume . '"/></td></tr>';
      ...
      return $output;


        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
        • 44877
        • 21 Posts
        Quote from: sottwell at Aug 26, 2013, 06:18 PM
        Snippets should not be mixed HTML and PHP. You should collect the output into a variable, then return the output.

        In this case, the HTML structure for the output could be loaded into placeholders, then a chunk used to format the output, in the same manner as most MODx snippets such as Wayfinder and getResources.

        Or if you want it fast-and-dirty, just convert that HTML output into something like this:
        $output = '<form method="post" action="[[~[[*id]]]]">';
        ...
        $output .= 'Known Volume (' . $measurements . '):<input type="text" name="knownvolume" value="' .  $knownvolume . '">';
        ...
        return $output;


        </form>

        Thank you, I'll see if I can do it the preferred way first. I'm just on a tight deadline and new to the MODx ways. But thank you again!
          • 44877
          • 21 Posts
          can you give me a quick example of how to go about doing this?
          • Examine the sample I posted. Watch your quote marks, or use a HEREDOC instead. Also note the aggregate $output variable assignments for each line - .= for all subsequent assignments. You can start building with an empty string initializing assignment like
            $output = '';
            $output .= '<form ... >';

            Put the HTML in a PHP variable - $output is good. Replace the inline PHP echo statements with . insertion of the variables in question.
            value="' .  $knownvolume . '"

            Then return the whole aggregate $output string.

            As far as putting all of the variables generated by your PHP code into placeholders (the recommended method for the most flexible usage), then processing a chunk tpl to insert the variables where needed, see templating your snippet.

            More:
            http://rtfm.modx.com/revolution/2.x/developing-in-modx/code-standards
            http://rtfm.modx.com/revolution/2.x/developing-in-modx/basic-development/snippets#Snippets-Don%27ttrytomixPHPandHTMLinaSnippet

            (new docs are being repaired at this moment; if there's still a problem with the docs try http://oldrtfm.modx.com/display/revolution20/Templating+Your+Snippets)
              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
              • 44877
              • 21 Posts
              This is what I have done, I created placeholders like you said, and I embedded the placeholders in a chunk called '1500calculator' like this [[+placeholder]] but I am still getting an error message. Does it have something to do with the output method at the bottom? The snippet is what I will place in the resource not the chunk correct?

              $parse_arr = array();
              
              
              if (!function_exists('selected')) {
                  function selected( $val = "") {
                      $units = isset( $_POST['units'] ) ? $_POST['units'] : null;
                      return ( $units == $val ) ? "CHECKED='CHECKED'" : "";
                  }
              }
              
              $units = isset( $_POST['units'] ) ? $_POST['units'] : null;
              $dia = isset( $_POST['dia'] ) ? $_POST['dia'] : null;
              $depth = isset( $_POST['depth'] ) ? $_POST['depth'] : null;
              $L = isset( $_POST['L'] ) ? $_POST['L'] : null;
              $num = isset($_POST['num']) ? $_POST['num'] : null;
              $waste = isset($_POST['waste']) ? $_POST['waste'] : null;
              $knownvolume = isset($_POST['knownvolume']) ? $_POST['knownvolume'] : null;
              $hw = isset($_POST['hw']) ? $_POST['hw'] : null;
              //$denom = ( ( $units == "US" ) ? 12 : 100 );
              $tubecoverage = ( ( $units == "US" ) ? 18.59 : 304.65);
              $onegalcoverage = ( ( $units == "US" ) ? 231 : 3785.41);
              $twogalcoverage = ( ( $units == "US" ) ? 462 : 7470.82);
              $fivegalcoverage = ( ( $units == "US" ) ? 1155 : 18927.06);
              
              $measurements = 'in or cm';
                  if( $units == 'US' ){
                  $measurements = 'in';
              }
                  if($units == 'Metric')
              {
                  $measurements = 'cm';
              }
              
              if($units == 'US')
                  $length = 'feet';
                  else if($units == 'Metric')
                  $length = 'meters';
                  else 
                  $length = 'feet or meters';
              
              $tubes = ($tubes);
                  if($knownvolume == TRUE)
                  $tubes = (($knownvolume / $tubecoverage) * ($waste + 1) * 1.05);
                  else 
                  $tubes = (((($dia / 2) * ($dia / 2) * pi()) / $tubecoverage) * ($waste + 1) * 1.05);
              
              $onegal = ($onegal);
                  if($knownvolume == TRUE)
                  $onegal = (($knownvolume / $onegalcoverage) * ($waste + 1) * 1.05);
                  else 
                  $onegal = (((($dia / 2) * ($dia / 2) * pi()) / $onegalcoverage) * ($waste + 1) *     1.05);
              
              $twogal = ($twogal);
                  if($knownvolume == TRUE)
                  $twogal = (($knownvolume / $twogalcoverage) * ($waste + 1) * 1.05);
                  else 
                  $twogal = (((($dia / 2) * ($dia / 2) * pi()) / $twogalcoverage) * ($waste + 1) * 1.05);
              
              $fivegal = ($fivegal);
                  if($knownvolume == TRUE)
                  $fivegal = (($knownvolume / $fivegalcoverage) * ($waste + 1) * 1.05);
                  else 
                  $fivegal = (((($dia / 2) * ($dia / 2) * pi()) / $fivegalcoverage) * ($waste + 1) * 1.05);
              
              
              $parse_arr['selected_us'] = selected("US");
              $parse_arr['selected_metric'] = selected("Metric");
              $parse_arr['measurements'] = $measurements;
              $parse_arr['depth'] = $depth;
              $parse_arr['knownvolume'] = $knownvolume;
              $parse_arr['dia'] = $dia;
              $parse_arr['L'] = $L;
              $parse_arr['waste'] = $waste;
              $parse_arr['hw'] = $hw;
              $parse_arr['tubes'] = $tubes;
              $parse_arr['onegal'] = $onegal;
              $parse_arr['twogal'] = $twogal;
              $parse_arr['fivegal'] = $fivegal;
              
              $output = $modx->getChunk('1500calculator', $parse_arr'[[+'measurements','selected_us','selected_metric','dia','L,waste','onegal','twogal','tubes','fivegal']]');
              return = $output;
              • Your $output should look like this. The array already contains all keys and values and you don't need to pass them again:
                $output = $modx->getChunk('1500calculator', $parse_arr);


                You can also take a look at http://rtfm.modx.com/revolution/2.x/developing-in-modx/basic-development/snippets/templating-your-snippets
                • The snippet goes in the resource; it will pick up the chunk to format its output. What error are you getting?
                    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
                    • 44877
                    • 21 Posts
                    labr1005 fixed my problem. I was getting a parser error I do believe. But

                    $output = $modx->getChunk('1500calculator', $parse_arr); 


                    is what fixed that problem. Thank you, labr1500.

                    Now I'm just trying to get the calculator to function properly. Thank you for all your help!