digitaL.braVo
10-29-2012, 07:25 PM
Okay so I KNOW I'm making some horrible amateur mistake mainly because I've been doing this now for about a combined total of 8 hours over 2 days...
So I'm following a PHP learning guide that I'll assume is okay to post... PHP For the Absolute Beginner (http://devzone.zend.com/6/php-101--php-for-the-absolute-beginner/) and so far it's been a really pleasant read. I have very little actual programming background so it's all pretty new candy so far.
In any case I set a small goal for myself to make a simple "calculator" setup just so I could say I've done it, and preferably before I end up finding a guide where someone explains it. Now the way I've got it setup so far is 2 text boxes divided by a drop down where you select one of four operators (+, -, *, /).
So I can get it to add just fine... but it seems to ignore any "if/elseif" statements I try after addition, it also seems to ignore a desperate attempt at using switch/case to figure out which operator was selected.
So with that tl;dr I cannot get my drop downs selected option to be considered when my calculator is running. And like any good amateur I started out with something very simple and it has progressively grown to something more ugly and probably less capable as I've gone on and, clearly, in the wrong direction...
So here's some HTML, this has barely changed as I've gone on...
<form action="calculator.php" method="post">
<input type="number" name="num1" size="6">
<select name="operator">
<option value="1">+</option>
<option value="2">-</option>
<option value="3">*</option>
<option value="4">/</option>
</select>
<input type="number" name="num2" size="6">
<br />
<input type="submit" value="Calc">
</form>
This was my first and what I thought was a graceful attempt NOTE, I trimmed some areas that I felt were irrelevant to the problem...
$n1 = $_POST['num1'];
$n2 = $_POST['num2'];
$o = $_GET['operator'];
if ($o = '1') {
$result = ($n1 + $n2);
$o2 = "+";
$solution = true;
}
elseif ($o = '2') {
$result = ($n1 - $n2);
$o2 = "-";
$solution = true;
}
elseif ($o = '3') {
$result = ($n1 * $n2);
$o2 = "*";
$solution = true;
}
elseif ($o = '4') {
$result = ($n1 / $n2);
$o2 = "/";
$solution = true;
And here comes the desperate, inexperienced, attempt at trying to throw more at the problem and hope I could figure it out a different way... Same thing applies trimmed some irrelevant parts...
$n1 = $_POST['num1'];
$n2 = $_POST['num2'];
$o = $_GET['operator'];
switch ($o) {
case '1':
$result = ($n1 + $n2);
$o2 = "+";
$solution = true;
break;
case '2':
$result = ($n1 - $n2);
$o2 = "-";
$solution = true;
break;
case '3':
$result = ($n1 * $n2);
$o2 = "*";
$solution = true;
break;
case '4':
$result = ($n1 / $n2);
$o2 = "/";
$solution = true;
break;
}
So. How bad did I do? (Oh and yes I saw the lines about turning on error reporting and tried those to no avail)
So I'm following a PHP learning guide that I'll assume is okay to post... PHP For the Absolute Beginner (http://devzone.zend.com/6/php-101--php-for-the-absolute-beginner/) and so far it's been a really pleasant read. I have very little actual programming background so it's all pretty new candy so far.
In any case I set a small goal for myself to make a simple "calculator" setup just so I could say I've done it, and preferably before I end up finding a guide where someone explains it. Now the way I've got it setup so far is 2 text boxes divided by a drop down where you select one of four operators (+, -, *, /).
So I can get it to add just fine... but it seems to ignore any "if/elseif" statements I try after addition, it also seems to ignore a desperate attempt at using switch/case to figure out which operator was selected.
So with that tl;dr I cannot get my drop downs selected option to be considered when my calculator is running. And like any good amateur I started out with something very simple and it has progressively grown to something more ugly and probably less capable as I've gone on and, clearly, in the wrong direction...
So here's some HTML, this has barely changed as I've gone on...
<form action="calculator.php" method="post">
<input type="number" name="num1" size="6">
<select name="operator">
<option value="1">+</option>
<option value="2">-</option>
<option value="3">*</option>
<option value="4">/</option>
</select>
<input type="number" name="num2" size="6">
<br />
<input type="submit" value="Calc">
</form>
This was my first and what I thought was a graceful attempt NOTE, I trimmed some areas that I felt were irrelevant to the problem...
$n1 = $_POST['num1'];
$n2 = $_POST['num2'];
$o = $_GET['operator'];
if ($o = '1') {
$result = ($n1 + $n2);
$o2 = "+";
$solution = true;
}
elseif ($o = '2') {
$result = ($n1 - $n2);
$o2 = "-";
$solution = true;
}
elseif ($o = '3') {
$result = ($n1 * $n2);
$o2 = "*";
$solution = true;
}
elseif ($o = '4') {
$result = ($n1 / $n2);
$o2 = "/";
$solution = true;
And here comes the desperate, inexperienced, attempt at trying to throw more at the problem and hope I could figure it out a different way... Same thing applies trimmed some irrelevant parts...
$n1 = $_POST['num1'];
$n2 = $_POST['num2'];
$o = $_GET['operator'];
switch ($o) {
case '1':
$result = ($n1 + $n2);
$o2 = "+";
$solution = true;
break;
case '2':
$result = ($n1 - $n2);
$o2 = "-";
$solution = true;
break;
case '3':
$result = ($n1 * $n2);
$o2 = "*";
$solution = true;
break;
case '4':
$result = ($n1 / $n2);
$o2 = "/";
$solution = true;
break;
}
So. How bad did I do? (Oh and yes I saw the lines about turning on error reporting and tried those to no avail)