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

X Number of links from each sub category on the category page.

Quote Reply
X Number of links from each sub category on the category page.
Hi All,


I have the following global that I'm using on the category template to bring back x number of links from all the subcategories (I have it set to 40), this works fine, however I want to set the script to bring back 5 links from each category.
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 ('GROUP BY LinkID ORDER BY Add_Date DESC Limit 40');
$sth = $search_db->select (['Links.ID', 'Links.Description', 'Links.Title', 'Links.Add_Date', 'Category.Full_Name', 'Category.Name', 'Links.Hits'], GT::SQL::Condition->new(['Full_Name', 'LIKE', $cat .'%'], ['isValidated', '=', 'Yes']));


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->{db_cgi_url}."/jump.cgi?ID=".$link->{'ID'};


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


}


return $output;
}
Anyone have any ideas?

Thanks!
George
Quote Reply
Re: [macbethgr] X Number of links from each sub category on the category page. In reply to
Hi George,

Here is the code that you can play. I did not test it yet but it should work.

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


use GT::SQL::Condition;


my $catdb = $DB->table('Category');
my $catids = $catdb->children($tags->{category_id});
unshift(@$catids, $tags->{category_id});



my $search_db = $DB->table('Links','CatLinks','Category');
$search_db->select_options ('GROUP BY LinkID ORDER BY Add_Date DESC Limit 5');


foreach my $catid (@$catids){

$sth = $search_db->select (['Links.ID', 'Links.Description', 'Links.Title', 'Links.Add_Date', 'Category.Full_Name', 'Category.Name', 'Links.Hits'], GT::SQL::Condition->new(['CategoryID', '=', $catid ], ['isValidated', '=', 'Yes']));


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->{db_cgi_url}."/jump.cgi?ID=".$link->{'ID'};


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


}
}

return $output;
}

Cheers,

Dat

Programming and creating plugins and templates
Blog
Quote Reply
Re: [tandat] X Number of links from each sub category on the category page. In reply to
Thanks Dat,

It does look like it should work, but it gave me the same results.

George
Quote Reply
Re: [macbethgr] X Number of links from each sub category on the category page. In reply to
Hi George,

It will be the same results if you have just 5 links in each sub categories. But if each sub categories have more then 5 links, the results should be different.

Cheers,

Dat

Programming and creating plugins and templates
Blog
Quote Reply
Re: [tandat] X Number of links from each sub category on the category page. In reply to
LOL.....

Of course, when there are only 2 links in a sub-category only 2 links can be returned, if there are 5 links, then 5 links are returned, some categories have hundreds of links, and yes.... hundreds are being returned all being returned. In all cases, all links are being returned.

Thanks
Quote Reply
Re: [macbethgr] X Number of links from each sub category on the category page. In reply to
Hi,

Mm, that would make it quite a slow global. It would need to first get the category IDs, then do a search for each category individually , and put them into the same loop.

It could also be done using a more advanced mySQL query, but I'm afraid I don't have time to play around with that Angelic

Anyway, this is untested , but should work:

get_top_from_each_subcat
Code:
sub {
my $full_name = $_[0];
my $output;
my $cat = $DB->table('Category');
my $search_db = $DB->table('Links','CatLinks');

my $sth = $search_db->select( GT::SQL::Condition->new('Full_Name', 'LIKE', "$cat%") );
while (my $cat = $sth->fetchrow_hashref) {

$search_db->select_options ('ORDER BY Add_Date DESC Limit 5');
my $link_sth = $search_db->select( { 'isValidated', '=', 'Yes' } ) || die $GT::SQL::error;
while (my $link = $link_sth->fetchrow_hashref) {
Links::SiteHTML::tags('link',$link);
$output .= Links::SiteHTML::display('link', { %$cat, %$link });
}
}

return $output;

}

You need to call it a little bit different to how you were before. When calling the global in the template, you need to also pass along $Full_Name as well now. For example:

Code:
<%get_top_from_each_subcat($Full_Name)%>

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] X Number of links from each sub category on the category page. In reply to
Thanks! This all puts me on the right track!