Jump to content

Form not outputting number


soupi

Recommended Posts

Given an input of an expression consisting of a string of letters and operators (plus sign, minus sign, and letters. IE: ‘b-d+e-f’) and a file with a set of variable/value pairs separated by commas (i.e: a=1,b=7,c=3,d=14) write a program that would output the result of the inputted expression.

For example, if the expression input was ("a + b+c -d") and the file input was ( a=1,b=7,c=3,d=14) the output would be -3.

Thus fur I have got this, it calculates the numbers but not sure how to calculate it by the letters abcd,
 
<html>
 <head>
 
 </head> 
 <body>
 
 <form action = ""  method = "GET"> 
Number 1: <input type = "text" name = "input[]" size=3> 
<br/> 

Number 2: <input type = "text" name = "input[]" size=3> <br> 

Number 3: <input type = "text" name = "input[]" size=3> <br> 

Number 4: <input type = "text" name = "input[]" size=3> <br> 

 <input type="hidden" name="calc" value ="yes"> 
 <input type = "submit" name = "Calculate"/> 
 </form> 
 </body> 
 </html> 

<?php 
   
  print_r($_GET);
  $myInputs = $_GET['input'];
  print_r($myInputs);
  
  $myArray = array();
  $myArray['a'] = 5;
  $myArray['b'] = 10 ; 
  $myArray['c'] = 15 ;
  $myArray['d'] = 20 ;

  
  echo $myArray[$_GET['a']] + $myArray[$_GET['b']] ; 
 
 /*
 if ($_GET['calc'] == "yes") 
 { 
 $a = $_GET['a']; 
 $b = $_GET['b']; 
 $c = $_GET['c']; 
 $d = $_GET['d']; 
 $total = $a+$b+$c+$d; 
 print "Total is: $total <p>";
 } 
 */
 ?>

 

Link to comment
https://forums.phpfreaks.com/topic/283374-form-not-outputting-number/
Share on other sites

I'm guessing this is homework!

 

Where are the operators?

 

But this line doesn't make sense:

echo $myArray[$_GET['a']] + $myArray[$_GET['b']];
* maybe it does make sense now :D

 

Maybe you need to go through your list of given operators, test to see what it is then do the operation, adding it to the running total.

 

Or a less safe way may be to use eval() ?

You could add index values to the input names:

 

 

Number 1: <input type = "text" name = "input[a]" size=3> <br> 
Number 2: <input type = "text" name = "input[b]" size=3> <br> 
Number 3: <input type = "text" name = "input[c]" size=3> <br> 
Number 4: <input type = "text" name = "input[d]" size=3> <br>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.