我从表单字段中获取以下信息:
只有一次计算公式
(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 (+*(-))*+(*)
答案 0 :(得分: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);
答案 1 :(得分: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);
答案 2 :(得分: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);