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 Link to comment https://forums.phpfreaks.com/topic/146209-solved-helphow-to-do-the-calculation-using-formula-that-stored-in-database/ 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. Link to comment https://forums.phpfreaks.com/topic/146209-solved-helphow-to-do-the-calculation-using-formula-that-stored-in-database/#findComment-767627 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 Link to comment https://forums.phpfreaks.com/topic/146209-solved-helphow-to-do-the-calculation-using-formula-that-stored-in-database/#findComment-767630 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... Link to comment https://forums.phpfreaks.com/topic/146209-solved-helphow-to-do-the-calculation-using-formula-that-stored-in-database/#findComment-767637 Share on other sites More sharing options...
chaiwei Posted February 23, 2009 Author Share Posted February 23, 2009 Thanks again. It works. Link to comment https://forums.phpfreaks.com/topic/146209-solved-helphow-to-do-the-calculation-using-formula-that-stored-in-database/#findComment-768827 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.