Jump to content

soupi

Members
  • Posts

    23
  • Joined

  • Last visited

soupi's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. <?php $expression = ''; $input = array( 'a' => 5, 'b' => 10, 'c' => 15, 'd' => 20); $total = 0; $sum = ''; // Override default expression and input values if(isset($_POST['Calculate'])) { $input = $_POST['input']; //input being passed via POST method $expression = $_POST['expression']; //expression being passed via POST method } // make sure the users expression is safe to use if(preg_match('#^[a-z+-\s]+$#i', $expression)) { $sum = $expression; //evaluates sum as expression $input_letters = array(); //puts letters into a array $input_operators = array(); //puts operators into a array for ($i = 0; $i < strlen($expression) ; $i++) { //gets string length if (($i % 2) == 0) { $input_letters[] = $expression[$i]; //getting the character for the position i } else { $input_operators[] = $expression[$i]; } } // for ($i = 0 ; $i < sizeof($input_letters); $i++) { //size of= numbers (4) $value = $input[$input_letters[$i]]; //takes the value from the letters if ($i == 0) { $sum += $value; //adds the sum if = 0 } else { if ($input_operators[$i-1] == '+') { //checks to see if the operator is + $sum += $value; } else if ($input_operators[$i-1] == '-') { //checks to see if the operator is - $sum -= $value; } } print_r($value); } } else { echo "Error: expression not correct form"; //error message } ?> <form action="" method="post"> <!--Form begins--> Number A: <input type="text" name="input[a]" value="<?php echo $input['a']; ?>" size=3> <br/> Number B: <input type="text" name="input[b]" value="<?php echo $input['b']; ?>" size=3> <br> Number C: <input type="text" name="input[c]" value="<?php echo $input['c']; ?>" size=3> <br> Number D: <input type="text" name="input[d]" value="<?php echo $input['d']; ?>" size=3> <br> <br> Expression: <input type = "text" name = "expression" value="<?php echo $expression ?>"> = <?php echo $sum; ?><br> Sum: <?php echo $sum; ?><br> <input type="submit" name="Calculate" /> </form> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script> $( "form" ).submit(function( event ) { var myExpression = $('[name="expression"]').val(); if $("$input_letters").validate({ myExpression: { maxlength: 4 } }; } else { alert("Enter only four expressions"); } }); </script> in the code i provided, how would i add a javascript on the bottom check to see that user entered four letters, if they enter more then for expressions, then when submit is clicked, put a alert message saying you cant. it starts in the script section but Im not sure why it isnt working when i click submit after I run it.
  2. <?php $expression = ''; $input = array( 'a' => 5, 'b' => 10, 'c' => 15, 'd' => 20); $total = 0; $sum = ''; // Override default expression and input values if(isset($_POST['Calculate'])) { $input = $_POST['input']; //input being passed via POST method $expression = $_POST['expression']; //expression being passed via POST method } // make sure the users expression is safe to use if(preg_match('#^[a-z+-\s]+$#i', $expression)) { $sum = $expression; //evaluates sum as expression $input_letters = array(); //puts letters into a array $input_operators = array(); //puts operators into a array for ($i = 0; $i < strlen($expression) ; $i++) { //gets string length if (($i % 2) == 0) { $input_letters[] = $expression[$i]; //getting the character for the position i } else { $input_operators[] = $expression[$i]; } } // for ($i = 0 ; $i < sizeof($input_letters); $i++) { //size of= numbers (4) $value = $input[$input_letters[$i]]; //takes the value from the letters if ($i == 0) { $sum += $value; //adds the sum if = 0 } else { if ($input_operators[$i-1] == '+') { //checks to see if the operator is + $sum += $value; } else if ($input_operators[$i-1] == '-') { //checks to see if the operator is - $sum -= $value; } } print_r($value); } } else { echo "Error: expression not correct form"; //error message } ?> <form action="" method="post"> <!--Form begins--> Number A: <input type="text" name="input[a]" value="<?php echo $input['a']; ?>" size=3> <br/> Number B: <input type="text" name="input[b]" value="<?php echo $input['b']; ?>" size=3> <br> Number C: <input type="text" name="input[c]" value="<?php echo $input['c']; ?>" size=3> <br> Number D: <input type="text" name="input[d]" value="<?php echo $input['d']; ?>" size=3> <br> <br> Expression: <input type = "text" name = "expression" value="<?php echo $expression ?>"> = <?php echo $sum; ?><br> Sum: <?php echo $sum; ?><br> <input type="submit" name="Calculate" /> </form> <script src="http://code.jquery.c...1.js"></script> <script> $( "form" ).submit(function( event ) { var myExpression = $('[name="expression"]').val(); if $("$input_letters").validate({ minlength: 4 } }); </script> in the code i provided, how would i add a javascript on the bottom check to see that use entered four letters, if they enter more when submit is clicked, put a alert message saying you cant.
  3. Hey josh thanks for your insight, i followed it and solved it. <?php $expression = ''; $input = array( 'a' => 5, 'b' => 10, 'c' => 15, 'd' => 20); $total = 0; $sum = ''; // Override default expression and input values if(isset($_POST['Calculate'])) { $input = $_POST['input']; $expression = $_POST['expression']; } // make sure the users expression is safe to use if(preg_match('#^[a-z+-\s]+$#i', $expression)) { $sum = $expression; $input_letters = array(); $input_operators = array(); for ($i = 0; $i < strlen($expression) ; $i++) { if (($i % 2) == 0) { $input_letters[] = $expression[$i]; } else { $input_operators[] = $expression[$i]; } } // for ($i = 0 ; $i < sizeof($input_letters); $i++) { $value = $input[$input_letters[$i]]; if ($i == 0) { $sum += $value; } else { if ($input_operators[$i-1] == '+') { $sum += $value; } else if ($input_operators[$i-1] == '-') { $sum -= $value; } } } } else { echo "Error: expression not correct form"; } ?> <form action="" method="post"> Number A: <input type="text" name="input[a]" value="<?php echo $input['a']; ?>" size=3> <br/> Number B: <input type="text" name="input[b]" value="<?php echo $input['b']; ?>" size=3> <br> Number C: <input type="text" name="input[c]" value="<?php echo $input['c']; ?>" size=3> <br> Number D: <input type="text" name="input[d]" value="<?php echo $input['d']; ?>" size=3> <br> <br> Expression: <input type = "text" name = "expression" value="<?php echo $expression ?>"> = <?php echo $sum; ?><br> Sum: <?php echo $sum; ?><br> <input type="submit" name="Calculate" /> </form>
  4. Hi is there another way t do this with out using the EVAL to get the sum? I tried $total =$sum; but that didnt work
  5. thank you, that was a excellent example. Thanks alot, im starting to understand php little by little
  6. Thanks josh, may i ask what the implode and explode?
  7. i got it now, thanks alot Ch0cu3r
  8. thanks C, so i entered letters abc and d and I should get 25 for the result, but im getting 0150? below are screen shots before and after clicking submit Before After
  9. hi, the values like a=5 b=10 c=15 d=10 is numbers i provide. all the use does it input a b c or d. which those numbers are the values I indicated. does that make sense?
  10. 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 far 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 = "first" name = "input[]" size=3> <br/> Number 2: <input type = "second" name = "input[]" size=3> <br> Number 3: <input type = "third" name = "input[]" size=3> <br> Number 4: <input type = "fourth" 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']; // assigning $_GET value print_r($myInputs); echo $myInputs[0]+ $myInputs[1]; $myArray = array(); $myArray['a'] = 5; $myArray['b'] = 10 ; $myArray['c'] = 15 ; $myArray['d'] = 20 ; // using a,b,c,d only... using $_GET here echo $myArray['a']+$myArray['b']; // get directly from$_GET values. echo $_GET['input'][0]+ $_GET['input'][1]; ?>
  11. 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>"; } */ ?>
  12. so would it look like this for 4 numbers? <?php $myString = 'a=1,b=2,c=3,d=4'; $expression = 'a + b - c * d'; // put all variables into $variables parse_str(str_replace(',', '&', $myString), $variables); foreach ($variables as $variable => $value) { // replace the variables in $expression with their values $expression = str_replace( if ($_GET['calc'] == "yes") { $a = $_GET['a']; $b = $_GET['b']; $c = $_GET['c']; $d = $_GET['d']; $total = $a+$b+$c+$d;); } eval('?><?php echo ' . $expression . ';'); ?>
  13. I am trying to learn PHP, how would I start this? 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. i know how to do it in JAVA but am stummped on PHP Thank you guys
×
×
  • 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.