Jump to content

Php calculator with brackets


mattafixed

Recommended Posts

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.

 

 

Link to comment
Share on other sites

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");
        }

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

/**	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;
}

Link to comment
Share on other sites

  • 3 months later...
  • 1 month later...

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?

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.