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
May 22, 2001, 1:11 PM
Administrator (9387 posts)
May 22, 2001, 1:11 PM
Post #2 of 8
Views: 1901
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.
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.
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?
This line initializes that there are tags?
Is $link_id have to be the TAGNAME I will use? What does "ID" refer to here? Is it the name of the field?
This line connects to the correct table?
$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?
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...
First of all, does it matter what data type the new field I added is?
This line initializes that there are tags?
Is $link_id have to be the TAGNAME I will use? What does "ID" refer to here? Is it the name of the field?
This line connects to the correct table?
$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?
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...
May 23, 2001, 10:25 AM
Administrator (9387 posts)
May 23, 2001, 10:25 AM
Post #4 of 8
Views: 1872
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.
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.
May 23, 2001, 11:55 AM
User (108 posts)
May 23, 2001, 11:55 AM
Post #5 of 8
Views: 1875
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?
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?
May 23, 2001, 12:00 PM
Administrator (9387 posts)
May 23, 2001, 12:00 PM
Post #6 of 8
Views: 1894
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.
return { CustomColumn => $results->{CustomColumn} };
Then you would have added the tag <%CustomColumn%> to that page.
Cheers,
Alex
--
Gossamer Threads Inc.
May 24, 2001, 10:01 PM
User (108 posts)
May 24, 2001, 10:01 PM
Post #7 of 8
Views: 1836
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.
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.
May 27, 2001, 8:27 PM
Administrator (9387 posts)
May 27, 2001, 8:27 PM
Post #8 of 8
Views: 1818
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.
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.