2

I have a MySQL database where part of it handles instrument’s depth of water. Each instrument has its own formula of calculation how depth the water when the operator collect the reading I stored the formula for each instrument in database/MySQL.

Example formula: [55-57]

This is a simple minus operation, where the number is actually represent the id of a row.

How do I represent those number with id of a row and later convert it to JavaScript readable code?

I simply want to do keyup event where every time user key in something into text field then the other part of HTML would reflect changes based on formula that I fetched from database.

This is simple function that I have right now:

inputListened: function (e) {
    var equation = $('input[data-equation]').data('equation');
    if (!_(equation).isBlank())
        console.log(equation);
}

the console would simply output the formula from database but I don't know how to represent number with other variable

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
Muhaimin
  • 1,643
  • 2
  • 24
  • 48

5 Answers5

3

If you have the options to create two database columns, I would create one column with "params" and one with the formula. Then you can use PHP to create a javascript function you can use.

EG:

<?php
$params = "x, y"; // get from mysql
$formula = "x-y"; //get from mysql

?>
function onKeyUp(<?php echo $params ?>) {
  return <?php echo $formula ?>
}
<?

Then you can directly call that method on any event.

But if you need to pass in the input fields by ID, you could instead use the params column to store the ID's of the input fields.

EG:

<?php
$params = "xVal, yVal"; // get from mysql
$formula = "xVal-yVal"; //get from mysql

$fields = explode(",", $params); //To make it better, store them in a seperate table so you dont need to explode

echo "function onKeyUp() {" . chr(10); //chr(10) = new line for readable output

$l =count($fields);
for ($x=0; $x<$l;$x++) {

  //store the value of the id in a var with the same name
  //make sure you only use IDS that are valid varnames!
  echo "var " .$fields[$x] . " = getElementById('".$fields[$x] ."').value;" . chr(10);

}

echo "return " .  $formula . chr(10) . " }";
?>
Hugo Delsing
  • 13,803
  • 5
  • 45
  • 72
  • And what if `$formula` is not a one-liner? Wrap in anonymous function? – ElmoVanKielmo Jun 11 '14 at 06:56
  • actually I dont have control on database. The formula is just plain like I describe above. – Muhaimin Jun 11 '14 at 06:57
  • 1
    If your formula has no distinct params, your out of luck. How would we know if 55 should be a variable or is a constant? And how should we determine where the input val would come from and which number is which? – Hugo Delsing Jun 11 '14 at 07:03
  • that's the problem. First I thought it would be simple like we use template engine like smarty or underscorejs – Muhaimin Jun 11 '14 at 07:19
  • Everything is possible, but it requires a lot of insight in all the formulas and how you want to get input. Basically its a project on its own to do this. Not something for SO. – Hugo Delsing Jun 11 '14 at 07:26
  • all formulas are simple one. minus, add, times and divide only – Muhaimin Jun 11 '14 at 07:33
  • 1
    Then I guess you could check out [Extract numbers from string](http://stackoverflow.com/q/6278296/434949) to get all the numbers. And then exchange the numbers for variables. – Hugo Delsing Jun 11 '14 at 10:12
  • do I have to open another question for this. I have successfully extract the numbers, but how the operator – Muhaimin Jun 14 '14 at 05:02
  • Since you put a bounty, share your code to extract the numbers. Perhaps then somebody can help – Hugo Delsing Jun 16 '14 at 06:40
  • @MuhaiminAbdul > You dont realy need the operator. If you change the numbers with values, you can execute the function with the operators in the correct place. But if you realy need the operators, you could replace all numbers with spaces. Then you can explode/split on it and you have an array with all the operators – Hugo Delsing Jun 18 '14 at 20:32
1

You can use eval function in both php and JavaScript after replacing id values with the quantities you want to use.

For replacement you can use preg_replace in php and replace method in JavaScript

itzmukeshy7
  • 2,669
  • 1
  • 21
  • 29
1

The Problem:

As far as I have understood from your question and comments, you want to perform basic mathematics operations on two values. Values and formula is stored in a database and on front end, it is required to calculate final values based on the formula.

Solution:

Following are the things I suggest should be updated/modified in order for achieving the desired output:

  • Change database column structure: Separate the values fields and operators field, there can be multiple methods for this depending upon how much extensive/scale-able solution is required. Example, most simplest can be to have different columns for value1, value2, operator.

  • AJAXIFY: If you want to return result from the database on keyup (or any other front end method), then you have to send value/id to a php script (which derives the data from database parse it and sends data back to your application view) using an AJAX request.

Qarib Haider
  • 4,796
  • 5
  • 27
  • 38
1

First I don't have control over database. That means I can't simply change DB structure. So here how I achieved to execute / eval formula at front end

var equation = $('input[data-equation]').data('equation'),
    result = eval( equation.replace( /\w+/g, function( res ){
        return +$('#'+ res).val();
    }));

I have this answer based on Zyklus's solution. I have slightly modified the code to suit with jQuery. Hope this will help others. Thank you all

Community
  • 1
  • 1
Muhaimin
  • 1,643
  • 2
  • 24
  • 48
0

I've created a simple formula parsing engine called mathy.js which would do this:

inputListened: function (e) {
    var equation = $('input[data-equation]').data('equation');
    if (!_(equation).isBlank()) {
        var engine = mathy.Engine({ name: 'formula', derivation: equation });
        var result = engine.process();

        console.log(result[0]);
    }
}
Aaron Powell
  • 24,927
  • 18
  • 98
  • 150
  • thanks for your answer but i dont see you refer to any element just like i did with `$('#' + res).val()` – Muhaimin Oct 14 '14 at 14:04