Harvard Extension School
CSCI E-12 Fundamentals of Web Site Development | Spring 2020

## 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
         $mysqli = new mysqli('localhost','class','cscie12','coursecatalog');
         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);

       // Performing SQL query
       $query = 'SELECT course_group_long, num_int, num_char, term, title, description, faculty_text';
       $query .= ' from courses where department_code = ';
       $query .= "'".$sqlescaped_dept_code."'";
       $query .= ' order by course_group_long, num_int, num_char, title';

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

       $i = 0;
       while ($row = $result->fetch_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';

       // assign some content. This would typically come from
       // a database or other source, but we'll use static
       // values for the purpose of this example.
       $smarty->assign('dept', $results);

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

    /* ==================================================
        Cleanup
       ================================================== */
       // Free resultset
       $result->free();

       // Closing connection
       $mysql->close();
    ?>

Copyright © David Heitmeyer