Listing 4. add-news-form.html

%# -*- mmm-classes: mason -*-
<Head><Title>Add a news story</Title></Head>
<Body>
<H1>Add a news story</H1>
<Form method="POST" action="add-news.html">
<table>
    <tr> <td>Headline</td>
   <td><input type="text"
name="headline"></td>
    </tr>
    <tr> <td>Body</td>
   <td><textarea name="body" cols="40"
            rows="10"></textarea></td>
    </tr>
    <tr> <td>
       Category
   </td> <td>
       <select name="category_id">
% foreach my $category (@categories) {
       <option value="<% $category-\->{id} %>"><% $category->{name} %>
% }
       </select>
   </td> </tr> <tr>
   <td colspan="2"><input type="submit" value="Add new news story"></td> </tr>
</table>
</Body>
<%once>
my $dbh;
</%once>
<%init>
# Connect to the database, if we haven't already
# done so
$dbh = $m->comp("database-connect.comp");
# Get the list of categories
my $sql = "SELECT category_id, category_name ";
  $sql .= "FROM Categories ";
  $sql .= "ORDER BY category_name ";
my $sth = $dbh->prepare($sql);
$sth->execute;
my $row_ref;
my @categories;
# Put the list of categories into @categories
while ($row_ref = $sth->fetchrow_arrayref)
{
    my ($id, $name) = @$row_ref;
    push @categories, {id => $id,
                       name => $name};
}
</%init>