Graxeon Posted January 18, 2010 Share Posted January 18, 2010 I have a math equation that calculates profit. What I don't know how to implement is "times." Times is how many times you repeat the equation. Each time you repeat it though, it uses the new TOTAL cash. So let's say I had this: equation.php?cash=100&cost=20&profit=3×=7 It should calculate this: 100/20 = 5 5*3 = 15 (now it calculates the "times") 100+15 = 115 115/20 = 5.75 5.75*3 = 17.25 ("times" #3) 115+17.25 = 132.25 132.25/20 = 6.6125 6.6125*3 = 19.8375 (then keep repeating until you get to "×=7") <?php $cash = $_GET['cash']; $cost = $_GET['cost']; $profit = $_GET['profit']; $times = $_GET['times']; $equation = $cash / $cost * $profit; //I don't know how to implement $times echo $equation; ?> Quote Link to comment https://forums.phpfreaks.com/topic/188910-math-equation/ Share on other sites More sharing options...
Buddski Posted January 18, 2010 Share Posted January 18, 2010 $cash = $_GET['cash']; $cost = $_GET['cost']; $profit = $_GET['profit']; $times = $_GET['times']; $equation = 0; for ($i=0;$i<$times;$i++) { $equation += ($cash / $cost * $profit); } echo $equation; Im assuming you want a complete total with this script Quote Link to comment https://forums.phpfreaks.com/topic/188910-math-equation/#findComment-997420 Share on other sites More sharing options...
JonnoTheDev Posted January 18, 2010 Share Posted January 18, 2010 To have something iterate x number of times you use a loop construct i.e <?php $numberOfTimes = 3; for($x = 1; $x <= $numberOfTimes; $x++) { print "loop number ".$x."<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/188910-math-equation/#findComment-997422 Share on other sites More sharing options...
Graxeon Posted January 18, 2010 Author Share Posted January 18, 2010 @Budd That gave me "105" as the answer. The answer should be well over 132 @neil I'm sorry, you lost me xD (though that's like almost the same thing as Budd's code) Quote Link to comment https://forums.phpfreaks.com/topic/188910-math-equation/#findComment-997425 Share on other sites More sharing options...
Buddski Posted January 18, 2010 Share Posted January 18, 2010 He was just giving you a run down of how to do a for loop.. Thats all. $cash = $_GET['cash']; $cost = $_GET['cost']; $profit = $_GET['profit']; $times = $_GET['times']; for ($i=0;$i<$times;$i++) { $cash += ($cash / $cost * $profit); } echo $cash; I think is correct now.. I didnt read your post properly.. (Its 3am) Quote Link to comment https://forums.phpfreaks.com/topic/188910-math-equation/#findComment-997426 Share on other sites More sharing options...
Graxeon Posted January 18, 2010 Author Share Posted January 18, 2010 Thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/188910-math-equation/#findComment-997427 Share on other sites More sharing options...
Graxeon Posted January 18, 2010 Author Share Posted January 18, 2010 Oh just a quick question: Is there any way to add commas? The number is hard to read if it goes into the millions. So instead of displaying "158782393.387839" it'll display it like "158,782,393.387839" Quote Link to comment https://forums.phpfreaks.com/topic/188910-math-equation/#findComment-997431 Share on other sites More sharing options...
Buddski Posted January 18, 2010 Share Posted January 18, 2010 I dont know how it will go with millions but.. echo number_format($cash,2,'.',','); Quote Link to comment https://forums.phpfreaks.com/topic/188910-math-equation/#findComment-997434 Share on other sites More sharing options...
Graxeon Posted January 18, 2010 Author Share Posted January 18, 2010 Perfect, thank you Quote Link to comment https://forums.phpfreaks.com/topic/188910-math-equation/#findComment-997436 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.