0

Hi I am having an issue with deciding how to store/calculate a value. The value is this case is dynamically generated by executing a javascript function that is stored in a mysql database.

The company I work for originally did this because other values that are used by the stored javascript function can be changed and because this is done by a AJAX request, the value needs to be calculated on the clients side. Somehow this doesn't feel right, and I think there has to be a better way to store this.

Now I am not sure how I should solve this, but I hope some of you can point me in the right direction.

I hope I explained the problem clear enough.

EDIT: An example. There is a value x that needs to be calculated and currently this happens.

function example(){
    if(y == true){
       return 0;
    }
    var result = z / areaM2;
    if(result > 100){
        return 1;
    }
    else{
        return 2;
    }
}
x = example();

y, z and areaM2 are actual stored variables.

1 Answer 1

1

You should only save the values that can change, not the function itself, even if it could change. In the latter case you'd need to provide multiple functions in a JavaScript file and have a property stored in your DB to define which function to use.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for thinking along with me. But with this solution it is still not possible to export the data to another non-javascript format (XML or EXCEL) is there a way to tell PHP to calculate a value? And preferable not by hard-coding all the if-statements that determin a calculated value.
Can the calculation function change?
No, it is made only once. But for each case there is a different function.
What do you mean with for each case?
There are multiple variables that need to be calculated. All by their own function. I meant one variable: one case. sorry for the confusion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.