Harvard Extension School
CSCI E-12 Fundamentals of Web Site Development | Fall 2019

## Adding another view - Department Course Listings

Create another PHP file that displays course information based upon a department_code parameter.

- `courses.php` - take a "department_code" parameter and produce alist of courses for that department.

Need to link to to courses.php:

- courses.php?department_code=CHEM
- courses.php?department_code=MCB

Modify the department listing to include the hyperlink:

    <!DOCTYPE html>
    <html>
     <head>
      <title>Departments</title>
      <link rel="stylesheet" href="site.css" type="text/css"/>
     </head>
     <body>
    <h1>Faculty of Arts &amp; Sciences</h1>
    <h2>Departments</h2>
    <?php
    // Connecting, selecting database
    $link = mysql_connect('localhost', 'class', 'dbpasswd')
         or die('Could not connect: ' . mysql_error());
    mysql_select_db('coursecatalog') or die('Could not select database');

    // Performing SQL query
    $query = 'SELECT distinct department_short, department_code FROM courses order by department_short';
    $result = mysql_query($query) or die('Query failed: ' . mysql_error());

    // Printing results in HTML
    echo "<ul>\n";
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
      echo "<li>";
      echo "<a href=\"courses.php?department_code=$row[department_code]\">$row[department_short]</a>";
      echo "</li>\n";
    }
    echo "</ul>\n";

    // Free resultset
    mysql_free_result($result);

    // Closing connection
    mysql_close($link);
    ?>

    </body>
    </html>

Copyright © David Heitmeyer