return to first page linux journal archive
keywordscontents

Listing 7. view-stories-form.html

%# -*- mmm-classes: mason -*-
<Head><Title>View latest news</Title></Head>
<Body>
<H1>View latest news</H1>
<Form method="POST"
action="view-stories.html">
<P>From which category would you like to view news?</P>
	    <select name="category_id">
% foreach my $category (@categories) {
	    <option value="<% $category->{id} %>">
            <% $category->{name} %>
% }
	    </select>
<P>And how many items would you like to see?
<input type="text" name="how_many" size="2" maxlength="2"></P>
<input type="submit" value="View stories">
</Form>
</P>
</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>