
soupi
Members-
Posts
23 -
Joined
-
Last visited
Everything posted by soupi
-
<?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.
-
<?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.
-
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>
-
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
-
thank you, that was a excellent example. Thanks alot, im starting to understand php little by little
-
Thanks josh, may i ask what the implode and explode?
-
i got it now, thanks alot Ch0cu3r
-
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
-
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?
-
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]; ?>
-
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>"; } */ ?>
-
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 . ';'); ?>
-
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
-
GOT IT <?php $start_weight = 180; $start_body_fat = 27; $start_body_fat_percent = $start_body_fat/$start_weight * 100; $pounds_lost = 15; echo "Starting weight is: ".$start_weight." Starting body fat is: ".$start_body_fat_percent."%"; for($i = 1; $i <= $pounds_lost; $i++) { //work out new body weight, take $i from $body_weight $new_weight = $start_weight - $i; //new body fat percentage, take $i from $start_body_fat and divide by new body weight $new_body_fat_percent = (($start_body_fat - $i) / $new_weight) * 100; // new weight and body fat percentage echo "<br/>After losing $i pounds of fat weight is now: $new_weight, body fat is: $new_body_fat_percent%"; } ?>
-
Im receiving a syntax error on this line echo "After losing $i pounds of fat weight is now: $new_weight, body fat is: $new_body_fat_percent%"; , can you atleast show me how to print the first statement After losing 1 pounds of fat Weight is now 179 body fat is now: 14.53% After losing 2 pounds of fat Weight is now 178 body fat is now: 14.04%
-
<?php $start_weight = 180; $start_body_fat = 27; $start_body_fat_percent = $start_body_fat/$start_weight * 100; $pounds_lost = 15; echo "Starting weight is: ".$start_weight." Starting body fat is: ".$start_body_fat_percent."%"; for($i = 1; $i <= $pounds_lost; $i++) { //work out new body weight, take $i from $body_weight*/ $new_weight = $i-$pounds_lost //recalculate body fat percentage, take $i from $start_body_fat and divide by new body weight and times by 100 $new_body_fat_percent = $i-$start_body_fat/$new_weight *100; // output how many pounds lost ($i), new weight and body fat percentage echo "After losing $i pounds of fat weight is now: $new_weight, body fat is: $new_body_fat_percent%"; } ?> I have a sytax on the red line, I kno what for loop does, but this is my first example. Im sorry
-
$new_weight =$number1= $start_weight-1; ?? will that be the new weight?
-
so would it be like this? <?php $start_weight = 180; $start_body_fat = 27; $start_body_fat_percent = $start_body_fat/$start_weight * 100; $pounds_lost = 15; echo "Starting weight is: ".$start_weight." Starting body fat is: ".$start_body_fat_percent."%"; for ($i =1; <= $pounds_lost; $i++); { echo "After losing 1 pounds of fat weight is now: $number1" ; echo " & Starting body fat is: ".$start_body_fat_percent1."%"; } ?>
-
Hi i need to Echo out this statement, Starting weight is: 180 Starting body fat is: 15% After losing 1 pounds of fat Weight is now 179 body fat is now: 14.53% After losing 2 pounds of fat Weight is now 178 body fat is now: 14.04% After losing 3 pounds of fat Weight is now 177 body fat is now: 13.56% After losing 4 pounds of fat Weight is now 176 body fat is now: 13.07% After losing 5 pounds of fat Weight is now 175 body fat is now: 12.57% After losing 6 pounds of fat Weight is now 174 body fat is now: 12.07% So this is what I have, im not sure how to subtract 1 pound to output 179 and so forth, any help and advice would be appreciated. Thank you. <?php $start_weight = 180; $start_body_fat = 27; $start_body_fat_percent = $start_body_fat/$start_weight * 100; $pounds_lost = 15; $first_number= 1; echo "Starting weight is: ".$start_weight." Starting body fat is: ".$start_body_fat_percent."%"; echo "<br>"; echo "After losing 1 pounds of fat weight is now: "$start_weight"- ".$first_number." Starting body fat is: ".$start_body_fat_percent."%"; ?>