jacob1986 Posted December 12, 2015 Share Posted December 12, 2015 Sorry to have to ask questions again... but I'm having serious hindrance in regards to an assignment. I have to create a Calculator using PHP (which I have completed - see code below), but I have to do this whilst using a query string to provide two numbers and an operator? Moreover, I have to show the URL - showing the following: yourscript.php?n1=5$n2=7&op=m It then says validate that 'n1' and 'n2' are both numbers and that 'op' contains only allowed values???? If everything validates I have to print the result of the equation? My code has two parts and shows two numbers and an operator and prints like this: 5 + 7 = 12. But... I also have to add http_build_query in my code to show the url? <?php require_once 'big.php';$number1 = 5;$number2 = 7;$operator = "+";$calculator = new Calculator();$calculator->setNumbers($number1, $number2);$calculator->setOperator($operator);$calculator->calculate();echo $number1." ". $operator." ".$number2." = ". $calculator->getOutput(); <?php class Calculator { private $number1, $number2, $operator, $output; public function setNumbers($number1, $number2) { $this->number1 = $number1; $this->number2 = $number2; } public function setOperator($operator) { $this->operator= $operator; } public function calculate() { if($this->operator == "+") $this->output = $this->number1 + $this->number2; elseif($this->operator == "-") $this->output = $this->number1 - $this->number2; elseif($this->operator == "*") $this->output = $this->number1 * $this->number2; elseif($this->operator == "/") $this->output = $this->number1 / $this->number2; else $this->output = "An Error Has Materialize!"; } public function getOutput() { return $this->output; } } Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted December 12, 2015 Share Posted December 12, 2015 it sounds like you are supposed to create a form for inputting the numbers/selecting the operator - Quote Link to comment Share on other sites More sharing options...
maxxd Posted December 12, 2015 Share Posted December 12, 2015 Also sounds like the op parameter is the operation, so instead of using '+','=','/' and '*' as your cases in your calculate() method, you'd use 'm' for multiplication, 'd' for division, 'a' for addition, and 's' for subtraction. Honestly, what I'd do in that situation is pass both numbers as parameters to the class constructor, then use the operations parameter as a method call. If the operation parameter maps to a method that doesn't exist in the object, throw an error. Otherwise, return the modified value. That being said, what you have will work just fine, so that's just a personal preference. Quote Link to comment Share on other sites More sharing options...
jacob1986 Posted December 12, 2015 Author Share Posted December 12, 2015 I have designed another (if rather crude) php calculator this time with a form - could you show me how to make the URL show: yourscript.php?n1=5$n2=7&op=m i.e. by using http_build_query? Code: index2.html <html><form action="Calculator2.php" method="POST">n1: <input type="text" name="num1"> <select name="operations"> <option>Select an Operation...</option> <option name="Add">Add</option> <option name="Subtract">Subtract</option> <option name="Multiply">Multiply</option> <option name="Divide">Divide</option></select>n2: <input type="text" name ="num2"><input type="submit" value ="Calculate"></form></html> Code: Calculator2.php <?php$num1 = $_POST["num1"];$num2 = $_POST["num2"];$operation = $_POST["operations"];if($operation == "Add") {}$answer_Add = $num1 + $num2;echo $num1. " + ".$num2." = ";echo $answer_Add ?> Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted December 13, 2015 Share Posted December 13, 2015 Just use the GET method instead of POST in your form, and all values will automatically transmitted as URL parameters. POST doesn't make sense anyway, because the form submission isn't supposed to change anything. Quote Link to comment 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.