Jump to content

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/299712-calculator-query-string/
Share on other sites

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.

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

 

?>

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.