kkarumu
asked on
How to use a formula stored in mysql, eks: (A + B + C)/(E + F)
How to use several formulas stored in a db?
I have several formulas, it's basic math operations, (AB + AC + AD + AE)/7 etc, the AB' is data has to replaced by values stored in different tables.
I tried to set a rule that each value has to be stored with a space and then split the function up in to a array and the use a loop and then create something like the c++ postfix expression and get the end value,
maybe this can be done easier? I don't like that the formula has to be entered with spaces, it could easily become problem.. I guess that the space thing can be solved by parsing it with regular expressions,
I have tried a bit, here's the start of that code:
sample Formula:( ( A1 + A2 ) + A3 + A4 + A5 + A6 + A7 )
$seperators = array('A1', 'A2','A3','A4','A5','A6', 'A7');
//just replacing the string with a sample number: 1
$str = str_replace($seperators,'1 ',$str);
$str = (preg_split('# #',$str));
for($i=0; $i<count($str); $i++){
if( $str[$i] == '+' || $str[$i] == '-' || $str[$i] == '*' || $str[$i] == '/' ){
//do something
}else if( $str[$i] == ')' ){
//do something
}else
//do something
}
I have several formulas, it's basic math operations, (AB + AC + AD + AE)/7 etc, the AB' is data has to replaced by values stored in different tables.
I tried to set a rule that each value has to be stored with a space and then split the function up in to a array and the use a loop and then create something like the c++ postfix expression and get the end value,
maybe this can be done easier? I don't like that the formula has to be entered with spaces, it could easily become problem.. I guess that the space thing can be solved by parsing it with regular expressions,
I have tried a bit, here's the start of that code:
sample Formula:( ( A1 + A2 ) + A3 + A4 + A5 + A6 + A7 )
$seperators = array('A1', 'A2','A3','A4','A5','A6', 'A7');
//just replacing the string with a sample number: 1
$str = str_replace($seperators,'1
$str = (preg_split('# #',$str));
for($i=0; $i<count($str); $i++){
if( $str[$i] == '+' || $str[$i] == '-' || $str[$i] == '*' || $str[$i] == '/' ){
//do something
}else if( $str[$i] == ')' ){
//do something
}else
//do something
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I'd suggest using preg_replace() instead of str_replace() so you could build a regexp like, perhaps '/\bA1\b/'
Perhaps
$expression = preg_replace('/\b' . $var . '\b/', $value, $expression);
Here note that \b is a "word boundary" in a regular expression, so "A1" followed by another "1" will not be considered an entire word, and will not get matched. A1 followed by a + or a space or any other non-word character will get replaced.
Perhaps
$expression = preg_replace('/\b' . $var . '\b/', $value, $expression);
Here note that \b is a "word boundary" in a regular expression, so "A1" followed by another "1" will not be considered an entire word, and will not get matched. A1 followed by a + or a space or any other non-word character will get replaced.
ASKER
That worked! thanx.
Is it possible to expand the formula with >< (bigger or smaller)?
Is it possible to expand the formula with >< (bigger or smaller)?
Absolutely; that's why I suggested eval() -- it'll run absolutely any valid PHP code.
ASKER
Is it ok to use eval?
have problem as you said to se the differen between A1 and A11..