## Mustache Departments

### Mustache Template

mustache-dept.php

### PHP

    <?php
    // Connecting, selecting database
      $mysqli = new mysqli('localhost','class','cscie12','coursecatalog');
      if ($mysqli->connect_errno) {
          echo "Failed to connect to mysql: ".$mysqli->connect_errno." ".$mysqli->connect_error;
      }

    // Performing SQL query
    $query = 'SELECT DISTINCT department, department_code  ext_courses ORDER BY department';
    $result = $mysqli->query($query);

    $i=0;
    while ($row = $result->fetch_assoc()) {
      $results[$i++] = $row;
    }

    $template_data['departments'] = $results;

    // Process with Mustache Template
    include('lib/Mustache.php');

    $m = new Mustache;
    $template_file = 'mustache_templates/departments.html';
    $template_contents = file_get_contents($template_file);
    echo $m->render($template_contents, $template_data);

    // cleanup query result and close connection
    $query_result->free();
    $mysqli->close();
    ?>

### Mustache Template

mustache_templates/departments.html

    <!DOCTYPE html>
    <html>

    <head>
      <meta charset="UTF-8" />
      <title>Harvard Extension School - Departments</title>
      <link rel="stylesheet" href="styles/site.css" />
    </head>

    <body>
      <h1>Harvard Extension School</h1>
      <h2>Departments</h2>
      <ul>
        {{#departments}}
        <li>
          <a href="mustache-courses.php?department_code={{department_code}}">{{department}}</a>
        </li>
        {{/departments}}
      </ul>

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