Listing 6. get-news.comp
%# -*- mmm-classes: mason -*-
<%perl>
# Connect to the database, if we haven't
# already done so
$dbh = $m->comp("database-connect.comp");
# Get the news items in this category
my $sql = "SELECT headline, body, posting_date ";
$sql .= "FROM Articles ";
$sql .= "WHERE category_id = ? ";
$sql .= "ORDER BY posting_date DESC ";
$sql .= "LIMIT $how_many ";
my $sth = $dbh->prepare($sql) || die "Error:
$DBI::errstr";
$sth->execute($category_id) || die "Error:
$DBI::errstr";
my @articles;
my $row_ref;
# Put the list of articles into @articles
while ($row_ref = $sth->fetchrow_arrayref)
{
my ($headline, $body, $posting_date) =
@$row_ref;
push @articles, {headline => $headline,
body => $body,
posting_date => $posting_date};
}
return @articles;
</%perl>
<%once>
my $dbh;
</%once>
<%args>
$category_id
$how_many => 5
</%args>
Copyright © 1994 - 2019 Linux Journal. All rights reserved.