PDA

View Full Version : PHP Calculator Help



xxlegitxx
11-28-2006, 09:38 PM
First post in the forum...scary.
I'm having trouble with my PHP calculator. Heres the code:

<body>
<?php
$eqString = $_POST["equation"]; //Postdata stored into $eqString.
$eqString = str_replace("+", " + ", $eqString); //Add spaces before and after +.
$eqString = str_replace("-", " - ", $eqString); //Add spaces before and after -.
$eqString = str_replace("*", " * ", $eqString); //Add spaces before and after *.
$eqString = str_replace("/", " / ", $eqString); //Add spaces before and after /.
$pieces = explode(" ", $eqString); //Put each term or operator of $eqString and put int into an array in $pieces.
$i1 = 0; //Set first counting variable to 0.
$i2 = 0; //Set second counting variable to 0.
foreach ($pieces as $array) { //This works for each variable in the array $pieces.
if ($array == "+") { //Checks to see if the current variable is +.
$operator[$il] = $array; //If so, then it sets the current operator variable as +.
// echo $operator[$i1]; //Test
$i1++; //Adds one to the first counting number.
} elseif ($array == "-") { //Checks to see if the current variable is -.
$operator[$il] = $array; //If so, then it sets the current operator variable as -.
// echo $operator[$i1]; //Test
$i1++; //Adds one to the first counting number.
} elseif ($array == "*") { //Checks to see if the current variable is -.
$operator[$il] = $array; //If so, then it sets the current operator variable as *.
// echo $operator[$i1]; //Test
$i1++; //Adds one to the first counting number.
} elseif ($array == "/") { //Checks to see if the current variable is -.
$operator[$il] = $array; //If so, then it sets the current operator variable as /.
// echo $operator[$i1]; //Test
$i1++; //Adds one to the first counting number.
} elseif ($array >= 0) { //Checks to see if the current variable is a number.
$term[$i2] = $array; //If so, then it sets the current term variable as that number.
// echo $term[$i2]; //Test
$i2++; //Adds one to the second counting number.
} else {
$answer = NULL; //Otherwise, it returns NULL.
}
}
print_r($term); //Test
print_r($operator); //Test
//function smath(&$term, &$operator, &$answer) {
echo $operator[0]; //Test
if ($operator[0] == "+") {
$answer = $term[0] + $term[1];
echo $answer; //Test
} elseif ($operator[0] == "-") {
$answer = $term[0] - $term[1];
echo $answer; //Test
} elseif ($operator[0] == "*") {
$answer = $term[0] * $term[1];
echo $answer; //Test
} elseif ($operator[0] == "/") {
$answer = $term[0] / $term[1];
echo $answer; //Test
} else {
$answer = NULL;
}
$i = 0;
foreach ($term as $array) {
if ($i > 0) {
if ($operator[$i] == "+") {
$answer = $answer + $array;
} elseif ($operator[$i] == "-") {
$answer = $answer - $array;
} elseif ($operator[$i] == "*") {
$answer = $answer - $array;
} elseif ($operator[$i] == "/") {
$answer = $answer / $array;
} else {
$answer = NULL;
}
}
$i++;
}
//}
?>
<form name="calculator" action="calcFrame.php" target="_self" method="POST">
<fieldset>
<legend>Calculator: (Currently Broken)</legend>
<input type="text" name="equation" maxlength="31" value="<?php echo $answer; ?>" />
<input type="submit" value="Enter" />
<p>Note: This calculator does not follow order of operations.</p>
</fieldset>
</form>
</body>


I think for some reason, it doesn't save the operators or terms to $operator[0] or $term[0]. But I have no clue why. Any thoughts?

Dankness
11-28-2006, 10:03 PM
Ill review your code and see what happens when i have time tonight, but the first thing i would do is is print all your varables and arrays after you add-remove from them like



print $var;
print_r($array);


print_r is my favorite tool for troubleshooting as it dumpsthe entire array wiht all keys and values. But if that leads you no where ill look at it when my son goes to sleep.

xxlegitxx
11-28-2006, 11:16 PM
Alright. Thanks. I'll try that now.

K, the problem seems to be recording the operator value as an array.

TOB
11-30-2006, 02:23 AM
I fixed :)


<body>
<?php
$action = $_POST['action'];
if($action == 1) {
$eqString = $_POST['equation']; //Postdata stored into $eqString.
$eqString = ereg_replace(" ", "", $eqString); //Makes it so that if they put in " + " the code below won't change it into " + "
$eqString = ereg_replace("\+", " + ", $eqString); //Add spaces before and after +.
$eqString = ereg_replace("-", " - ", $eqString); //Add spaces before and after -.
$eqString = ereg_replace("\*", " * ", $eqString); //Add spaces before and after *.
$eqString = ereg_replace("/", " / ", $eqString); //Add spaces before and after /.
$pieces = explode(" ", $eqString); //Put each term or operator of $eqString and put int into an array in $pieces.
$i1 = 0; //Set first counting variable to 0.
$i2 = 0; //Set second counting variable to 0.
foreach ($pieces as $array) { //This works for each variable in the array $pieces.
if (($array == "+") || ($array == "-") || ($array == "*") || ($array == "/")) { //Checks to see if the current variable is +. //If so, then it sets the current operator variable as +.
$operator[$i1] = $array;
//echo $operator[$i1]."<br>"; //Test
$i1++; //Adds one to the first counting number.
} elseif ($array >= 0) { //Checks to see if the current variable is a number.
$term[$i2] = $array; //If so, then it sets the current term variable as that number.
//echo $term[$i2]."<br>"; //Test
$i2++; //Adds one to the second counting number.
} else {
$answer = NULL; //Otherwise, it returns NULL.
}
}
//print_r($term); //Test
//print_r($operator); //Test
//function smath(&$term, &$operator, &$answer) {
//echo $operator[0]; //Test
$key1 = 0;
$key2 = 1;
$answer = $term[0];
while(array_key_exists($key1, $operator)) {
//echo "operator[".$key1."]: ".$operator[$key1]."<br>";
//echo "answer: ".$answer."<br>";
if(array_key_exists($key2, $term)) {
//echo "term[".$key2."]: ".$term[$key2]."<br>";
if ($operator[$key1] == "+") {
$answer = $answer + $term[$key2];
//echo $answer; //Test
} elseif ($operator[$key1] == "-") {
$answer = $answer - $term[$key2];
//echo $answer; //Test
} elseif ($operator[$key1] == "*") {
$answer = $answer * $term[$key2];
//echo $answer; //Test
} elseif ($operator[$key1] == "/") {
$answer = $answer / $term[$key2];
//echo $answer; //Test
} else {
$answer = NULL;
}
}
$key1++;
$key2++;
}
//}
}
echo "<br>The Answer For ". $eqString." is ".$answer.".<br><hr>";
?>
<form name="calculator" action="calcFrame.php" target="_self" method="POST">
<fieldset>
<legend>Calculator: (Fixed By TOB)</legend>
<input type="text" name="equation" maxlength="31"/>
<input type="hidden" name="action" value="1">
<input type="submit" value="Enter" />
<p>Note: This calculator does not follow order of operations.</p>
</fieldset>
</form>
</body>

xxlegitxx
12-01-2006, 12:07 AM
So what was the matter? And yea I'll include credits to you.

TOB
12-01-2006, 12:33 AM
Not entirely sure sorry :P. I think it was the way you set the values, I had made a couple change before that version so I can't remember what it was you did and what I did.

xxlegitxx
12-02-2006, 09:46 PM
Lol all right.

viky
01-26-2011, 10:09 AM
you may check php calculator (http://www.phpkode.com/scripts/category/php-calculators/) script to find the script you need!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Compound Interest Calculator</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
</head>

<body>
<?php
if (isset($_POST['balance'])) {
$orgbal = $_POST['balance'];
$orgint = $_POST['interest'];
} else {
// starting values for the calculator, change thi sto suit your audience
$orgbal = '3000';
$orgint = '19.99';
}
?>



<div><form id="calculate" name="calculate" method="post" action="<?php print_r($_SERVER['SCRIPT_NAME']); ?>">
<table id="calculate" width="600px" border="0" cellpadding="0" cellspacing="0" class="calculatortable">
<tr>
<th colspan="2">
<div align="left">Compound Interest Calculator
</div></th>
</tr>
<tr>
<td colspan="2">
Enter your balance and interest rate to calculate the compound interest you will be charged on a
daily, weekly, monthly and yearly total.</td>
</tr>
<tr>
<td width="50%"><strong>Balance</strong></td>
<td width="50%">$
<input id="user_interest" name="balance" type="text" value="<?php echo $orgbal ?>" size="7" maxlength="7" /></td>
</tr>
<tr>
<td width="50%"><strong>Rate</strong></td>
<td width="50%">&nbsp;&nbsp;&nbsp;
<input id="user_total" name="interest" type="text" value="<?php echo $orgint ?>" size="5" maxlength="5" />%
</td>
</tr>
<tr>
<td width="50%">&nbsp;</td>
<td><input type="submit" name="Submit" value="Calculate Interest" style="float:right;" /></td>
</tr>
<tr>
<td colspan="2">
<span class="smltxt"> This calculation does not include credit card fees, other charges and repayments to show only interest charges. Created by <a href="http://www.bestratecreditcards.com.au">Best Rate Credit Cards Australia</a></span>
</td>
</tr>
</table>

</form>

</div>


<br />


<?php
if (isset($_POST['balance'])) {


$bal = $_POST['balance'];
$int = $_POST['interest']/100;
$per = 12;

$interestcharged = $bal*(pow(1+($int/$per),$per)-1);

$dailyi = $interestcharged/365;
$weeklyi = $interestcharged/52;
$monthlyi = $interestcharged/12;
?>

<div>
<table width="600px" border="0" cellspacing="0" cellpadding="0" class="calculatortable">
<tr>
<th colspan="3"><div align="left">Calculated Interest</div></th>
</tr>
<tr>
<td width="20%"><strong>Interest Charged</strong></td>
<td width="20%"><strong>Amount</strong></td>
<td width="20%"><strong>New Balance </strong></td>
</tr>
<tr>
<td width="20%">Daily</td>
<td width="20%"><span class="mtxtbold">$<?php echo number_format($dailyi, 2) ?></span></td>
<td width="20%" class="mtxtbold"><strong>$<?php echo number_format($dailyi + $bal, 2) ?></strong></td>
</tr>
<tr>
<td width="20%">Weekly</td>
<td width="20%"><span class="mtxtbold">$<?php echo number_format($weeklyi, 2) ?></span></td>
<td width="20%" class="mtxtbold"><strong>$<?php echo number_format($weeklyi + $bal, 2) ?></strong></td>
</tr>
<tr>
<td width="20%">Monthly</td>
<td width="20%"><span class="mtxtbold">$<?php echo number_format($monthlyi, 2) ?></span></td>
<td width="20%" class="mtxtbold"><strong>$<?php echo number_format($monthlyi + $bal, 2) ?></strong></td>
</tr>
<tr>
<td width="20%">Yearly</td>
<td width="20%"><span class="mtxtbold">$<?php echo number_format($interestcharged, 2) ?></span></td>
<td width="20%" class="mtxtbold"><strong>$<?php echo number_format($interestcharged + $bal, 2) ?></strong></td>
</tr>
</table>
</div>


<?php
}
?>

</body>
</html>