PHP Final Exam

  1. HTML Image Map

    Consider the campus map of NCNU. Use HTML MAP tag to specify the coordinates of three rectangles on the map, which correspond to the colleges of Science & Technology, Management, and Humanity, respectively. When a user click on an area corresponding to a college, the browser will open the homepage of that college.

    Save your HTML file as "final-1.html", and submit the URL to Moodle.

  2. File Access
    1. Download the "phonebook.txt" file and save it under your WWW directory.
    2. The following script is expected to generate a table of three columns. However, it seems all the three items (name, phone, email) are shown in the same column, instead of three separate columns.
    3. Try to modify one line of the script so that it can display the table contents correctly.
    4. Paste the modified script on Moodle, with the modified line marked with a special color, as shown in the example.
      
      <?php
      $theData = file("phonebook.txt");
      print "<TABLE BORDER='1'>\n";
      foreach ($theData as $line) {
      $line = rtrim($line);
      list($name, $phone, $email) = split('\t', $line);
      print "<TR><TD> $name <TD> $phone <TD> $email \n";
      }
      ?>
      </TABLE>


  3. The following script does not display the time correctly.
    Please modify the definition of the class so that it can work correctly.
    Highlight modified lines with a different color.

    <?php
    class Time {
    private $hour;
    private $minute;

    function __construct($s) {
    list($hour, $minute) = split(":", $s);
    }

    function getTime() {
    return $hour . ':' . $minute;
    }

    function modify($d) {
    $minute = $minute + $d;
    }
    }
    ?>

    <?php
    $departure = new Time("22:54");
    $arrival = new Time("23:36");
    print "Your train departs at\t\t" . $departure->getTime();
    print " and arrives at " . $arrival->getTime() . "<br />\n";

    $departure->modify(-39);
    $arrival->modify(-39);
    print "The previous train departs at\t\t" . $departure->getTime();
    print " and arrives at " . $arrival->getTime() . "<br />\n";

    $departure->modify(99);
    $arrival->modify(99);
    print "The next train departs at\t\t" . $departure->getTime();
    print " and arrives at " . $arrival->getTime() . "<br />\n";

    ?>

  4. Rational Number
    1. Design a class Rational to implement a rational number (p, q), where p and q are integers.
    2. Save your definition in a file "final-4.php".
    3. Test your class with the following code:
      <?php
      include "final-4.php";
      $a = new Rational(1, 3);
      $b = new Rational(1, 2);
      $c = $a->add($b);
      $d = $b->add($b);
      print $c . "\n";
      print $d . "\n";
      ?>
    4. The result should be
      5/6
      1/1
    5. Hint: You may wish to define a __toString() function when you print an object.
  5. Database Manipulation
    1. Develop a PHP script, which will read the contents of a text file "phonebook.txt", whose format is similar to the one we used previously.
    2. You script will then create a table under the database "final". The table name should be "phonebook_xxxxxxxxx", where "xxxxxxxxx" is your NCNU student ID.
    3. The table has the following columns:
      • name VARCHAR(20);
      • phone VARCHAR(20);
      • email VARCHAR(20);
    4. You script will then insert the data read from the file "phonebook.txt" into the table.
    5. If a table with the same name already exists in the database, your script should silently delete it before it inserts data.
    6. Save your script as "final-5.php" under your WWW directory, and submit its URL to Moodle.