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?
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?