Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to use a formula stored in mysql, eks: (A + B + C)/(E + F)

Posted on 2006-11-04
5
Medium Priority
?
158 Views
Last Modified: 2010-04-06
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
    }
0
Comment
Question by:kkarumu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 19

Accepted Solution

by:
VoteyDisciple earned 750 total points
ID: 17874328
I'm wary of trying to parse expressions by hand, when expressions like that are Context-Free (so a regular expression is insufficient to parse them).  Here's one approach that may be valid if you can carefully control formulas as they're entered:

$variables = array('A1' => 3, 'A2' => 3748.12, ....);
$expression = '((A1 +

foreach($variables as $var => $value) {
     str_replace($var, $value, $expression);
}
eval('$result = ' . $expression);
echo $result;


Instead of str_replace() you'd really have to do a preg_replace() to make sure you don't accidentally fill in the value for A1 in place of A11 and A111 and A1111 and leave a whole huge mess, but the principle is sound.

It's also a nasty way to expose the system if you allow through a formula that actually contains PHP code.  


Just an "out of the box" line of thinking to consider.
0
 
LVL 1

Author Comment

by:kkarumu
ID: 17876885
ok, thanx, that works,

Is it ok to use eval?

have problem as you said to se the differen between A1 and A11..
0
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17876902
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.
0
 
LVL 1

Author Comment

by:kkarumu
ID: 17877441
That worked! thanx.

Is it possible to expand the formula with >< (bigger or smaller)?
0
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17877661
Absolutely; that's why I suggested eval() -- it'll run absolutely any valid PHP code.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question