## courses.php

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

    <!DOCTYPE html>
    <html>
     <head>
      <meta charset="UTF-8"/>
      <title>Harvard Extension Courses</title>
      <link rel="stylesheet" href="styles/site.css" type="text/css"/>
     </head>
     <body>
    <h1>Harvard Extension Courses</h1>
    <p><a href="departments.php">Return to Department List</a></p>
    <?php

    // use Dotenv to load database info from .env file
    // DB_HOST, DB_USER, DB_PW,     DB_NAME
    // host,    user,    password,  database name
    require_once('vendor/autoload.php');
    $dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
    $dotenv->safeLoad();

    // Connecting, selecting database
    $mysqli = new mysqli($_ENV['DB_HOST'],$_ENV['DB_USER'],$_ENV['DB_PW'],$_ENV['DB_NAME']);
    if ($mysqli->connect_errno) {
        echo "Failed to connect to mysql: ".$mysqli->connect_errno." ".$mysqli->connect_error;
    }

    $dept_code = $_GET['department_code'];
    $sqlescaped_dept_code = $mysqli->real_escape_string($dept_code);
    $dept_code_html = htmlentities($dept_code);
    ?>
    <h2>Department - <?php echo $dept_code_html ?></h2>
    <?php
    // Performing SQL query
    $query = <<<ENDQUERY
    SELECT
      department,
      department_code,
      course_number,
      term,
      academic_year,
      short_title,
      title,
      description,
      faculty_description
    FROM
      ext_courses
    WHERE
      department_code = '$sqlescaped_dept_code'
    ORDER BY
      department,
      course_number,
      title
    ENDQUERY;

    $result = $mysqli->query($query);

    // Printing results in HTML

    $i = 0;
    while ($row = $result->fetch_assoc()) {
      // calculate year
      $academic_year_display = $row['academic_year'];
      if ($row['term'] == 'Spring' or $row['term'] == 'January') {
        $academic_year_display += 1 ;
      }

      $course_content = <<<ENDCONTENT
      <section class="course">
      <h2 class="abbrev"> $row[short_title] </h2>
      <div>
        <p class="term">$row[term] $academic_year_display</p>
        <p class="title"><strong>$row[title]</strong></p>
        <p class="faculty">$row[faculty_description]</p>
        <p class="description">$row[description]</p>
      </div>
      </section>
    ENDCONTENT;
      echo($course_content);
    }

    // cleanup query result and close connection
    $query_result->free();
    $mysqli->close();
    ?>
    <footer>
    <p><a href="./">PHP/mysql example list</a></p>
        <p>This is a learning example to demonstrate PHP and mysql for the courses CSCI E-12 and CSCI S-12 taught by David Heitmeyer.  <br/>
      The real <a href="https://courses.dce.harvard.edu/">Harvard Extension School course search</a> is at: <a href="https://courses.dce.harvard.edu/">courses.dce.harvard.edu</a>
        </p>
    </footer>
    </body>
    </html>