Convert Equation from String to .....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
the_last_tamurai
Forum Commoner
Posts: 87
Joined: Wed Feb 28, 2007 8:24 am
Location: cairo
Contact:

Convert Equation from String to .....

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
User avatar
the_last_tamurai
Forum Commoner
Posts: 87
Joined: Wed Feb 28, 2007 8:24 am
Location: cairo
Contact:

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
Post Reply