Jump to content

[SOLVED] Help..How to do the calculation using formula that stored in database


chaiwei

Recommended Posts

Hi all,

 

Let say i got stored the formula in database.

eg.

$basic/26*$leave
$basic+$basic

 

The fomula is user to key in whatever it is. but the variable must be only $basic and $leave.

 

let say I got another salary table and leave table.

 

and I retrieve the salary and leave has been taken.

 

So How I gonna use the formula to calculate the amount.

 

SELECT Formula from formula where company_id='1';
$rs=mysql_fetch_array(...);
$formula=$rs['formula'];            //$basic/26*$leave

SELECT Salary from Salary where emp_id='2';
$rs1=mysql_fetch_array(...);
$basic=$rs1['Salary'];        //1200

SELECT leave_taken from formula where emp_id='2';
$rs=mysql_fetch_array(...);
$leave=$rs['leave_taken'];  //3

$amount=round($formula,2); //coz formula is $basic/26*$leave so it suppose 1200/26*3
echo $amount;    //but  it can't get the amount, it display the string 

 

Thanks in advance

Hi all,

 

I found this.

 

<?
$basic='1200';
$leave='3';
//formula = $basic/26*$leave
parse_str($formula);
echo $basic;  //1200
echo $leave;  //3
$a=str_ireplace('$basic',$basic,$payment_amount);
$b=str_ireplace('$leave',$leave,$a);
echo $b; //  1200/26*3
?>

 

I want to get the 138.46 not the 1200/26*3

i have try ($b*1) but it returns 1200 only.

<?php

$a = 1;

$b = 2;

$str = "$a+$b";

eval("\$str = $str;");

echo $str; // output: 3

?>

 

so you could change this code to be:

 

$amount=round(eval("\$formula=$formula"),2);

echo $amount;   

 

(i think...)

 

have a play with it ;)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.