Gossamer Forum
Home : Products : Gossamer Links : Discussions :

Displaying Multiple Categories for a Link

Quote Reply
Displaying Multiple Categories for a Link
I'm trying to display a link to all the categories in which a link is listed.

I used the following global but it only displays a single category:

sub {
my $tag = shift;
my $table = $DB->table ('CatLinks');
my $sth = $table->select ( { LinkID => $tag->{ID} }, ['CategoryID'] );
my $catID = $sth->fetchrow();
my $cattable = $DB->table('Category');
my $sth2 = $cattable->select ( { ID => $catID }, ['Full_Name'] );
my $name = $sth2->fetchrow_array;
my $clean_name = $cattable->as_url($name);
my $url = "<a href=\"" . $CFG->{build_root_url} . "/" . $clean_name . '/' . $CFG->{build_index} . "\">" . $name . "</a>";
return $url;
}


Is there a way to modify this or loop this code to display multiple categories?

Thanks.
Quote Reply
Re: [biglion] Displaying Multiple Categories for a Link In reply to
Hi,

Try this:

Call the global "get_cats_in", with the following code:

Code:
sub {
my $tag = shift;
my $sth = ('CatLinks')->select ( { LinkID => $tag->{ID} }, ['CategoryID'] ) || die $GT::SQL::error;

my @cats;
while (my $hit = $sth->fetchrow_hashref) {
my $cat = $DB->table('Category')->select( { ID => $hit->{CategoryID} } )->fetchrow_hashref;
my $cat_url = $DB->table('Category')->as_url($cat);
my $tmp; $tmp->{Name} = $cat; $tmp->{URL} = $cat_url;
push @cats, $tmp;
}

return { cats_in => \@cats };
}
Then, call with:

Code:
<%get_cats_in%>
<ul>
<%loop cats_in%>
<li><a href="<%URL%>"><%Name%></a></li>
<%endif%>
</ul>

Please note, this isn't tested - so may have some bugs (shouldn't do, but it may <G>)

Hope that helps.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!

Last edited by:

Andy: Aug 24, 2007, 8:09 AM
Quote Reply
Re: [Andy] Displaying Multiple Categories for a Link In reply to
I'm getting the error...

Unable to compile 'get_cats_in': Bareword "cat" not allowed while "strict subs" in use at (eval 95) line 7.
Quote Reply
Re: [biglion] Displaying Multiple Categories for a Link In reply to
Hi,

Sorry about that - I missed a $ in front of "cat". I've updated my post above.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Displaying Multiple Categories for a Link In reply to
It wasn't finding the CatLinks table so I changed...
my $sth = ('CatLinks')->select ( { LinkID => $tag->{ID} }, ['CategoryID'] ) || die $GT::SQL::error;
to...
my $table = $DB->table ('CatLinks');
my $sth = $table->select ( { LinkID => $tag->{ID} }, ['CategoryID'] ) || die $GT::SQL::error;

also, I changed <%endif%> to <%endloop%>

Now the categories are appearing but they're incorrectly linked. They all link to the /detailed directory.
Quote Reply
Re: [biglion] Displaying Multiple Categories for a Link In reply to
Hi,

Try this:

Code:
sub {

my $sth = $DB->table('CatLinks')->select ( { LinkID => $_[0] }, ['CategoryID'] ) || die $GT::SQL::error;

my @cats;
while (my $hit = $sth->fetchrow_hashref) {

# print $hit->{LinkID} . "<br/>";

my $cat = $DB->table('Category')->select( { ID => $hit->{CategoryID} } )->fetchrow_hashref;
my $cat_url = $DB->table('Category')->as_url( $cat->{Full_Name} );
my $tmp;
$tmp->{Name} = $cat->{Name};
$tmp->{URL} = $CFG->{build_root_url} . "/" . $cat_url . "/" . $CFG->{build_index};
push @cats, $tmp;
}

return { cats_in => \@cats };
}

..and then this template code:

Code:
<%get_cats_in($ID)%>
<ul>
<%loop cats_in%>
<li><a href="<%URL%>"><%Name%></a></li>
<%endloop%>
</ul>

Tested that on our dev install, and all seems to work :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Displaying Multiple Categories for a Link In reply to
That seems to work perfectly. Thanks!