Jump to content

nested and statements


bscotyb

Recommended Posts

:wtf:

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>");

?>

Link to comment
https://forums.phpfreaks.com/topic/260578-nested-and-statements/
Share on other sites

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
	 }
}

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>");

<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>

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.