Gossamer Forum
Quote Reply
category Links
Hi,

I am using the below global to get the latest 8 links i the current category - how can I pass a specific category to is as <%category_link('x')%> where x is the ID of the category I want the latest links from?

Code:
sub {
my $tags = shift;
my $cat = $tags->{'Full_Name'};
my $output;
my $sth;
my $link;

use GT::SQL::Condition;


my $search_db = $DB->table('Links','CatLinks','Category');
$search_db->select_options ('ORDER BY Add_Time DESC Limit 8');
$sth = $search_db->select (['Links.ID', 'Links.Description', 'Links.Title', 'Links.Add_Date', 'Links.isNew', 'Links.Image', 'Links.Story', 'Links.Add_Time', 'Links.Country', 'Category.Full_Name', 'Category.Name'], GT::SQL::Condition->new(['Full_Name', 'LIKE', $cat .'%'], ['isValidated', '=', 'Yes']));

my $output = qq~<br><hr>~;

while ($link = $sth->fetchrow_hashref)
{
# Set the category url
my $category_url = $CFG->{build_root_url}."/".$link->{'Full_Name'}."/";

# Set the detailed_url
my $detailed_url = "$CFG->{build_detail_url}/$link->{'ID'}$CFG->{build_extension}";

$output .= Links::SiteHTML::display('link', $link);
}

return $output;
}

http://www.ameinfo.com
Quote Reply
Re: [klauslovgreen] category Links In reply to
Replace

my $tags = shift;
my $cat = $tags->{'Full_Name'};

with

my $cat = shift;

Then you can remove the condition and just have

{'CategoryID' => $cat, 'isValidated' => 'Yes'}

in its place.

If you want to have the new links from the a category and all its subcategories then add

my $all_ids = $DB->table('Category')->children($cat);
push @$all_ids, $cat;

at the top of the global after the $cat line and replace your original condition with

GT::SQL::Condition->new(['CategoryID', 'IN', $all_ids], ['isValidated', '=', 'Yes'])
Quote Reply
Re: [afinlr] category Links In reply to
Hi Laura,

Could not make it work - returns errors - could I ask you a favor - show the global as it should you in your opinion?

I need the version that list the X latest links from a category and all subcategories if any...

Cheers
Klaus

http://www.ameinfo.com
Quote Reply
Re: [klauslovgreen] category Links In reply to
Its easier for me if you paste what you have and I'll try to spot the error. Wink

Otherwise, I have written this global lots of times for slightly differing requirements. If you do a search for global and posted by afinlr you should find a few examples.
Quote Reply
Re: [afinlr] category Links In reply to
This is what I tried (and got error):

I call it as <%frontpage('1')%> where 1 is the category (and subcategories) I want the latest 2 links from. I need all the fields in each link available..

Thanks in advance
Klaus

Code:
sub {
my $cat = shift;
my $output;
my $sth;
my $link;
my $all_ids = $DB->table('Category')->children($cat);
push @$all_ids, $cat;

use GT::SQL::Condition->new(['CategoryID', 'IN', $all_ids], ['isValidated', '=', 'Yes'])

my $search_db = $DB->table('Links','CatLinks','Category');
$search_db->select_options ('ORDER BY Add_Time DESC Limit 2');
$sth = $search_db->select (['Links.ID', 'Links.Description', 'Links.Title', 'Links.Add_Date', 'Links.isNew', 'Links.Image', 'Links.Story', 'Links.Add_Time', 'Links.Country', 'Category.Full_Name', 'Category.Name'], GT::SQL::Condition->new(['Full_Name', 'LIKE', $cat .'%'], ['isValidated', '=', 'Yes']));

my $output = qq~<br><hr>~;

while ($link = $sth->fetchrow_hashref)
{
# Set the category url
my $category_url = $CFG->{build_root_url}."/".$link->{'Full_Name'}."/";

# Set the detailed_url
my $detailed_url = "$CFG->{build_detail_url}/$link->{'ID'}$CFG->{build_extension}";

$output .= Links::SiteHTML::display('link', $link);
}

return $output;
}

http://www.ameinfo.com
Quote Reply
Re: [klauslovgreen] category Links In reply to
Right, the first thing I've spotted is that I obviously didn't explain the condition replacement clearly enough - you have a strange use statement (which is also missing a semicolon) for the condition instead of replacing the actual condition in the select statement. Does this work?

sub {
my $cat = shift;
my $output;
my $sth;
my $link;
my $all_ids = $DB->table('Category')->children($cat);
push @$all_ids, $cat;

use GT::SQL::Condition;

my $search_db = $DB->table('Links','CatLinks','Category');
$search_db->select_options ('ORDER BY Add_Time DESC Limit 2');
$sth = $search_db->select (['Links.ID', 'Links.Description', 'Links.Title', 'Links.Add_Date', 'Links.isNew', 'Links.Image', 'Links.Story', 'Links.Add_Time', 'Links.Country', 'Category.Full_Name', 'Category.Name'], GT::SQL::Condition->new(['CategoryID', 'IN', $all_ids], ['isValidated', '=', 'Yes']) );

my $output = qq~<br><hr>~;

while ($link = $sth->fetchrow_hashref)
{
# Set the category url
my $category_url = $CFG->{build_root_url}."/".$link->{'Full_Name'}."/";

# Set the detailed_url
my $detailed_url = "$CFG->{build_detail_url}/$link->{'ID'}$CFG->{build_extension}";

$output .= Links::SiteHTML::display('link', $link);
}

return $output;
}
Quote Reply
Re: [afinlr] category Links In reply to
Sure did - thanks Smile

http://www.ameinfo.com
Quote Reply
Re: [afinlr] category Links In reply to
One more question - what if I wanted to use loop instead of the link.html template?

Klaus

http://www.ameinfo.com
Quote Reply
Re: [klauslovgreen] category Links In reply to
Change the

my $output;

to

my @list;

then replace

$output .= Links::SiteHTML::display('link', $link);

with

push @list, $link;

And finally, you'll need to return the list:

return {New_Links_Loop => \@list};

You'll need to call this global in your page somewhere before you use the loop.
Quote Reply
Re: [afinlr] category Links In reply to
Thanks - so I would call it as

<%newlinks('1')%>
<%loop New_Links_Loop%>
<%title%> etc...
<%endloop%>

Right?

Klaus

http://www.ameinfo.com
Quote Reply
Re: [klauslovgreen] category Links In reply to
Exactly.
Quote Reply
Re: [afinlr] category Links In reply to
Thanks for your help Laura

Klaus

http://www.ameinfo.com