Chris Pollett >
Old Classes >
PIC40

   ( Print View )

Lecture Notes-PDF

Spring '01 Ad: Enrollment info

Course Info: Homework Assignments:
Practice Exams:
PIC:
                           












HW6 Solutions Page

Return to homework page.

<? if($State=="Save Itinerary"){SetCookies();}
//
// FileName: p40hw6file1.php (Travel Planner program)
//                            Now with Advertisements
//
// Purpose: This file accesses the airtransportation database to allow
//          user to make a travel itinerary based on the flight
//          information stored in this database.
//
?>

<html>
  <head>
    <title>Travel Planner</title>
    <script language="javascript" type="text/javascript">

   <? //check if we will do the ItinerarySaved page
      //
      if($State =="Save Itinerary")
      {
   ?>

      //
      // Load in Our Images for ItinerarySaved page
      //

      $Car0 = new Image;
      $Car0.src = "car0.gif";

      $Car1 = new Image;
      $Car1.src = "car1.gif";

   <? }//end ItinerarySaved Check

      //Intro check
      //Only need to output VerifyDate when could be on Intro page

      if(!$State || $State=="Main Menu" || $State=="Show Flights")
      {
   ?>
	//
	// Function: VerifyDate()
	//
	// Purpose: Checks that the date is in the correct format
	//
	//
      function VerifyDate()
      {
	var $DEPDATE=document.forms[1].DEPDATE.value;
	var $BadDate = false, $DateParts, $Part, $i;
	var $Month, $Day;

	//Split Date into parts make sure has three parts

	$DateParts = $DEPDATE.split("-");

	if($DateParts.length != 3) {return BadDate();}

	//Make sure parts of date of correct length and consist
        //of numbers.

	if(!( $DateParts[0].length == 4  &&
              $DateParts[1].length == 2  &&
	      $DateParts[2].length == 2)) {return BadDate();}

	for ($Part in $DateParts)
	{
		for($i=0; $i < $Part.length ; $i++)
		{ if($Part.charAt($i)<'0' || $Part.charAt($i)> '9')
                           {return BadDate();}
		}
	}

	$Month = parseInt($DateParts[1]);
	$Day = parseInt($DateParts[2]);

	if( ($Month == 2 && $Day >28) ||
	    (($Month == 4 || $Month == 6 || $Month == 9 || $Month == 11)
               && $Day >30) || $Day >31) {return BadDate();}

	return true;
      }

      function BadDate()
      {
	var $MSG = 'Date format should be YYYY-MM-DD\n';
	    $MSG += 'where Y=Year, M=Month, D=Day.\n';
            $MSG += 'For example, 2001-08-01.\n';
	    $MSG += 'Make sure the day is correct for\n';
	    $MSG += 'the month as well. For example, Feb. has\n';
	    $MSG += '28 days.';
	alert($MSG);
	return false;
      }

   <? } //end of Intro Check

	   // Check again if will do ItinerarySaved page
	   //
	   if($State == "Save Itinerary")
	   {
   ?>

	//
	// Function: GoToURL()
	//
	// Purpose: used by the select object on the ItinerarySaved
 	//          page to immediately jump to selected hotel
	//
      function GoToURL()
      {
	var $Hotels = document.forms[0].hotels;
	var $Hotel = $Hotels.options[$Hotels.selectedIndex].value;

	document.location.href = $Hotel;
      }

	//
	// Function: ChangeImage()
	//
	// Purpose: changes which car is display on the ItinerarySaved()
	//          page
	//
      function ChangeImage($car)
      {
	document.car.src=$car.src;

      }

    <? }// End second ItinerarySaved check
    ?>

    </script>
  </head>
  <body bgcolor="#99FFCC">

  <?
	//
        // Main Loop:
        //
        // $State variable is used to control which page is displayed

	switch($State)
	{
		case "Fetch Now":
		  Itinerary("saved");
		break;

		case "Save Itinerary":
		  ItinerarySaved();
		break;

		case "Show Flights":
                  $MSG = CKAirport();
                   if($MSG == "Flight")
                     {Flight();}
                   else if($MSG != "done")
                     {Intro($MSG);}
                break;

		case Previous:
		  if($SEAT2) {Flight();}
                  else {SelectSeat();}
		break;

                case Flight:
	          Flight();
		break;

                case "Continue":
		  if($SEAT)
                       {Itinerary("unsaved");}
                  else
                      {SelectSeat();}
		break;

		case "Main Menu":
		default:
		  Intro();
	}


  ?>

  </body>
</html>

<?
//
// Function: ItinerarySaved()
//
// Purpose: After the itinerary has been saved this page
//          provides the user with links to hotels
//          and car rental companies in the destination city
//
function ItinerarySaved()
{
	GLOBAL $DESCITY;
 ?>
	<h3 style="color:blue">Itinerary Saved</h3>
	<h1>Now that you've chosen your trip may we suggest the following
           hotels...</h1>
	<hr>

	<form method="post" action="hw6.php">
	<div style="text-align:center">
	<table>
<?
	if($DESCITY == "YFK")
	{?>
	<tr><td>
	<select name="hotels" onChange="GoToURL()" size="4">
	  <option value="http://www.centennialhotels.com/" selected="true">
	  Centenial Hotels Halifax
	  </option>
	  <option value="http://www.ramadans.com/">
	  Ramada Park Place
	  </option>
	  <option value="http://www.citadelhalifax.com/">
	  The Citadel Hotel
	  </option>
	  <option value="http://www.waverleyinn.com/">
	  The Waverley Inn
	  </option>
	</select>
	</td></tr>

	<tr><td>
	<a href="http://www.halifax.worldweb.com/Rentals/CarTrucks/"
           onMouseover="ChangeImage($Car1); return true;"
           onMouseout="ChangeImage($Car0); return true;">
	<img name="car" src="car0.gif" />
	</a>
	</td></tr>
	<tr><td>
	<?}

	if($DESCITY == "BOS")
	{?>
	<tr><td>
	<select name="hotels" onChange="GoToURL()" size="4">
	  <option value="http://www.theinnatharvard.com" selected="true">
	  The Inn at Harvard
	  </option>
	  <option value="http://www.fshr.com/locations/Boston/">
	  Four Seasons
	  </option>
	  <option value=
          "http://www.MarriottHotels.com/dpp/PropertyPage.asp?MarshaCode=BOSLW">
	  The Marriot Long Wharf
	  </option>
	  <option value=
              "http://www.reservation-services.com/radissonhotelboston.html">
	  Radisson Hotel Downtown
          </option>
	</select>
	</td></tr>

	<tr><td>
	<a href="http://www.boston-bos.com/car_rental.html"
           onMouseover="ChangeImage($Car1); return true;"
           onMouseout="ChangeImage($Car0); return true;">
	<img name="car" src="car0.gif" />
	</a>
	</td></tr>
	<tr><td>
	<?}

	if($DESCITY == "LAX")
	{?>
	<tr><td>
	<select name="hotels" onChange="GoToURL()" size="4">
	  <option value="http://www.holidayinnlongbeach.com" selected="true">
	  Long Beach Holiday Inn
          </option>
	  <option value="http://www.bestwesternhollywood.com">
	  Best Western Hollywood
	  </option>
	  <option value="http://www.daysinn-maingate.com">
	  Days Inn Anaheim
	  </option>
	  <option value="http://www.esanaheim.com">
	  Embassy Suites Anaheim
	  </option>
	</select>
	</td></tr>

	<tr><td>
	<a href="http://www.la-rentacar.com/"
           onMouseover="ChangeImage($Car1); return true;"
           onMouseout="ChangeImage($Car0); return true;">
	<img name="car" src="car0.gif" />
	</a>
	</td></tr>
	<tr><td>
	<?}

	if($DESCITY == "CDG" || $DESCITY == "ORY" )
	{?>
	<tr><td>
	<select name="hotels" onChange="GoToURL()" size="4">
	  <option value="http://www.ritz.com/" selected="true">
	  The Ritz Paris
	  </option>
	  <option value="http://www.fourseasons.com/locations/Paris/index.php">
	  Hotel George V
	  </option>
	  <option value="http://www.sofitel.mgn.fr/">
	  Sofitel
	  </option>
	  <option value="http://www.hotels-parisiens.com/">
	  Comfort Inns in Paris
	  </option>
	</select>
	</td></tr>

	<tr><td>
	<a href="http://www.deluxe-international.com/"
           onMouseover="ChangeImage($Car1); return true;"
           onMouseout="ChangeImage($Car0); return true;">
	<img name="car" src="car0.gif" />
	</a>
	</td></tr>
	<tr><td>
	<?}


	OutButton(submit,"State","Main Menu");
?>
	</td></tr>
	</table>
	</div>
	</form>
<?
}

//
// Function: Intro
//
// Purpose: This function takes a message $MSG and if it exists
//          outputs in blue followed by the intro screen to
//          the Travel Planner program. If $MSG is false it
//          just outputs the intro screen. If $MSG has some value
//          then any of the form variables is not set it also
//          outputs a star by this element in the intro screen.
//
function Intro($MSG=false) {
  GLOBAL $DEPCITY, $DESCITY, $DEPDATE;



  if ($MSG)   {print "<h3 style=\"color:blue\"> $MSG</h3>";}
 ?>

  <h1>Travel Planner</h1>

  <form method="POST" action="hw6.php" >

  <h3>Previous Itinerary:
     <? OutButton("submit","State","Fetch Now");?>
  </h3>
  </form>
  <hr>

  <form method="POST" action="hw6.php" onSubmit="return VerifyDate();">
    <table border="0" cellpadding="3" cellspacing="3">
     <tr>
      <td><b>Departure City or Airport:</b></td>
      <td><? OutText("DEPCITY", "$DEPCITY", 15, 80);
             if($MSG && !$DEPCITY) {OutStar();}
          ?>
      </td>
     </tr>
     <tr>
      <td><b>Destination City or Airport:</b></td>
      <td><?  OutText("DESCITY", "$DESCITY", 15, 80);
             if($MSG && !$DESCITY) {OutStar();}
          ?>
      </td>
     </tr>
     <tr>
      <td><b>Departure Date:</b></td>
      <td><?  OutText("DEPDATE", "$DEPDATE", 15, 80);
             if($MSG && !$DEPDATE) {OutStar();}
          ?>
      </td>
     </tr>
     <tr>
      <td></td>
      <td>YYYY-MM-DD
      </td>
     </tr>
    <td align="center" colspan="2">
    <h3>
     <?OutButton("reset","reset","Reset");
       OutButton("submit","State","Show Flights");?>
    </h3>
    </td>
    </table>
  </form>
<?}

//
// Function:CKAirport
//
// Purpose: this function check if the three variables
//          $DEPCITY, $DESCITY, $DEPDATE all have values.
//          If not it returns the string "Sorry, you did not give
//          me enough data. Please fill in the starred fields."
//          Otheriwse, it looks at the Airport table of the airtransportation
//          database to see if $DEPCITY and $DESCITY are codes for
//          airports or are cities. If they are cities but there
//          is only one airport in that city the $DEPCITY or $DESCITY
//          is then set to the code of that airport. If at this
//          point both $DEPCITY and $DESCITY are codes of airports
//          then the value "Flight" is returned. If neither a code nor
//          a city could be found then "Sorry we not service $DEPCITY"
//          or "Sorry we nor service $DESCITY" or both is returned
//          by CKAirport. Finally, if there is more airport for $DESCITY
//          of $DEPCITY of both it produces a form with selection
//          selection gadgets to let the user pick between the possibilities.
//          after generating this form it returns the value "done".
//

function CKAirport()
{
	GLOBAL $DEPCITY, $DESCITY, $DEPDATE;
	$DEPCITY=DeleteSharp($DEPCITY);
	$DESCITY=DeleteSharp($DESCITY);

	if (!($DEPCITY && $DESCITY && $DEPDATE))
	{
		$ERRMSG = "Sorry, you did not give me enough data. Please";
                $ERRMSG .= " fill in the starred fields.";
		return $ERRMSG;
	}

	$dbh=Connect();

	$cur=Prepare("select code from Airport where city='$DEPCITY'
                                                  OR code='$DEPCITY'");
	if(NumRows($cur)==0) { $ERRMSG.=
                                  "Sorry, we do not service $DEPCITY.<br />\n";
                                $DEPCITY = false;
                              }
	if(NumRows($cur)==1) {$row=FetchRow($cur);
                                $DEPCITY = $row[0];
				$isAirport1=true;
                               }
	FreeCursor($cur);

	$cur=Prepare("select code from Airport where city='$DESCITY'
                                                  OR code='$DESCITY'");
	if(NumRows($cur)==0) { $ERRMSG .=
                                  "Sorry, we do not service $DESCITY.<br />\n";
                                $DESCITY = false;
                              }

	if(NumRows($cur)==1) {
                               $row=FetchRow($cur);
                                $DESCITY = $row[0];
				$isAirport2=true;
                               }
	FreeCursor($cur);

	if(!$DEPCITY || !$DESCITY) {Close($dbh); return $ERRMSG;}
	if($isAirport1 && $isAirport2) {Close($dbh); return "Flight";}

	?>

	<form method="POST" action="hw6.php"> <?
	  $cur=Prepare("select code from Airport where city='$DEPCITY'");
	  if(NumRows($cur) > 1) {
		print "<h3> Please select a departure airport:</h3>\n";
		DrawSelect($cur,"DEPCITY",2);
		print "<br />";
          }
	  else {  OutButton("hidden","DEPCITY","$DEPCITY");}

	  FreeCursor($cur);
	  $cur=Prepare("select code from Airport where city='$DESCITY'");
	  if(NumRows($cur) > 1) {
		print "<h3> Please select a destination airport:</h3>\n";
		DrawSelect($cur,"DESCITY",2);
		print "<br />";
          }
	  else {  OutButton("hidden","DESCITY","$DESCITY");}
	  FreeCursor($cur);
        OutButton("hidden","DEPDATE","$DEPDATE");
        OutButton("submit","State","Main Menu");
        OutButton("submit","State","Show Flights");
	?> </form> <?
	Close($dbh);
	return "done";
}

//
// Function: Flight
//
// Purpose: produces the screen that allows the user to
//          select the from a list of available flights
//          between $DEPCITY and $DESCITY on $DEPDATE.
//          Queries the airtransportation database Flight
//          table to get this info.
//

function Flight()
{
	GLOBAL $DEPCITY, $DESCITY, $DEPDATE;
	$dbh=Connect();

	$cur=Prepare("select flightNo, airCraftName, dateTime
                      from Flight
                      where fromAirport='$DEPCITY'
                            AND toAirport='$DESCITY'
                            AND dateTime like '$DEPDATE%'");


 ?>
  <h1>Travel Planner</h1>
  <hr>

  <form method="POST" action="hw6.php">

  <table>
  <tr>
  <td>
  <h3>Please select a flight:</h3>
  </td>
  </tr>
  <tr>
  <td>
  <?
	DrawSelect($cur, "FLIGHT", 10);

	Close($dbh);
  ?>
  <td>
  </tr>
  <tr>
  <td>
  <? OutButton("hidden","DEPCITY",$DEPCITY);
     OutButton("hidden","DESCITY",$DESCITY);
     OutButton("hidden","DEPDATE",$DEPDATE);
     OutButton("submit","State","Main Menu");
     OutButton("submit","State","Continue");
  ?>
  </tr>
  </td>
  </table>
  </form>
<?}

//
// Function: SetCookies
//
// Purpose: sets cookies to store the variables
//          $DEPCITY, $DESCITY, $DEPDATE, $FLIGHT, $SEAT
//

function SetCookies()
{
  GLOBAL $DEPCITY, $DESCITY, $DEPDATE, $FLIGHT, $SEAT;
  SetCookie("DEPCSAVE","$DEPCITY");
  SetCookie("DESCSAVE","$DESCITY");
  SetCookie("DEPDSAVE","$DEPDATE");
  SetCookie("FLIGHTSAVE","$FLIGHT");
  SetCookie("SEATSAVE","$SEAT");
}

//
// Function: Itinerary
//
// Purpose: if $saved is equal to "saved". It copies the variables
//          stored in the cookies back into the variables
//          $DEPCITY, $DESCITY, $DEPDATE, $FLIGHT, $SEAT first.
//          In all cases, it then draws the itinerary that the
//          user has selected so far followed by the buttons
//          Main Menu, Previous, and Save Itinerary.
//          these either return to the Intro screen, select seat
//          screen or returns to Intro screen with saved info
//          message and where the first line of the program has been
//          execute to set the appropriate cookies.
//

function Itinerary($saved)
{ GLOBAL $DEPCITY, $DESCITY, $DEPDATE, $FLIGHT, $SEAT;
  GLOBAL $DEPCSAVE, $DESCSAVE, $DEPDSAVE, $FLIGHTSAVE, $SEATSAVE;

  	if($saved=="saved")
	{
		if(!$DEPCSAVE) return Intro("No previous itinerary");
		$DEPCITY=$DEPCSAVE;
		$DESCITY=$DESCSAVE;
		$DEPDATE=$DEPDSAVE;
		$FLIGHT=$FLIGHTSAVE;
		$SEAT=$SEATSAVE;
	}

	$flArray = split( "#", $FLIGHT);
	$flightNo = $flArray[0];
 ?>
  <h1>Travel Planner</h1>
  <hr>
  <h3>Your current schedule is as follows:</h3>
  <p>You are leaving on: <? print "$DEPDATE."; ?></p>
  <p>You are flying from: <? print "$DEPCITY."; ?></p>
  <p>You are flying to: <? print "$DESCITY."; ?></p>
  <p>The flight number is: <? print "$flightNo."; ?></p>
  <p>Your seat is: <? print "$SEAT."; ?></p>
  <form method="POST" action="hw6.php">
  <?   OutButton("hidden","DEPCITY",$DEPCITY);
       OutButton("hidden","DESCITY",$DESCITY);
       OutButton("hidden","DEPDATE",$DEPDATE);
       OutButton("hidden","FLIGHT",$FLIGHT);
       OutButton("hidden","SEAT",$SEAT);
       OutButton("submit","State","Main Menu");
       OutButton("submit","State","Previous");
       OutButton("submit","State","Save Itinerary"); ?>
  </form>
<?
 }

//
// Function: SelectSeat
//
// Purpose: This function draws the screen to allow the user to pick a
//          from the available seats on a given flight.
//

function SelectSeat()
{
	GLOBAL $DEPCITY, $DESCITY, $DEPDATE, $FLIGHT;

	$flArray = split( "#", $FLIGHT);
	$flightNo = $flArray[0];
        $aircraft = $flArray[1];
	$dateTime = $flArray[2];

 ?>
  <h1>Travel Planner</h1>
  <hr>

  <form method="POST" action="hw6.php">

  <table>
  <tr>
  <td>
  <h3>Please select a seat:</h3>
  </td>
  </tr>
  <tr>
  <td>
  The information below is formatted as follows:
  </td>
  </tr>

  <tr>
  <td>
   Row#Seat#Aisle?#Window?#Class
  </td>
  </tr>

  <tr>
  <td>
  <?
	print "<select name=\"SEAT\" size=\"10\">\n";
	$flag=true;

	$dbh=Connect();

	//
	// Notice I open up two cursors at this point. I wouldn't
        // need to do this on a database
	//

	$cur=Prepare("select rowNo, seat,aisle,window, class
                      from AircraftRow
                      where airCraftName = '$aircraft'");
	while($row = FetchRow($cur))
	{

	   $cur2=Prepare("select rowNo, seat from Bookings
                          where flightNo='$flightNo'
			  and dateTime='$dateTime'
                          and rowNo='$row[0]' and seat='$row[1]' ");
	  if(!FetchRow($cur2))
		{
		   OutOption($flag,$row);
                   if($flag) {$flag=false;}
                }
	}



	Close($dbh);
  ?>
     </select>
  <td>
  </tr>
  <tr>
  <td>
  <? OutButton("hidden","DEPCITY",$DEPCITY);
     OutButton("hidden","DESCITY",$DESCITY);
     OutButton("hidden","DEPDATE",$DEPDATE);
     OutButton("hidden","FLIGHT",$FLIGHT);
     OutButton("hidden","SEAT2","true");
     OutButton("submit","State","Main Menu");
     OutButton("submit","State","Previous");
     OutButton("submit","State","Continue");
  ?>
  </tr>
  </td>
  </table>
  </form>

<?}

//
// Function: OutButton
//
// Purpose: draws to screen a button of type $type with name $name
//          and with value $value.
//

function OutButton($type,$name,$value)
{
 print "<input type=\"$type\" name=\"$name\" value =\"$value\" />";
}

//
// Function: OutText
//
// Purpose: draws to screen a textfield of type $type with name $name
//          and with value $value of size $size and max number of char's
//          maxlength.
//


function OutText($name,$value,$size,$maxLength)
{
 print "<input type=\"text\" name=\"$name\" value =\"$value\"";
 print " size=\"$size\" maxlength=\"$maxLength\" />";
}

//
// Function: OutStar
//
// Purpose: draws a bold-face blue star
//

function OutStar()
{
	print "<b style=\"color:blue\">*</b>\n";
}

//
// Function: OutOption
//
// Purpose: Writes out an option of a selection gadget
//          If $selected is true this will be a selected option
//          of the selection gadget. $row is the value of the option.
//

function OutOption($selected,$row)
{
		if($selected) {print "<option selected=\"true\">";
                           $flag=false;
                          }
		else {print "<option>";}
                foreach($row as $field)
                {
			print "$field#";
		}?>
                </option>
<?}

//
// Function: DeleteSharp
//
// Purpose: returns the substring of $string before the first # symbol
//

function DeleteSharp($string)
{
	$row=split("#",$string);
	return $row[0];
}

//
// Function:DrawSelect
//
// Purpose: Draws a selection gadget with name $name where the options
//          are given by cycling over the rows returned by the database
//          cursor $cursor.
//

function DrawSelect($cursor, $name, $rowsVisible)
{
	print "<select name=\"$name\" size=\"$rowsVisible\">\n";
	$flag=true;

	while($row = FetchRow($cursor))
	{
                OutOption($flag,$row);
		if($flag) { $flag=false;}
         }?>
                </select>
<?}

//
// Function:Connect
//
// Purpose: connect to the database system.
//

function Connect()
{
	return mysql_connect("localhost", "pic40", "pic40");

}

//
// Function: FetchRow
//
// Purpose:
//

function FetchRow($cursor)
{
	return mysql_fetch_row($cursor);
}
//
// Function:NumRows
//
// Purpose: returns the numbers of rows left in database cursor $cursor.
//

function NumRows($cursor)
{
	return mysql_num_rows($cursor);
}

//
// Function: Prepare
//
// Purpose: prepares an SQL query to be executed
//

function Prepare($query)
{
	return mysql_db_query("airtransportation", $query);
}

//
// Function:FreeCursor
//
// Purpose: frees a cursor
//

function FreeCursor($cursor)
{
    mysql_free_result($cursor);
}

//
// Function: Close
//
// Purpose: closes a database handle $dbhandle
//

function Close($dbhandle)
{
    mysql_close($dbhandle);

}

?>