Gossamer Forum
Home : Products : Gossamer Links : Discussions :

loop for last links

Quote Reply
loop for last links
Hi I'm trying to display the last links on the home page using a template and I was wanting to create a table where the links to be displayed are displayed one per row.

Using this global (found on the forum and modified a bit):

sub {
# Displays the newest links on the home page.
# Get the link information
my ($output,$sth,$link);
my $search_db = $DB->table('Links');
$search_db->select_options ('ORDER BY Add_Date DESC Limit 5');
$sth = $search_db->select (
# This only show links considered as new in the database
#{ isNew => 'Yes'},
{ isValidated => 'Yes'});
while (my $link = $sth->fetchrow_hashref()) {
# Get the links category information
my $catdb = $DB->table('CatLinks', 'Category');
my $catsth = $catdb->select ({ 'LinkID' => $link->{ID} }, ["Full_Name"]);
# This is the normal line
$link->{category} = $catsth->fetchrow_array;
$output .= Links::SiteHTML::display ('lastlinks', $link);
}
return $output;
}

I have a template called lastlinks that corresponds to a row and I have to create the table structure in home.html since the loop effect takes place through the sub.

In my home.html template I'll have :
<table border="0" cellpadding="3" cellspacing="0">
<tr>
<th><strong>Title</strong></th>
<th><strong>Editor</strong></th>
<th><strong>Category</strong></th></tr>
<%last_links%>
</table>

and the lastlinks.html template has this :
<tr><td><a href="<%db_cgi_url%>/detail_page.cgi?ID=<%ID%>"><%Title%></a></td>
<td><%Contact_Name%></td>
<td><%category%></td>
</tr>

I'd like to be able to create a loop in the lastlinks.html template so that I can just call the sub and that the lastlinks.html template has the whole structure with a loop in it. For example :

<table border="0" cellpadding="3" cellspacing="0">
<tr>
<th><strong>Title</strong></th>
<th><strong>Editor</strong></th>
<th><strong>Category</strong></th></tr>
<%loop links_loop%>
<tr><td><a href="<%db_cgi_url%>/detail_page.cgi?ID=<%ID%>"><%Title%></a></td>
<td><%Contact_Name%></td>
<td><%category%></td>
</tr>
<%endloop%>
</table>

I'm guessing I need to change the end of my sub to :
while ($link = $sth->fetchrow_hashref) {
push @links, $link;
}
return {LinksLoop=>\@output};
}

But I don't get where I tell the sub to use my lastlinks.html template to format the data...

Can I do this :
@output .= Links::SiteHTML::display ('lastsites', $link);
}
return {LinksLoop=>\@output};
}
Blush

If anyone someone could point me in the right direction Smile

Thanks, John
Significant Media

Last edited by:

Jag: Mar 12, 2005, 2:43 AM
Quote Reply
Re: [Jag] loop for last links In reply to
Hi,

You have it right with your first guess. You just need to use <%include lastlinks.html%> in your home template.
Quote Reply
Re: [afinlr] loop for last links In reply to
Hi Laura,

Thanks for your response, I tried the following :
sub called : "last_links_loop"

sub {
# Displays the newest links on the home page.
# Get the link information
my (@links,$sth,$link);
my $search_db = $DB->table('Links');
$search_db->select_options ('ORDER BY Add_Date DESC Limit 5');
$sth = $search_db->select (
# This only show links considered as new in the database
#{ isNew => 'Yes'},
{ isValidated => 'Yes'});
while (my $link = $sth->fetchrow_hashref()) {
# Get the links category information
my $catdb = $DB->table('CatLinks', 'Category');
my $catsth = $catdb->select ({ 'LinkID' => $link->{ID} }, ["Full_Name"]);
# This is the normal line
$link->{category} = $catsth->fetchrow_array;
push @links, $link;
}
return {LinksLoop=>\@output};
}

and a template called test_loop_articles.html where I have this :

<table border="0" cellpadding="3" cellspacing="0">
<tr>
<th><strong>Title</strong></th>
<th><strong>Editor</strong></th>
<th><strong>Category</strong></th></tr>
<%last_links_loop%>
<%loop Links_Loop%>
<tr><td><a href="<%db_cgi_url%>/detail_page.cgi?ID=<%ID%>"><%Title%></a></td>
<td><%Contact_Name%></td>
<td><%category%></td>
</tr>
<%endloop%>
</table>

but this gives me an error :
Unable to compile 'last_links_loop': Global symbol "@output" requires explicit package name at (eval 40) line 19.

I didn't think that the @output or $output needed to be declared ! I must be doing something silly here but I can't see what...
Significant Media
Quote Reply
Re: [Jag] loop for last links In reply to
Change @output to @links

Last edited by:

afinlr: Mar 14, 2005, 6:15 PM
Quote Reply
Re: [afinlr] loop for last links In reply to
Hi,
Well I don't get the error anymore but unfortunately I don't get any data except "Title Editor Category" which is hard coded into the template Frown

Thanks for your help anyway Laura
Significant Media
Quote Reply
Re: [Jag] loop for last links In reply to
Check that the tag you are returning and the one you are using in the template have the same name - you are returning LinksLoop and then trying to use Links_Loop?
Quote Reply
Re: [afinlr] loop for last links In reply to
Thanks,

For being able to figure out my silly mistakesBlush.
That works perfect by changing the Links_Loops in the template to LinksLoops. I was getting a bit lost as to what to change and not paying enough attention during the process I didn't check they were identical...

Thanks again Laura, you're a star Smile
Significant Media