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
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
Share Improve this question edited Jun 14, 2014 at 5:13 Giao1968 25.9k11 gold badges76 silver badges105 bronze badges asked Jun 11, 2014 at 6:46 MuhaiminMuhaimin 1,6432 gold badges26 silver badges48 bronze badges 1- Make sure to add a minimal set of code. – user924016 Commented Jun 11, 2014 at 6:49
5 Answers
Reset to default 3If 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) . " }";
?>
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
The Problem:
As far as I have understood from your question and ments, 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.
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
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]);
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745257671a4619052.html
评论列表(0条)