Gossamer Forum
Home : Products : Gossamer Links : Discussions :

tags to non "Links" table

Quote Reply
tags to non "Links" table
I would like to use a template tag that would point to a value in the CatLinks database where I created a new field. I imagine it must be by using global variable but not sure how, especially how to make it list only the value that matches the unique LinkID CategoryID...thnk

Quote Reply
Re: tags to non "Links" table In reply to
Hi,

The CatLinks table is not loaded by default. You would want to add a global that does something like:

sub {
my $tags = shift;
my $link_id = $tags->{ID} or return;
my $catlnk_db = $DB->table('CatLinks');
my $sth = $catlnk_db->select ( { LinkID => $link_id } );
my $results = $sth->fetchrow_hashref;
...
return $output;
}

or something similiar..

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: tags to non "Links" table In reply to
It didn't work but I don't think I understood everything in the code. Perhaps you can help me understand this.
First of all, does it matter what data type the new field I added is?
In Reply To:
sub {
my $tags = shift;
This line initializes that there are tags?
In Reply To:
my $link_id = $tags->{ID} or return;
Is $link_id have to be the TAGNAME I will use? What does "ID" refer to here? Is it the name of the field?
In Reply To:
my $catlnk_db = $DB->table('CatLinks');
This line connects to the correct table?
In Reply To:
my $sth = $catlnk_db->select ( { LinkID => $link_id } );
$sth, is this a special type of call into the dB or could I name the variable $whatever and it would work just the same?
In Reply To:
my $results = $sth->fetchrow_hashref;
maybe this is where my problem is? Does this result matter if we're dealing with different fieldtypes?

-
Thanks.

p.s. sorry but please reply to this at http://www.gossamer-threads.com/perl/forum/showflat.pl?Cat=&Board=LSQLNG&Number=142779&page=0&view=collapsed&sb=5 instead of here. Sorry for the dupe question, but the subject here was bad, I'm sure more people would be interested in this mod so I explained the whole thing there...
Quote Reply
Re: tags to non "Links" table In reply to
Hi,

First off, just a little context here. This function will be saved in the Template Globals as:

tag_name => sub { .. }

in the admin. Now when you use <%tag_name%> in any template, it will run the given function.

my $tags = shift;

The first argument to the function will be the list of other tags on the template. So if you had <%tag_name%> in the link.html template, in $tags would be a hash of all the fields available in link.html.

my $link_id = $tags->{ID} or return;

This assumes that on the template you are using, you have the ID number of the link you want. This may be wrong, as I'm not sure exactly what you are doing. If there is no ID tag on this page, then we return as there is not much we can do.

my $catlnk_db = $DB->table('CatLinks');

This gives you a GT::SQL::Table object that works off of the CatLinks table.

my $sth = $catlnk_db->select ( { LinkID => $link_id } );

This selects out of the database the row where LinkID = the ID on this template.

my $results = $sth->fetchrow_hashref;

This returns the results as a hash. So it would look like { LinkID => 5, CategoryID => 3, CustomColumn => ... }.

You can now either return what you want printed, or if you just return the hash, then you will have <%LinkID%>, <%CategoryID%> and <%CustomColumn%> tags available on the page.

Let me know if this makes sense,

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: tags to non "Links" table In reply to
Yes it does make sense, thanks so much! So the only info I'm missing to try this out is how do I output ONLY "CustomColumn" to the tag?

p.s., hmm maybe I didn't get it, as you said that the custom column tag would still be available...so I have to call the function in the template? How do I do that? After calling the function I can just use the custom column name as it appears in the table?
Quote Reply
Re: tags to non "Links" table In reply to
Well, as the last line you could do:

return { CustomColumn => $results->{CustomColumn} };

Then you would have added the tag <%CustomColumn%> to that page.

Cheers,

Alex

--
Gossamer Threads Inc.
Quote Reply
Re: tags to non "Links" table In reply to
Thanks, ok almost got it all but it didn't work. Let me state everything I did exactly, maybe that will explain what went wrong:

In CatLinks, I created a new field called "Featured", Integer. Then I took a unique Link Identified by the 2 fields in CatLinks by

LinkID=100
CategoryID=25

And added the value

Featured=1

Then I created a Global Tag called
"Priority" with the following sub in it:

sub {
my $tags = shift;
my $link_id = $tags->{ID} or return;
my $catlnk_db = $DB->table('CatLinks');
my $sth = $catlnk_db->select ( { LinkID => $link_id } );
my $results = $sth->fetchrow_hashref;
return { Featured => $results->{Featured} };
}

and then I modified the Link.html template to include a tag called

<%Priority%>

and looked for it in Category 25. I expected (or let's say more like prayed for :) ) a "1" there. But there was nothing.


Quote Reply
Re: tags to non "Links" table In reply to
Hi,

Since you did:

return { Featured => $results->{Featured} };

the tag <%Priority%> does nothing. It runs the code and makes new tags available. What you need to do is anywhere after <%Priority%> you would put <%Featured%>. Since you are only setting one thing, you should really just do:

return $results->{Featured};

and that way, when you call <%Priority%> it will return what is in the Featured column.

Cheers,

Alex

--
Gossamer Threads Inc.