Results 1 to 8 of 8

Thread: Use of formulas in form generator(s)???

  1. #1
    New user
    Join Date
    09-19-10.
    Location
    Orlando, FL
    Posts
    11

    Default Use of formulas in form generator(s)???

    Hi guys,

    I need to calculate the various aspects of a grade using a formula and seem to be stuck...
    The formula to work with is: L = (He - Ha) * sqrt(1 + (10000/(power(S,2)))
    where
    He = end elevation
    Ha = start elevation
    S = slope in %

    The data-entry form is no problem and works just fine; the problem arises in the "result output form".
    1. Sessionform is of no help here, as it doesn't allow formulas in the "Calculate from session" field.
    2. Using insert tags in efg or the standard form generator produces plain text output instead of the formula's result.
    3. Creating a custom insert tag with nested insert tags in the formula does not produce any output.

    If I could use insert tags, the formula would look like this:
    Code:
    ({{form::He}} - {{form::Ha}} * bcsqrt(1 + (10000/bcpow({{form::S}},2,2))
    For now, I worked around with redirects to pages with custom php code. As a reference, here is the link to my website. The Contao version is 2.10.3.

    Can anybody please shed some light on this and point me to the right direction?
    Thanks in advance,
    Erhard

  2. #2
    User
    Join Date
    06-19-09.
    Posts
    328

    Default Re: Use of formulas in form generator(s)???

    Quote Originally Posted by erhard
    Hi guys,

    I need to calculate the various aspects of a grade using a formula and seem to be stuck...
    The formula to work with is: L = (He - Ha) * sqrt(1 + (10000/(power(S,2)))
    add a text field to your form.

    use the load form field hook to display a disabled text field if no value have been submitted or the result if the user filled every required field.
    Consulenza Contao CMS https://www.intco.it

  3. #3
    New user
    Join Date
    09-19-10.
    Location
    Orlando, FL
    Posts
    11

    Default Re: Use of formulas in form generator(s)???

    ga.n,
    thanks for the hint.

    However, it seems to me that the formula is not being processed (parsed). I can see the field values in the result page but I don't see the result of the formula.

    Questions:
    1. Where and how do I load the hook (loadFormfield)? I didn't need to use it yet, so I am not familiar with it...
    2. Where do I actually initiate the formula-processing? As far as I understood, this has to be done in the result-page but how??

    Or is this a redundant question?

    Puzzled,
    Erhard

    Addendum:
    From my logic, it was more approbiate to use the "processData"-Hook, since I want to calculate a result from a formula.
    So I tried this:
    system/modules/GetGradeLength/config/config.php:
    Code:
    <?php if (!defined('TL_ROOT')) die('You cannot access this file directly!');
    $GLOBALS['TL_HOOKS']['processFormData'][] = array('GetGradeLength','findgradelen');
    ?>
    system/modules/GetGradeLength/GetGradeLength.php:
    Code:
    <?php if (!defined('TL_ROOT')) die('You cannot access this file directly!');
    
    class GetGradeLength extends Frontend
    {
    	public function findgradelen($arrPost,$arrForm,$arrFiles)
        {
        	if($arrPost['FORM_SUBMIT'] == 'auto_form_19')
            {
            	// Get values from form fields:
                $this->He	= $arrPost['He'];
                $this->Ha	= $arrPost['He'];
                $this->Sl	= $arrPost['Sl'];
                $this->Di	= $arrPost['Di'];
                
                //Compute formula:
                $this->Di	= ($this->He - $this->Ha) * bcsqrt(1 + (10000 / bcpow($this->Sl,2,2)));
            
                // Write result to form field
    	     $arrPost['Di'] = $this->Di;
            }
        }
    }
    ?>
    I don't see a result though. Any idea what I am missing?

    Thanks,
    Erhard

  4. #4
    User
    Join Date
    06-19-09.
    Posts
    328

    Default Re: Use of formulas in form generator(s)???

    Quote Originally Posted by erhard
    Addendum:
    From my logic, it was more approbiate to use the "processData"-Hook, since I want to calculate a result from a formula.
    no, because the processFormData does not work this way, the processFormData will be appropriate if you need to write the result somewhere (e.g. email, update a field in the database, a text file)

    Quote Originally Posted by erhard
    So I tried this:
    system/modules/GetGradeLength/config/config.php:
    Code:
    <?php if (!defined('TL_ROOT')) die('You cannot access this file directly!');
    $GLOBALS['TL_HOOKS']['processFormData'][] = array('GetGradeLength','findgradelen');
    ?>
    create a 'get_grade_length' field in your form and replace the above code with the loadFormField instead of processFormData

    then in your GetGradeLength

    Quote Originally Posted by erhard

    system/modules/GetGradeLength/GetGradeLength.php:
    Code:
    <?php if (!defined('TL_ROOT')) die('You cannot access this file directly!');
    
    class GetGradeLength extends Frontend
    {
    	public function findgradelen(Widget $objWidget, $strForm, $arrForm)    {
    
                  /*  instead of using 'auto_form_19' you can assign an ID to your form so the code will be more readable */
                  if ($arrForm['formID'] != 'auto_form_19') {
                        return $objWidget;
                  }
    
                  // the field that will hold the result is named get_grade_length
    
                  if ($objWidget->name != 'get_grade_length') {
                          return;
                  }
    
                  // at this point we are in the right form and we are processing the right field
                   
                 $He = $this->Input->post('He');
                 $Ha = $this->Input->post('Ha'); 
                 // .... etc.etc.etc.
    
                 // ensure all required fields were filled
                 if (!empty($He) && !empty($Ha)) {
                        
                           $objWidget->value = .... your formula here ....;
                 }
    
                 return $objWidget;
             }
        }
    }
    ?>
    you can use the above code for all your forms that need to hold a calculated text field, be sure to check that are you working on the right field (the first two if) before calculate the result.

    hope this helps to get you started
    Consulenza Contao CMS https://www.intco.it

  5. #5
    New user
    Join Date
    09-19-10.
    Location
    Orlando, FL
    Posts
    11

    Default Re: Use of formulas in form generator(s)???

    Thanks a bunch!
    ... but is doesn't work. ops:

    The config.php and GetGradeLength.php are modified according to your proposals.
    Config.php:
    Code:
    <?php if (!defined('TL_ROOT')) die('You cannot access this file directly!');
    
    $GLOBALS['TL_HOOKS']['loadFormField'][] = array('GetGradeLength','findgradelen');
    ?>
    GetGradeLength.php:
    Code:
    <?php if (!defined('TL_ROOT')) die('You cannot access this file directly!');
    
    class GetGradeLength extends Frontend
    {
        public function findgradelen(Widget $objWidget, $strForm, $arrForm)
        {
        	if ($arrForm['formID'] != 'auto_find-grade-length') 
           {
                return $objWidget;
           }
           if ($objWidget->name != 'Di') 
           {
                return;
           }
           $He = $this->Input->post('He');   // End elevation of grade
           $Ha = $this->Input->post('Ha');   // Start elevation of grade
           $Sl = $this->Input->post('Sl');   // Slope in %
           $Di = $this->Input->post('Di');   // to be calculated : Length of grade
    
           /* In the test data entry form, the "result" filed has the value "Entered value" as default; could also be "0" */
           if (!empty($He) && !empty($Ha) && !empty($Di))
           {
              // Compute grade length:
              $objWidget->value = ($He - $Ha)*sqrt(1 + (10000/pow($Sl,2)));
    
              /* Test to see if the field is touched at all */
              //$objWidget->value = $objWidget->name;   // Should be "Di" !!!
           }
           return $objWidget;
        }
    }
    ?>
    In my results page, I use "text from session" as output method.
    I tried a default value for "Di" and no default value for "Di". Shouldn't make any difference, and it doesn't. It seems to me that the value for "Di" is not computed at all, since the result page shows the default value from the data entry form.
    It also doesn't make any difference whether I configure the output as text field or as text from session.
    I know the formula is correct which is proven in the work-around page.
    If you want to play around with this, go to the Tools->Grade calculator page (tabbed page). From this menu, you can also access the work-around pages (listed under the "Grade calculator" option)...

    This is driving me somewhat crazy...
    Take care,
    Erhard

  6. #6
    User
    Join Date
    06-19-09.
    Posts
    328

    Default Re: Use of formulas in form generator(s)???

    Quote Originally Posted by erhard
    In my results page, I use "text from session" as output method.
    sorry but I think you have not understood what I mean

    I am telling you to add a new textfield in your form (http://goo.gl/vwBvw)
    you form will be submitted on the same page

    the new textfield value will be initially empty, when user submit the form you will display the calculated value

    please note that in the code I posted there was an error

    Code:
                  if ($objWidget->name != 'get_grade_length') {
                          return;
                  }

    should be

    Code:
                  if ($objWidget->name != 'get_grade_length') {
                          return $objWidget;
                  }
    hope this is clear
    Consulenza Contao CMS https://www.intco.it

  7. #7
    New user
    Join Date
    09-19-10.
    Location
    Orlando, FL
    Posts
    11

    Default Re: Use of formulas in form generator(s)???

    I'm feeling sooooo dumb! I did quite some programming in php but despite that and your really great help I can't get it to function. Are you on Skype (sent you a PM)?

    1. I added the extra textfield in the data entry form
    2. corrected the code in GetGradeLength.php
    3. Data entry form is displayed on the same page after user submits
    4. Result - field (get_grade_length) still shows up empty which is the initial value

    Thank you very much for your time,
    Erhard

  8. #8
    User
    Join Date
    06-19-09.
    Posts
    328

    Default Re: Use of formulas in form generator(s)???

    Quote Originally Posted by erhard
    I'm feeling sooooo dumb! I did quite some programming in php but despite that and your really great help I can't get it to function. Are you on Skype (sent you a PM)?

    1. I added the extra textfield in the data entry form
    2. corrected the code in GetGradeLength.php
    3. Data entry form is displayed on the same page after user submits
    4. Result - field (get_grade_length) still shows up empty which is the initial value

    Thank you very much for your time,
    Erhard
    I made a simple test page following the above steps. Here is the result:

    http://demo.zedseries.com/others/210/

    the above result field is calculated with the loadFormField hook

    I did exactly what I told you
    Consulenza Contao CMS https://www.intco.it

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •