bscotyb Posted April 8, 2012 Share Posted April 8, 2012 i have been trying to make this work for days, this needs to also include a discount for staying over >=5 days in Tokyo, no matter what i do i always gets syntax error this class is making me crazy how do i go about getting the discount in this????? please help <?php $destination = $_POST['destination']; $numTravelers = $_POST['numTravelers']; $numNights = $_POST['numNights']; $airFare = $_POST['airFare']; $hotel = $_POST['hotel']; if ($destination == Barcelona) {$airFare = 875.00; $perNight = 85.00;} elseif ($destination == Cairo) {$airFare= 950.00; $perNight = 98.00;} elseif ($destination == Rome) {$airFare = 875.00; $perNight = 110.00;} elseif ($destination == Santiago) {$airFare = 820.00; $perNight = 85.00;} elseif ($destination == Tokyo) {$airFare = 1575.00; $perNight = 240.00;} this is where i can not figure out how to put in the discount off the airfare for staying over five days elseif ($destination == Tokyo && $numNights >=5) $tickets = $numTravelers * $airFare; $hotel = $numTravelers * $numNights * $perNight; $totalCost = $tickets + $hotel; print("<p>Destination: $destination<br />"); print("Number of people: $numTravelers<br />"); print("Number of nights: $numNights<br />"); print("Airline Tickets: $".number_format($tickets, 2)."<br />"); print("Hotel Charges: $".number_format($hotel, 2)."</p>"); print("<p><strong>TOTAL COST: $".number_format($totalCost, 2)."</strong></p>"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/260578-nested-and-statements/ Share on other sites More sharing options...
dcro2 Posted April 8, 2012 Share Posted April 8, 2012 First of all, wrap your strings in double or single quotes. That's probably causing the syntax error. Second, your logic needs some work. An elseif only matches if a previous if/elseif hasn't already been true. In other words, because $destination == "Tokyo" is true, your elseif($destination == "Tokyo" && $numNights >= 5) will never happen. Instead do something like: elseif ($destination == "Tokyo") { $airFare = 1575.00; $perNight = 240.00; if($numNights >= 5) { $airFare -= 200; //discount } } Quote Link to comment https://forums.phpfreaks.com/topic/260578-nested-and-statements/#findComment-1335458 Share on other sites More sharing options...
chriscloyd Posted April 8, 2012 Share Posted April 8, 2012 how about this? <?php //i like creating arrays to keep things clearner $Array = array( "Barcelona" => array("875.00","85.00"), "Cairo" => array("950.00","98.00"), "Rome" => array("875.00","110.00"), "Santiago" => array("820.00","85.00"), "Tokyo" => array("1575.00","240.00") ); $destination = $_POST['destination']; $numTravelers = $_POST['numTravelers']; $numNights = $_POST['numNights']; $airFare = $_POST['airFare']; $hotel = $_POST['hotel']; $airFare = $Array[$destination][0]; $perNight = $Array[$destination][1]; $discount = ($destination == "Tokyo" && $numNights >= 5 ? "200" : "0"; //above change discount to whatever you need it to be $tickets = $numTravelers * $airFare; $hotel = $numTravelers * $numNights * $perNight; $totalCost = $tickets + $hotel + $discount; //I do not know where u want the discount added, to the totalcost or what print("<p>Destination: $destination<br />"); print("Number of people: $numTravelers<br />"); print("Number of nights: $numNights<br />"); print("Airline Tickets: $".number_format($tickets, 2)."<br />"); print("Hotel Charges: $".number_format($hotel, 2)."</p>"); print("<p><strong>TOTAL COST: $".number_format($totalCost, 2)."</strong></p>"); Quote Link to comment https://forums.phpfreaks.com/topic/260578-nested-and-statements/#findComment-1335467 Share on other sites More sharing options...
chriscloyd Posted April 8, 2012 Share Posted April 8, 2012 Do you have an form i could see so I can test it for you? Quote Link to comment https://forums.phpfreaks.com/topic/260578-nested-and-statements/#findComment-1335468 Share on other sites More sharing options...
bscotyb Posted April 8, 2012 Author Share Posted April 8, 2012 <html> <head> <title>Travel</title> <link rel ="stylesheet" type="text/css" href="sample.css" /> </head> <body> <h1>Travel Reservation Form</h1> <p> <form action = "travelBoles.php" method = "post" > <table> <tr><td>Select your vacation destination</td> <td><select name = "destination" /> <option>Barcelona</option> <option>Cairo</option> <option>Rome</option> <option>Santiago</option> <option>Tokyo</option> </select></td></tr> <tr><td>How many people are traveling?</td> <td><input type = "text" size = "5" name = "numTravelers" /></td> </tr><tr><td>How many nights will you be staying?</td> <td><input type = "text" size = "5" name = "numNights" /></td></tr> </table> <p><input type = "submit" value = "Submit Your Reservation" /></p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/260578-nested-and-statements/#findComment-1335470 Share on other sites More sharing options...
chriscloyd Posted April 8, 2012 Share Posted April 8, 2012 okay i put up a test one at http://cloydprojects.com Quote Link to comment https://forums.phpfreaks.com/topic/260578-nested-and-statements/#findComment-1335473 Share on other sites More sharing options...
bscotyb Posted April 8, 2012 Author Share Posted April 8, 2012 it rocks thank you Quote Link to comment https://forums.phpfreaks.com/topic/260578-nested-and-statements/#findComment-1335479 Share on other sites More sharing options...
chriscloyd Posted April 8, 2012 Share Posted April 8, 2012 here is all the code i used <?php if(isset($_POST['destination'])) { //i like creating arrays to keep things clearner $Array = array( "Barcelona" => array("875.00","85.00"), "Cairo" => array("950.00","98.00"), "Rome" => array("875.00","110.00"), "Santiago" => array("820.00","85.00"), "Tokyo" => array("1575.00","240.00") ); $destination = $_POST['destination']; $numTravelers = $_POST['numTravelers']; $numNights = $_POST['numNights']; $airFare = $_POST['airFare']; $hotel = $_POST['hotel']; $airFare = $Array[$destination][0]; $perNight = $Array[$destination][1]; $discount = ($destination == "Tokyo" && $numNights >= 5 ? "200" : "0"); //above change discount to whatever you need it to be $tickets = $numTravelers * $airFare; $hotel = $numTravelers * $numNights * $perNight; $totalCost = $tickets + $hotel + $discount; //I do not know where u want the discount added, to the totalcost or what print("<p>Destination: $destination<br />"); print("Number of people: $numTravelers<br />"); print("Number of nights: $numNights<br />"); print("Airline Tickets: $".number_format($tickets, 2)."<br />"); print("Hotel Charges: $".number_format($hotel, 2)."<br />"); if ($discount != 0) { print("Discount: $".number_format($discount, 2)."</p>"); } print("<p><strong>TOTAL COST: $".number_format($totalCost, 2)."</strong></p>"); } ?> <html> <head> <title>Travel</title> <link rel ="stylesheet" type="text/css" href="sample.css" /> </head> <body> <h1>Travel Reservation Form</h1> <p> <form action="" method="post" > <table> <tr><td>Select your vacation destination</td> <td><select name = "destination" /> <option>Barcelona</option> <option>Cairo</option> <option>Rome</option> <option>Santiago</option> <option>Tokyo</option> </select></td></tr> <tr><td>How many people are traveling?</td> <td><input type = "text" size = "5" name = "numTravelers" /></td> </tr><tr><td>How many nights will you be staying?</td> <td><input type = "text" size = "5" name = "numNights" /></td></tr> </table> <p><input type = "submit" value = "Submit Your Reservation" /></p> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/260578-nested-and-statements/#findComment-1335481 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.