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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.