need to calculate value for dynamic formulas,

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • madhanrajesh210002
    New Member
    • Aug 2010
    • 6

    need to calculate value for dynamic formulas,

    I'm trying to calculate dynamic formula value for dynamic inputs, that is user inputs and formulla all will be given by user, i tried in php, but failed , codes as follows,
    Code:
    					$a=10; //$a - integer value user input - dynamic
    					$b=20; //$b - integer value user input - dynamic
    					echo $formulla1= ('$a+$b+11');  // formulla so, string value - user input - dynamic
    					// out put is $a+$b+11... we need to show - 41
    					echo "<br>";
    					echo $formulla2 = "$a+$b";    // formulla so, string value - user input - dynamic
    					// out put is 10+20... we need to show - 30
    then i used java script to that formula calculation, it works quite, but it return string output so that i can't use that for further calculation, or i need to use java script whole project, its very difficult,
    and not even can not convert the output as integer, if convert its value only zero, java script used code as follows,
    Code:
    					echo "<br>";
    				    echo $formulla3= "<script type=\"text/javascript\">"."document.write(j=eval ($formulla2));"."</script>"; 
    					//output is 30
    					echo "<br>";
    					echo gettype ($formulla3);  // now $formulla3 is a string
    so, requesting all to solve the prob in php with out-using javascript,
    or how to conver the value as pure-integer,
    Expecting sollution asap.

    Thanks
  • pradeepkr13
    New Member
    • Aug 2010
    • 43

    #2
    As far as I understood you are saying that formula as well as variables are provided by the user. And you have to just calculate as per the these two inputs.

    Code:
    $sumt=0; 
    $formula = "";
    if(isset($_REQUEST['formula']))
       $formula = $_REQUEST['formula']; 
    $var_a = 0;
    if(isset($_REQUEST['a']))
       $var_a   = $_REQUEST['a'];
    $var_b = 0;
    if(isset($_REQUEST['b']))
       $var_b   = $_REQUEST['b']; 
    
    if($formula == '$a+$b+11')
      $sum = $var_a + $var_b + 11;
    elseif($formula == '$a+$b')
      $sum = $var_a + $var_b;
    else
      echo 'Wrong input';
    
    echo $sum;

    Comment

    • kovik
      Recognized Expert Top Contributor
      • Jun 2007
      • 1044

      #3
      Basically, change this line:

      Code:
      echo $formulla2 = "$a+$b";
      Into this:

      Code:
      echo $formulla2 = $a+$b;

      Everything else that you are trying to accomplish seems... Vague. But, if I understand what you are saying, you may want to make use of str_replace(), sprintf(), and eval() on the user's provided formula.

      And if you decide to use eval(), please please validate user input.

      Comment

      • madhanrajesh210002
        New Member
        • Aug 2010
        • 6

        #4
        need to calculate value for dynamic formulas,

        thankyou, mr. pradeepkr13,

        but the only big, problem in my end is to calculate value for dynamic formula with out limitation, they can provide 'n' no of type formullas.

        the only thing, formula in string format, as seen in thread, i cannt solve it in php, 1. how to solve it
        or if i use java script, its working cool but, the result also string, i cannot use it for further,
        2. how to convert/use the same thing for further calculation.

        kindly provide a solution.
        thanks

        Comment

        • kovik
          Recognized Expert Top Contributor
          • Jun 2007
          • 1044

          #5
          I already told you:

          Originally posted by kovik
          [...] you may want to make use of str_replace(), sprintf(), and eval() on the user's provided formula.
          But, if you insist on doing it in JavaScript, make sure that if you don't want a string, then don't return a string. Every string that you have should be converted to a number via parseInt() or parseFloat(). THEN perform the additions.

          Comment

          • pradeepkr13
            New Member
            • Aug 2010
            • 43

            #6
            If you allow users to input any formula and you execute eval (or other function) then that would be a security issue.
            User can pass any thing in the input and break your code or extract important info.

            Comment

            Working...