Gossamer Forum
Home : Products : Gossamer Links : Development, Plugins and Globals :

Showing all categories a link belongs to

Quote Reply
Showing all categories a link belongs to
I am using* this global to show the category name/link in link.html:
Code:
sub {
my ($rec) = @_;
my $id = $rec->{ID};
my $db = $DB->table ('Category','CatLinks');
my $sth = $db->select ( { 'CatLinks.LinkID' => $id }, ['Category.ID', 'Category.Full_Name'] );
my $cat;
while (my ($id,$name) = $sth->fetchrow_array) {
$name =~ s/ /_/g;
$cat = "<a href=$CFG->{build_root_url}/$name>$name</a> ";
}
return $cat;
}

Now, this shows the "main" category.

Can anyone suggest how to show the additional categories, a link belongs to? That could be one or more categories. Or none at all (i.e., a link only belongs to one category).


*: I've used it in 2.xx as well as now in 3.00.
Quote Reply
Re: [gotze] Showing all categories a link belongs to In reply to
There is a small bug in it (highlighted in red):
Code:
sub {
my ($rec) = @_;
my $id = $rec->{ID};
my $db = $DB->table ('Category','CatLinks');
my $sth = $db->select ( { 'CatLinks.LinkID' => $id }, ['Category.ID', 'Category.Full_Name'] );
my $cat;
while (my ($id,$name) = $sth->fetchrow_array) {
$name =~ s/ /_/g;
$cat .= "<a href=$CFG->{build_root_url}/$name>$name</a> ";
}
return $cat;
}


Since $cat was always overwritten (not concatenated), finally you always got displayed the last link value, only.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...

Last edited by:

webmaster33: Apr 14, 2005, 6:00 PM
Quote Reply
Re: [webmaster33] Showing all categories a link belongs to In reply to
I didn't realise it was such a small change I needed ... but that was exacly what I wanted Cool

I've changed the global slightly:

Code:
sub {
my ($rec) = @_;
my $id = $rec->{ID};
my $db = $DB->table ('Category','CatLinks');
my $sth = $db->select ( { 'CatLinks.LinkID' => $id }, ['Category.ID', 'Category.Name', 'Category.Full_Name'] );
my $cat;
while (my ($id,$name,$fullname) = $sth->fetchrow_array) {
$name =~ s/ /_/g;
$cat .= "<a href=\"$CFG->{build_root_url}/$fullname\">$name</a> ";
}
return $cat;
}

This shows the category name itself, linked correctly even if it's a subcategory (the code above would show "Category/Subcategory"). Also, it now inserts "" around the hrefs (necessary for valid xhtml)

John
Quote Reply
Re: [gotze] Showing all categories a link belongs to In reply to
This is a nice global, I have 2 questions:

My categories show up as "Category_Name", How do I get them to show up as "Category Name"?

I also have a column in my category table called sm_graphic which contains a link to a graphic for that category.... How would I get that to appear with the category name?

George
Quote Reply
Re: [macbethgr] Showing all categories a link belongs to In reply to
Thanks,
After looking at it closer I was able to figure it out...