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

Fetch Category Name

(Page 1 of 2)
> >
Quote Reply
Fetch Category Name
Hi,

I need a small global that will return the Category name (only the name - not like title_linked).

I will pass CategoryID - so something like:

Code:
<%fetch_name($CategoryID)%>

I have tried a few things but keep getting an error

Cheers
Klaus

http://www.ameinfo.com
Quote Reply
Re: [klauslovgreen] Fetch Category Name In reply to
Code:
sub {
my $id = shift;
my $cat = $DB->table('Category')->get($id);
return $cat ? $cat->{Name} : '';
}

Ivan
-----
Iyengar Yoga Resources / GT Plugins

Last edited by:

yogi: Mar 19, 2003, 12:18 AM
Quote Reply
Re: [yogi] Fetch Category Name In reply to
Hi Ivan,

It does not return anything?

Klaus

http://www.ameinfo.com
Quote Reply
Re: [klauslovgreen] Fetch Category Name In reply to
It should of course be 'Name' and not 'Title'.

I've edited the global.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Fetch Category Name In reply to
I should have picked up on that myself - thanks for your help Ivan. works :-)

Klaus

http://www.ameinfo.com
Quote Reply
Re: [klauslovgreen] Fetch Category Name In reply to
BTW: there is a lot of information about the GT modules in the 'Help' section of Links SQL. In the section 'GT module documentation', you will find a description of all the modules. Look for example in GT::SQL::Table. This helps you in writing your own globals Wink.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Fetch Category Name In reply to
Cheers Ivan :-)

http://www.ameinfo.com
Quote Reply
Re: [yogi] Fetch Category Name In reply to
How about:

Code:
sub {
return $DB->table('Category')->select('Name', { ID => $_[0] })->fetchrow;
}
Quote Reply
Re: [Paul] Fetch Category Name In reply to
That gives an error if the category does not exist.

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Fetch Category Name In reply to
Hmm did you test it?.....I would have expected it to just return nothing.
Quote Reply
Re: [Paul] Fetch Category Name In reply to
I haven't tested it, but I would expect something like:
Can't call method 'fetchrow' on an undefined value at ....

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Fetch Category Name In reply to
I think that would only happen if the SQL syntax caused an error or something like that....if there is no ID matching the query it would just return blank.

It's the same with an update, you could do:

UPDATE lsql_Links SET Title = 'Foo' WHERE ID = '10000000000000000'

...that would not cause an error, it would just return 0 rows affected, but if you changed "Title" to a non existant column name then you'd get an error.
Quote Reply
Re: [Paul] Fetch Category Name In reply to
any advantage in using your solution Paul?

Klaus

http://www.ameinfo.com
Quote Reply
Re: [klauslovgreen] Fetch Category Name In reply to
Nothing major, it is just a little shorter and it only selects the category name instead of the whole row so it should be a little quicker.
Quote Reply
Re: [Paul] Fetch Category Name In reply to
Cheers Paul - works well :-)

Klaus

http://www.ameinfo.com
Quote Reply
Re: [yogi] Fetch Category Name In reply to
By hacking up the Build.pm file, I was able to get a tag to show just the Category name in a detailed template.

However, I think modifying the global would be better. Any ideas?

Last edited by:

Katana Man: Mar 25, 2003, 8:18 PM
Quote Reply
Re: [Paul] Fetch Category Name In reply to
As a start, I've tried
Code:
sub {
my $id = shift;
my $cat = $DB->table('CatLinks')->get($id);
return $cat ? $cat->{LinksID} : '';
}


but even this won't return the CategoryID number. I am quite suprised that something like <%CategoryName%> does'nt work on every page.
Quote Reply
Re: [Katana Man] Fetch Category Name In reply to
Well using LinksID won't return a category number, mainly because there's no such column, it's LinkID, but secondly because that's the link id, the category is CategoryID
Quote Reply
Re: [Paul] Fetch Category Name In reply to
Thanks Paul. I did use LinkID, I just typed it wrong in the post, sorry. I tried all other combinations and I still can't get a response from it :(

Oh well. I think I may just add an additional redundant Category field to each link since I want to sort by it on the search results page. Even if I got this global to kick back the Category name for various templates, I still wouldn't be able to sort by it. I must be missing something, because my solution of adding a redundant Category field is not efficient at all.
Quote Reply
Re: [Katana Man] Fetch Category Name In reply to
Hi,

You should just be able to do a select, and combine the two tables. I forget the statement syntax, but Alex has posted it several times.

You want to join the Links table and the CatLinks table. Depending on how it's joined (I'm not sure the default) you can either attach a link record to each ID in the catlinsk table, or you can attach the first CategoryID to the Link in the Links table.

While I'm not sure if it will do what you want, sometimes tweaking the code in Links core code is still the only way to make it do what you want. Just keep a list of the tweaks so you can make them again when the new versions come out.

Once you get the CategoryID attached to the Link, you can pass that to one of the routines that picks off the category ID and turns it into a formatted name string, or you can "get" the category record in full using that ID.


BTW: the reason this isn't in the "core" code, is that a link can be a member of several categories, and it was a major rewrite to make it possible <G> Once Alex did that, everyone started asking for the "old" way <G>. The new way, if you only let links be in one category, can be used if you do the join between Links and CatLinks, while it required major contortions to allow multiple categories using the old system of attaching the category ID directly to the link.


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.

Last edited by:

pugdog: Mar 26, 2003, 11:53 AM
Quote Reply
Re: [Katana Man] Fetch Category Name In reply to
Can you just explain again what you need...I can probably help, I'm just not sure exactly what you need.
Quote Reply
Re: [Paul] Fetch Category Name In reply to
I need to be able to display the Category name for a given link. Anywhere, on any template.

I also need to sort by the Category name on the search results page.
Quote Reply
Re: [Katana Man] Fetch Category Name In reply to
Regarding number one, this should work as you want:

Code:
sub {
return $DB->table('CatLinks','Links')->select('Name', { LinkID => $_[0] })->fetchrow;
}

...call it like:

<%your_global($ID)%>
Quote Reply
Re: [Paul] Fetch Category Name In reply to
Can't call method "fetchrow" on an undefined value at (eval 20) line 1.

Thanks for trying to help. However, if I can't get the sort to work, then there is no sense in making the global.

Last edited by:

Katana Man: Mar 26, 2003, 1:19 PM
Quote Reply
Re: [Katana Man] Fetch Category Name In reply to
Whoops my fault, add 'Category' into the table() call instead of 'Links'

To sort by category have you tried passing sb=Name into search.cgi? (or whatever the sort by field is called).
> >