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

Re: [VishalT] [Is this possible?] : Include Random Links that with certain link-properties?

Quote Reply
Re: [VishalT] [Is this possible?] : Include Random Links that with certain link-properties? In reply to
load_random_partners_in_cat

Code:
sub Random_Link_In_Category {

my $cat = $_[0];
my $limit = $_[1] || ;
my $all_ids = $DB->table('Category')->children($cat);
push @$all_ids, $cat;

my $db_obj = $DB->table('Links','CatLinks','Category');
$db_obj->select_options ("ORDER BY RAND() LIMIT $limit");

my $cond = GT::SQL::Condition->new('CategoryID', 'IN', $all_ids);
my $cond2 = GT::SQL::Condition->new('isValidated','=','Yes','is_partner','=','Yes');
my $sth = $db_obj->select (['Links.*'], $cond, $cond2 ) || die $GT::SQL::error;

my @cats;
while (my $hit = $sth->fetchrow_hashref) {

if ($CFG->{build_detailed}) { $hit->{detailed_url} = $CFG->{build_detail_url} . "/" . $DB->table('Links')->detailed_url( $hit->{ID} ); }

if ($hit->{isNew} eq "Yes") { $hit->{isNew} = 1; } else { $hit->{isNew} = 0; }
if ($hit->{isPopular} eq "Yes") { $hit->{isPopular} = 1; } else { $hit->{isPopular} = 0; }
if ($hit->{isChanged} eq "Yes") { $hit->{isChanged} = 1; } else { $hit->{isChanged} = 0; }

push @cats, $hit;
}

return { random_partner_link_in_this_cat => \@cats }

}

Code:
<%load_random_partners_in_cat($ID,4)%>
<%if random_partner_link_in_this_cat.length%>
<%loop random_partner_link_in_this_cat%>
<%include link.html%>
<%endloop%>
<%endif%>

Something like that should work. That will get matches from ANY sub-category as well. If you only want it to find links in the actual category ID you are looking at, then something like this:

Code:
sub Random_Link_In_Category {

my $cat = $_[0];
my $limit = $_[1] || 1;

my $db_obj = $DB->table('Links','CatLinks','Category');
$db_obj->select_options ("ORDER BY RAND() LIMIT $limit");

my $cond = GT::SQL::Condition->new('CategoryID', '=', $cat);
my $cond2 = GT::SQL::Condition->new('isValidated','=','Yes','is_partner','=','Yes');
my $sth = $db_obj->select (['Links.*'], $cond, $cond2 ) || die $GT::SQL::error;

my @cats;
while (my $hit = $sth->fetchrow_hashref) {

if ($CFG->{build_detailed}) { $hit->{detailed_url} = $CFG->{build_detail_url} . "/" . $DB->table('Links')->detailed_url( $hit->{ID} ); }

if ($hit->{isNew} eq "Yes") { $hit->{isNew} = 1; } else { $hit->{isNew} = 0; }
if ($hit->{isPopular} eq "Yes") { $hit->{isPopular} = 1; } else { $hit->{isPopular} = 0; }
if ($hit->{isChanged} eq "Yes") { $hit->{isChanged} = 1; } else { $hit->{isChanged} = 0; }

push @cats, $hit;
}

return { random_partner_link_in_this_cat => \@cats }

}

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!
Subject Author Views Date
Thread [Is this possible?] : Include Random Links that with certain link-properties? VishalT 1835 Apr 15, 2021, 11:43 PM
Thread Re: [VishalT] [Is this possible?] : Include Random Links that with certain link-properties?
Andy 1771 Apr 17, 2021, 1:17 AM
Post Re: [Andy] [Is this possible?] : Include Random Links that with certain link-properties?
VishalT 1763 Apr 17, 2021, 1:52 AM