mindapolis Posted June 14, 2012 Share Posted June 14, 2012 Hi, is it possible to have several if statements but just one else statement? Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 14, 2012 Share Posted June 14, 2012 <?php if(){ }else if(){ }else if(){ }else{ } ?> Also, see switch Quote Link to comment Share on other sites More sharing options...
mindapolis Posted June 14, 2012 Author Share Posted June 14, 2012 Ok I got everything set up but when I click the calculate button, it doesn't do the calculating. Also, should I make the last elseif statement the else statement? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Untitled Document</title> </head> <body> <?php if ($i = 2) { $price = 3.50; } elseif ($i = 3) { $price = 4.70; } if ($i = 4) { $price = 5.90; } elseif ($i = 5) { $price = 7.00; } elseif ($i = 6) { $price = 8.20; } else { } function calculate($TotalSqFt, $price) { $total = $TotalSqFt + $price; return $total; } ?> <form action="" "" method="post"> <table> <tr> <td> total sq ft </td><td> <input name="TotalSqFt" type="text" size="4"></td><br /> <td> number of cuts </td><td><select name="cuts" id="cuts"> <br> <?php for ($i =2; $i <=6; $i ++) { echo "<option value=\"$i\""; if($cut == $i){ echo ' SELECTED'; } echo ">$i</option>"; } ?> </select> </td> </tr> <tr> <td> <input name="submit" type="submit" value="calculate"></td><td> <?php calculate($TotalSqFt, $price); ?> </td> </tr> </table> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Jessica Posted June 14, 2012 Share Posted June 14, 2012 $i is not defined before your if. turn on error reporting to E_ALL And that is a great example of if/else that should be written as a switch. Quote Link to comment Share on other sites More sharing options...
scootstah Posted June 14, 2012 Share Posted June 14, 2012 You are not defining $i, $TotalSqFt, or $price. This tells me that you either have register_globals on (BAD!) or you do not have error reporting on. So for starters, put this in the top of your script: ini_set('display_errors', 1); error_reporting(-1); Quote Link to comment Share on other sites More sharing options...
mindapolis Posted June 14, 2012 Author Share Posted June 14, 2012 I'm a little confused. $TotalSqFt should be defined by the user 's input, right? Quote Link to comment Share on other sites More sharing options...
scootstah Posted June 14, 2012 Share Posted June 14, 2012 Only if you have the register_globals directive turned on, which, like I said, is BAD! Leaving register_globals on is a big security risk, which is why it is deprecated and being removed from future versions of PHP. The proper way to capture user input is to use the appropriate PHP superglobal. In this case, you'd use $_POST. Quote Link to comment Share on other sites More sharing options...
mindapolis Posted June 14, 2012 Author Share Posted June 14, 2012 so at the beginning of the PHP code I would do something like $TotalSqFt = $_POST['TotalSqFt'] right? Quote Link to comment Share on other sites More sharing options...
Mahngiel Posted June 14, 2012 Share Posted June 14, 2012 so at the beginning of the PHP code I would do something like $TotalSqFt = $_POST['TotalSqFt'] right? If you have an INPUT form name'd 'TotalSqFt', then yes. <input type="text" name="TotalSqFt" /> Quote Link to comment Share on other sites More sharing options...
scootstah Posted June 14, 2012 Share Posted June 14, 2012 Yes, but if you haven't submitted the form yet you will get an "undefined index" warning (because $_POST has not yet been populated). So, usually scripts will check to see if the form has been submitted before trying to work with it. I (quickly) rewrote part of your script: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Untitled Document</title> </head> <body> <?php function calculate($TotalSqFt = null, $price = null) { return $TotalSqFt + $price; } // initialize variables so we dont get undefined errors later on $price = null; $TotalSqFt = null; // check if the form was submitted if (!empty($_POST)) { $cut_types = array( 2 => '3.50', 3 => '4.70', 4 => '5.90', 5 => '7.00', 6 => '8.20' ); $price = $cut_types[$_POST['cuts']]; $TotalSqFt = $_POST['TotalSqFt']; } ?> <form action="" "" method="post"> <table> <tr> <td> total sq ft </td><td> <input name="TotalSqFt" type="text" size="4"></td><br /> <td> number of cuts </td><td><select name="cuts" id="cuts"> <br> <?php for ($i =2; $i <=6; $i ++) { echo "<option value=\"$i\""; if($cut == $i){ echo ' SELECTED'; } echo ">$i</option>"; } ?> </select> </td> </tr> <tr> <td> <input name="submit" type="submit" value="calculate"></td><td> <?php calculate($TotalSqFt, $price); ?> </td> </tr> </table> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
mindapolis Posted June 15, 2012 Author Share Posted June 15, 2012 I tried your code and when I click calculate still nothing happens Quote Link to comment Share on other sites More sharing options...
scootstah Posted June 15, 2012 Share Posted June 15, 2012 Since calculate returns a value you have to echo it. <?php echo calculate($TotalSqFt, $price); ?> Quote Link to comment Share on other sites More sharing options...
mindapolis Posted June 15, 2012 Author Share Posted June 15, 2012 it is working! While I was waiting for help I tried a different code that I felt more comfortable with and I was wonder if I could get help understanding this error. Notice: Undefined index: cut in /home/content/04/8884504/html/jorge/calculator.php on line 6 <?php ini_set('display_errors', 1); error_reporting(-1); if(isset($_POST['submit'])) { $TotalSqFt = $_POST['TotalSqFt']; $cut = $_POST['cut']; } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Untitled Document</title> </head> <body> <?php if ($i = 2) { $price = 3.50; } elseif ($i = 3) { $price = 4.70; } if ($i = 4) { $price = 5.90; } elseif ($i = 5) { $price = 7.00; } elseif ($i = 6) { $price = 8.20; } else { } function calculate($TotalSqFt, $price) { $total = $TotalSqFt + $price; return $total; } ?> <form action="" "" method="post"> <table> <tr> <td> total sq ft </td><td> <input name="TotalSqFt" type="text" size="4"></td><br /> <td> number of cuts </td><td><select name="cuts" id="cuts"> <br> <?php for ($i =2; $i <=6; $i ++) { echo "<option value=\"$i\""; if($cut == $i){ echo ' SELECTED'; } echo ">$i</option>"; } ?> </select> </td> </tr> <tr> <td> <input name="submit" type="submit" value="calculate"></td><td> <?php echo calculate($TotalSqFt, $price); ?> </td> </tr> </table> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Mahngiel Posted June 15, 2012 Share Posted June 15, 2012 Undefined Index refers to the array key of the superglobal you're requesting. In your situation, you are requesting the key 'cut' for $_POST i.e. $_POST['cut'] Your form MUST have that input being submitted if you wish to retrieve it on the opposite side. Back to what we've been saying repeatedly here: define what you request and request what you define. Quote Link to comment Share on other sites More sharing options...
scootstah Posted June 15, 2012 Share Posted June 15, 2012 The field is named "cuts" and not "cut". Quote Link to comment Share on other sites More sharing options...
kicken Posted June 15, 2012 Share Posted June 15, 2012 <?php if ($i = 2) { $price = 3.50; } elseif ($i = 3) { $price = 4.70; } if ($i = 4) { $price = 5.90; } elseif ($i = 5) { $price = 7.00; } elseif ($i = 6) { $price = 8.20; } else { } That block of IF statements does not make sense where it is, as $i is not defined at that point. From what it looks like you probably want to be using $cut instead of $i in your conditions. The conditions themselves are also incorrect as the = operator does an assignment not a comparison. You need to use == (two equal signs) to do a comparison check. The way it is right now, your price would always be 3.50 because the first condition would assign the value 2 to your variable, and then set the price to 3.50 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.