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

## Smarty Template for Courses

The template (courses.tpl):

    <!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>
    <p><a href="smarty-dept.php">Return to Department List</a></p>
    <h2>Department {$department_code}</h2>
    <table cellspacing="0" cellpadding="0">
    {foreach from=$courses item=course name=courses}
    <tr class="{cycle values="row1,row2,row3,row4"}">
    <td class="abbrev">{$course.course_group_long} {$course.num_int}{$course.num_char}</td>
    <td class="long">
      <strong>{$course.title}</strong><br/>
      <span class="faculty">{$course.faculty_text}</span>
      <p class="description">
        {$course.description}
      </p>
    </td>
    </tr>
    {/foreach}
    </table>
    </body>
    </html>

Get the Courses for the CHEM department: smarty-courses.php?department_code=CHEM

The SQL:

    SELECT course_group_long,
                  num_int, num_char, term,
                  title, description

smarty-course.php:

    <?php
    /* ==================================================
        Database Stuff
       ================================================== */
    // 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 course_group_long, num_int, num_char, term, title, description, faculty_text';
    $query .= ' from courses where department_code = ';
    $query .= "'".mysql_escape_string($_GET['department_code'])."'";
    $query .= ' order by course_group_long, num_int, num_char, title';
    $res = mysql_query($query) or die('Query failed: ' . mysql_error());
    $i = 0;
    while ($row =  mysql_fetch_array($res, MYSQL_ASSOC)) {
      $results[$i++] = $row;
    }

    /* ==================================================
        Smarty Template Stuff
       ================================================== */
    require('/usr/local/lib/php/Smarty/Smarty.class.php');
    // create object
    $smarty = new Smarty;
    // pass the results to the template
    $smarty->assign('courses', $results);
    $smarty->assign('department_code', $_GET['department_code']);

    $smarty->template_dir = './smarty_templates';
    $smarty->compile_dir = '/tmp/smarty/templates_c';
    $smarty->cache_dir = '/tmp/smarty/cache';
    $smarty->config_dir = './smarty_configs';

    // display it
    $smarty->display('courses.tpl');

    /* ==================================================
        Cleanup
       ================================================== */
    // Free resultset
    mysql_free_result($res);
    // Closing connection
    mysql_close($link);
    ?>

Copyright © David Heitmeyer