0

I get from the form field the following information:

Only once the formula for the calculation

(z + a * (s-k)) * d + (m * x)

And seven text (input) fields with this information:

value => 0.5
name => a

value => 10.2
name => z

value => 4
name => x

value => 2
name => s

value => 2
name => d

value => 0.4
name => k

value => 2.1
name => m

From this information I would like this result as a text for the calculation:

(10.2 + 0.5 * (2-0.4)) * 2+ (2.1 * 4)

My experiment looks like this:

$sFields => stdClass Object (

    [price] => 0.5
    [letter] => a

    [price] => 10.2
    [letter] => z

    [price] => 4
    [letter] => x

    [price] => 2
    [letter] => s

    [price] => 2
    [letter] => d

    [price] => 0.4
    [letter] => k

    [price] => 2.1
    [letter] => m
)

$sFormula = $this->formRequestParameter("(z + a * (s-k)) * d + (m * x)");

    protected function _calculateFormFields($sFields, $sFormula)
    {

        $formulaString = preg_replace("/[^a-zA-Z0-9]+/", ',', strtolower($sFormula));

        $exLetter = explode(',', $formulaString);

        $sPrice = null;

        foreach ($exLetter as $sKey => $sVal) {

            if (strtolower($sVal) == strtolower($sFields->letter)) {

                $sPrice[$sKey] = $sFields->price;

            }
        }

        $buildFormula = str_replace(

            $exLetter,
            $sPrice,
            $sFormula
        );

        print_r($buildFormula);

        // $this->_calculatePrice($buildFormula);
    }

And the output looks like this. :-(

10.2 (+*(-))*+(*) 
4 (+*(-))*+(*) 
2.1 (+*(-))*+(*) 
2 (+*(-))*+(*) 
2 (+*(-))*+(*)  
0.5 (+*(-))*+(*)  
0.4 (+*(-))*+(*)
od_
  • 183
  • 3
  • 16
  • $result = ($_POST['z'] + $_POST['a'] * ($_POST['s']-$_POST['k'])) * $_POST['d'] + ($_POST['m'] * $_POST['x']); – Maths RkBala Jan 20 '17 at 05:08
  • [How to make a calculator in PHP](http://stackoverflow.com/questions/12692727/how-to-make-a-calculator-in-php) – Mark Baker Jan 20 '17 at 13:27
  • This is not the calculation. Only letters with the correct numbers are to be exchanged. – od_ Jan 20 '17 at 13:29

3 Answers3

0

Assuming letter is the name of the form fields :

$a = $_POST['a'];
$z = $_POST['z'];
$x = $_POST['x'];
$s = $_POST['a'];
$d = $_POST['d'];
$k = $_POST['k'];
$m = $_POST['m'];

$result = ($z + $a * ($s-$k)) * $d + ($m * $x);
Ahmad Mobaraki
  • 7,426
  • 5
  • 48
  • 69
  • not $_POST['A']; it should be $_POST['a']; you should not capitalize the name fields – NID Jan 20 '17 at 05:10
  • Thank you Ahmad. Calculation formula is variable, see my updated code. That would not work. – od_ Jan 20 '17 at 05:20
  • @anonym you dont want the final results!? do you want the string representing the formula? what is `$field` and `$formula` variablae? how do you use the `_calculateFormFields` function ? – Ahmad Mobaraki Jan 20 '17 at 05:30
  • Ahmad, a calculation function I've already written. I've posted the output, see above. First, the sequence of numbers is not correct and second, all operators are written by numbers. – od_ Jan 20 '17 at 05:40
  • @anonym show me `$field` and `$sFormula` , before using them in your function – Ahmad Mobaraki Jan 20 '17 at 05:51
0

Get the form data using $_POST method, store them in separate variables and then calculate the result using the formula as shown in here:

$a = $_POST['a'];
$z = $_POST['z'];
$x = $_POST['x'];
$s = $_POST['s'];
$d = $_POST['d'];
$k = $_POST['k'];
$m = $_POST['m'];

$result = ($z + $a * ($s-$k)) * $d + ($m * $x);
Sakibur Rahman
  • 834
  • 3
  • 10
  • 26
0

Try this:

<?php

$post_array = ['a' => 0.5,'z' => 10.2,'x' => 4,'s' => 2,'d' => 2,'k' => 0.4,'m' => 2.1]; // $post_array = $_POST; 

extract($post_array); // or extract($_POST)

echo ($z + $a * ($s-$k)) * $d + ($m * $x);
mith
  • 1,680
  • 1
  • 10
  • 12