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

Edited by mentalist
Link to comment
Share on other sites

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>
Link to comment
Share on other sites

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.