chaiwei Posted February 21, 2009 Share Posted February 21, 2009 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 Quote Link to comment Share on other sites More sharing options...
chaiwei Posted February 21, 2009 Author Share Posted February 21, 2009 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. Quote Link to comment Share on other sites More sharing options...
chriso20 Posted February 21, 2009 Share Posted February 21, 2009 <?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 Quote Link to comment Share on other sites More sharing options...
chriso20 Posted February 21, 2009 Share Posted February 21, 2009 <?php $basic=1200; $leave=3; $formula = "$basic/26*$leave"; eval("\$formula=$formula;"); echo round($formula,2); ?> obviously you set $formula from the database... Quote Link to comment Share on other sites More sharing options...
chaiwei Posted February 23, 2009 Author Share Posted February 23, 2009 Thanks again. It works. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.