mattafixed Posted May 20, 2009 Share Posted May 20, 2009 Hello to all, I hope I am not repeating a topic, couldn't find anything here. I am pretty new to php, and have googled the basic calculator functions but im stuck on something. I need to make a calc with: Inputs on form: D10, D15, D12, D20, D17 Press "Calculate" button to calculate this formula D10/(D17*D15*D12*D20*4) and display the answer. Is this possible with php? Any help is greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/158897-php-calculator-with-brackets/ Share on other sites More sharing options...
trq Posted May 20, 2009 Share Posted May 20, 2009 Is this possible with php? Yes. You just did it. You might take a look at the arithmetic operators. Quote Link to comment https://forums.phpfreaks.com/topic/158897-php-calculator-with-brackets/#findComment-838024 Share on other sites More sharing options...
Mark Baker Posted May 20, 2009 Share Posted May 20, 2009 If the inputs are worksheet cells, you might want to take a look at PHPExcel which has a built in calculation engine. Quote Link to comment https://forums.phpfreaks.com/topic/158897-php-calculator-with-brackets/#findComment-838029 Share on other sites More sharing options...
mattafixed Posted May 20, 2009 Author Share Posted May 20, 2009 So something like this: ? Html form code: <form action="calc.php" method="post"> D10: <input type="text" name="d10" size="10"> D15: <input type="text" name="d15" size="10"> D12: <input type="text" name="d12" size="10"> D20: <input type="text" name="d20" size="10"> D17: <input type="text" name="d17" size="10"> <input type="submit" value="Calculate"> <input type="reset" value="Clear"> </form> php code: $d4 = 4; if($calc != null) { switch($calc) { $result= $d10 / ( $d17 * $d15 *$d12* $d20*d4; )break; echo("Calculation result: $result"); } Quote Link to comment https://forums.phpfreaks.com/topic/158897-php-calculator-with-brackets/#findComment-838039 Share on other sites More sharing options...
Daniel0 Posted May 20, 2009 Share Posted May 20, 2009 Except it should be $_POST['d10'] etc. Why don't you just try it out? Then you'll see if it works or not... Quote Link to comment https://forums.phpfreaks.com/topic/158897-php-calculator-with-brackets/#findComment-838041 Share on other sites More sharing options...
mattafixed Posted May 20, 2009 Author Share Posted May 20, 2009 If the inputs are worksheet cells, you might want to take a look at PHPExcel which has a built in calculation engine. Thanks, you are right, excel Quote Link to comment https://forums.phpfreaks.com/topic/158897-php-calculator-with-brackets/#findComment-838042 Share on other sites More sharing options...
mattafixed Posted May 20, 2009 Author Share Posted May 20, 2009 I can't figure this out, can anyone show me how? Quote Link to comment https://forums.phpfreaks.com/topic/158897-php-calculator-with-brackets/#findComment-838227 Share on other sites More sharing options...
Mark Baker Posted May 20, 2009 Share Posted May 20, 2009 I can't figure this out, can anyone show me how?Not without a bit more information about what exactly you are trying to do, what you've managed so far, and what exact problems you have. Quote Link to comment https://forums.phpfreaks.com/topic/158897-php-calculator-with-brackets/#findComment-838282 Share on other sites More sharing options...
mattafixed Posted May 20, 2009 Author Share Posted May 20, 2009 Well what I would like to do is to make a simple php calculator: Number Inputs on form: D10, D15, D12, D20, D17 Press "Calculate" button to calculate this formula: D10/(D17*D15*D12*D20*4) Then display the answer. Thats all. Quote Link to comment https://forums.phpfreaks.com/topic/158897-php-calculator-with-brackets/#findComment-838295 Share on other sites More sharing options...
Mark Baker Posted May 21, 2009 Share Posted May 21, 2009 To start with, do you even have an html form for entering the values and formula? Quote Link to comment https://forums.phpfreaks.com/topic/158897-php-calculator-with-brackets/#findComment-838734 Share on other sites More sharing options...
Mark Baker Posted May 21, 2009 Share Posted May 21, 2009 /** Include path **/ set_include_path(get_include_path() . PATH_SEPARATOR . 'PHPExcel/Classes/'); /** Set initial values **/ $D10 = (isset($_POST['D10'])) ? $_POST['D10'] : ''; $D15 = (isset($_POST['D15'])) ? $_POST['D15'] : ''; $D12 = (isset($_POST['D12'])) ? $_POST['D12'] : ''; $D20 = (isset($_POST['D20'])) ? $_POST['D20'] : ''; $D17 = (isset($_POST['D17'])) ? $_POST['D17'] : ''; $formula = (isset($_POST['formula'])) ? $_POST['formula'] : ''; /** Display the Form **/ ?> <FORM ACTION="calculator.php" METHOD="POST"> <B>D10</B> <INPUT NAME="D10" TYPE="text" VALUE="<?php echo $D10; ?>"><BR /> <B>D15</B> <INPUT NAME="D15" TYPE="text" VALUE="<?php echo $D15; ?>"><BR /> <B>D12</B> <INPUT NAME="D12" TYPE="text" VALUE="<?php echo $D12; ?>"><BR /> <B>D20</B> <INPUT NAME="D20" TYPE="text" VALUE="<?php echo $D20; ?>"><BR /> <B>D17</B> <INPUT NAME="D17" TYPE="text" VALUE="<?php echo $D17; ?>"><BR /> <B>Formula</B> <INPUT NAME="formula" TYPE="text" VALUE="<?php echo $formula; ?>"> <INPUT NAME="submit" TYPE="submit" VALUE="calculate"> </FORM> <?php /** If the user has submitted the form: **/ if (isset($_POST['submit'])) { /** Include PHPExcel to perform the calculations **/ include('PHPExcel.php'); /** Instantiate a new workbook in memory **/ $objPHPExcel = new PHPExcel(); /** Set cells to the values input by the user **/ /** Note that we'll put our formula in cell A1, with an = sign (exactly as would be done in Excel itself) **/ $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('D10', $D10) ->setCellValue('D15', $D15) ->setCellValue('D12', $D12) ->setCellValue('D20', $D20) ->setCellValue('D17', $D17) ->setCellValue('A1', '='.$formula); /** Get the calculated value for cell A1 into $result **/ $result = $objPHPExcel->getActiveSheet()->getCell('A1')->getCalculatedValue(); /** Display the calculated $result **/ echo $result; } Quote Link to comment https://forums.phpfreaks.com/topic/158897-php-calculator-with-brackets/#findComment-838738 Share on other sites More sharing options...
vovkind Posted September 12, 2009 Share Posted September 12, 2009 Hi,peoples! I just wrote a simple calc. with brackets.Using/operations you can to see if you rollover a help picture in a main page of my site. http://www.zetta.co.il/entry.php Quote Link to comment https://forums.phpfreaks.com/topic/158897-php-calculator-with-brackets/#findComment-917374 Share on other sites More sharing options...
vovkind Posted October 29, 2009 Share Posted October 29, 2009 excuse me, i changed link to calculator:now it's http://www.zetta.co.il/entry.html Quote Link to comment https://forums.phpfreaks.com/topic/158897-php-calculator-with-brackets/#findComment-946988 Share on other sites More sharing options...
Mark Baker Posted October 29, 2009 Share Posted October 29, 2009 Manually entered in the calc field, rather than using your buttons Input expression: (2 + 3) * 4 Not numeric value.Operation had been terminated Suggest you do something to ignore spaces in a formula (2+-3)*4 results in a broken page. Can't it handle negative numbers? Quote Link to comment https://forums.phpfreaks.com/topic/158897-php-calculator-with-brackets/#findComment-947011 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.