0

我从表单字段中获得以下信息:

只有一次计算公式

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

以及包含此信息的七个文本(输入)字段:

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

根据此信息,我希望将此结果作为计算文本:

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

我的实验如下所示:

$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);
    }

输出看起来像这样。:-(

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

3 回答 3

0

假设letter是表单字段的名称:

$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);
于 2017-01-20T05:08:23.013 回答
0

使用 $_POST 方法获取表单数据,将它们存储在单独的变量中,然后使用如下所示的公式计算结果:

$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);
于 2017-01-20T05:19:09.433 回答
0

尝试这个:

<?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);
于 2017-01-20T05:19:35.073 回答