Gossamer Forum
Home : Products : Gossamer Links : Discussions :

Alex : Globals

Quote Reply
Alex : Globals
Hi Alex,

I have been trying to create a global that reads my data from a new table in LinkSQL, I was able to do so using the following code (the global name is realated_news) :

Code:
sub {
my $tags = shift;
my $related_db = $DB->table("Relations");
my $id = $tags->{ID};
my $related_news = $related_db->get($id, "HASH");
return $related_news;
}

I have 3 fields in my Relations table, the first one is called ID (primary key) Second one is LinkID and the third one is RelationTitle.

The tag that I am using is <%related_news%><%RelationTitle%> and this shows the RelationTitle field based on the ID field!

Now the problem is that I want to be able to do the above using the LinkID field instead of the ID field (primary key).

I tried to replace
Code:
my $id = $tags->{ID};

with

Code:
my $id = $tags->{LinkID};

but it gives me an error "Unknown Tag: 'RelationTitle'"

Please help me with this!

Thanks,
PCMANIA

Last edited by:

pcmania: Jan 29, 2002, 8:10 AM
Quote Reply
Re: [pcmania] Alex : Globals In reply to
Hi,

You should return a scalar , not a hash ...e.g. :

return $related_news->{RelationTitle};

then you can call a tag like <%related_news%> in your templates (assuming 'related_news' was name of your sub)

Cheers,

jean@gossamer-threads.com
Quote Reply
Re: [jean] Alex : Globals In reply to
Hi Jean,

Thanks for the help, it works fine now however I am facing a new problem. The problem is that this global only shows one of the results, how am I suppose to make a loop for this global?

Code:
sub {
my $tags = shift;
my $link_id = $tags->{ID} or return;
my $related_db = $DB->table('Relations');
my $sth = $related_db->select ( { LinkID => $link_id } );
my $results = $sth->fetchrow_hashref;
return $results->{RelationTitle};
}

Thanks in advance,
PCMANIA

Last edited by:

pcmania: Jan 30, 2002, 8:33 AM
Quote Reply
Re: [pcmania] Alex : Globals In reply to
How about looping within the global and returning the final output?
Quote Reply
Re: [RedRum] Alex : Globals In reply to
Hi RedRum,

Thanks for the quick respose. I am not a programmer and I have spent more than 2 days to come up with the above code. So I have no idea how to write loops and other stuff. I would appreciate it if you could write the code for me in here.

Thanks,
PCMANIA

Last edited by:

pcmania: Jan 30, 2002, 8:37 AM
Quote Reply
Re: [pcmania] Alex : Globals In reply to
How are you wanting to display the return value?

If you want something simple you could do:

return join "<BR>", keys %$results;
Quote Reply
Re: [RedRum] Alex : Globals In reply to
Hi,

I changed my code to
Code:
sub {
my $tags = shift;
my $link_id = $tags->{ID} or return;
my $related_db = $DB->table('Relations');
my $sth = $related_db->select ( { LinkID => $link_id } );
my $results = $sth->fetchrow_hashref;
return join "<BR>", keys %$results;
}

but it is not showing any results! Am I suppose to change my tag inside the template as well? I am currently using <%related_news%> (The name of this global is related_news)

Thanks again,
PCMANIA
Quote Reply
Re: [pcmania] Alex : Globals In reply to
You can try with the simple code below:
Code:
sub {
my $tags = shift;
my $link_id = $tags->{ID} or return;
my $related_db = $DB->table('Relations');
my $sth = $related_db->select ( { LinkID => $link_id } );
my $results;
while (my $row = $sth->fetchrow_hashref) {
$results .= '<BR>'.$row->{RelationTitle};
#add your custom code here
}
return $results;
}
Cheers,
jean@
Quote Reply
Re: [pcmania] Alex : Globals In reply to
From your original, change:

my $id = $tags->{ID};
my $related_news = $related_db->get($id, "HASH");

to:

my $link_id = $tags->{ID};
my $related_news = $related_db->select( { LinkID => $link_id })->fetchrow_hashref;

and it should work.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Alex : Globals In reply to
Hi,

The following code works just fine however it only shows the RelationTitle field as the result:

Code:
sub {
my $tags = shift;
my $link_id = $tags->{ID} or return;
my $related_db = $DB->table('Relations');
my $sth = $related_db->select ( { LinkID => $link_id } );
my $results;
while (my $row = $sth->fetchrow_hashref) {
$results .= '<BR>'.$row->{RelationTitle};
#add your custom code here
}
return $results;
}

However I am interested in an output like this:

Code:
<br><a href="http://www.mysite.com/RelationID.html"><font color="#000000" face="Tahoma" size="2">RelationTitle</font></a>

Please note that RelationID is another field in this Table.

I know that I should replace $results with some other code but I am not sure how to do it. I would appreciate it if you can help me with this.

Thanks in advance,
PCMANIA
Quote Reply
Re: [pcmania] Alex : Globals In reply to
Try:

Code:
while (my $row = $sth->fetchrow_hashref) {
$results .= qq|<br><a href="http://www.mysite.com/$row->{RelationID}.html"><font color="#000000" face="Tahoma" size="2">$row->{RelationTitle}</font></a>|;
}
Quote Reply
Re: [RedRum] Alex : Globals In reply to
Thank you very much RedRum, you are the man!