<?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';
$res = mysql_query($query) or die('Query failed: ' . mysql_error());
$i=0;
while ($row = mysql_fetch_array($res, MYSQL_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);
// Free resultset
mysql_free_result($res);
// Closing connection
mysql_close($link);
?>
mustache_templates/departments.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Departments</title>
<link rel="stylesheet" href="site.css" type="text/css"/>
</head>
<body>
<h1>Faculty of Arts & Sciences</h1>
<h2>Departments</h2>
<ul>
{{#departments}}
<li>
<a href="mustache-courses.php?department_code={{department_code}}">{{department_short}}</a>
</li>
{{/departments}}
</ul>
</body>
</html>
Copyright © David Heitmeyer