Page 1 of 1

Convert Equation from String to .....

Posted: Mon Aug 13, 2007 3:26 am
by the_last_tamurai
Hi Everybody,
I've a problem in my code...with no long intro
Suppose I've an array contains Equations to be shown Like this one

Code: Select all

// Some query from DB

$array = array ( 
"Equation 1" => "field11,+,field22",
"Equation 2"=> "(,field11,+,field22,),/,field33"
);
I make this to substitute the fieldxx in the query result and make my calculations
after that I loop in the array and explode the formulas to get the results, unfortunately it's a string and I get something like this
Equation 1 = 52.02+9898
Equation 2 = (52.02+9898)/23
How can I make this work???? And get the result of the equations
I think the logic is bad but I need this work ASAP

Posted: Mon Aug 13, 2007 3:48 am
by Chris Corbyn
You can build the equation as a string and then eval() it, but I'd seriously advise against it. MySQL has a complete set of mathematical functions so I'd try using them:

Code: Select all

select ((field11 + field22)/field33) as Equation2 from tbl where id = :something

Posted: Mon Aug 13, 2007 4:05 am
by the_last_tamurai
Thanx d11wtq,but database solution won't help my code,because I have a big array and it's not logic to make a query for every eqation.
When using eval() it returns a parse error, NOTE that the string is like that :
"(52.02+9898)/23"
and the error is :
Parse error:
syntax error, unexpected $end in xxxxxx\xxx.php(13) : eval()'d code on line 1

Posted: Mon Aug 13, 2007 5:53 am
by superdezign
the_last_tamurai wrote:Parse error:
syntax error, unexpected $end in xxxxxx\xxx.php(13) : eval()'d code on line 1
Eval'd statements need to be properly ended with a semicolon.