Hmm, let's clean that up a bit. Try:
my $tags = shift;
my $limit = ($tags->{Random_Limit} =~ /^(\d+)$/) ? $1 : 3;
my @parents = $IN->param('Random_CatID');
my @cat_ids;
foreach my $id (@parents) {
push @cat_ids, $id, $DB->table('Category')->children($id);
}
my $link_db = $DB->table('Links','CatLinks');
$link_db->select_options ('ORDER BY RAND()', "LIMIT $limit");
my $condition = GT::SQL::Condition->new( 'Description', 'IS NOT', \'NULL','isValidated','=','Yes');
if (@cat_ids) {
$condition->add('CategoryID', 'IN', \@cat_ids);
}
my $sth = $link_db->select($condition);
my @output;
while (my $hash = $sth->fetchrow_hashref) {
push @output, $hash;
}
return { Random_Loop => \@output }
}
This is a little different in that it will pick random link from the category you pass in, or any subcategory, not just the first level of subcategory.
To call this, just do:
<!--#include virtual="/cgi-bin/page.cgi?page=random&Random_CatID=1&Random_CatID=2"-->
to get 3 random links from anywhere inside category 1 or 2.
Cheers,
Alex
--
Gossamer Threads Inc.
Code:
sub { my $tags = shift;
my $limit = ($tags->{Random_Limit} =~ /^(\d+)$/) ? $1 : 3;
my @parents = $IN->param('Random_CatID');
my @cat_ids;
foreach my $id (@parents) {
push @cat_ids, $id, $DB->table('Category')->children($id);
}
my $link_db = $DB->table('Links','CatLinks');
$link_db->select_options ('ORDER BY RAND()', "LIMIT $limit");
my $condition = GT::SQL::Condition->new( 'Description', 'IS NOT', \'NULL','isValidated','=','Yes');
if (@cat_ids) {
$condition->add('CategoryID', 'IN', \@cat_ids);
}
my $sth = $link_db->select($condition);
my @output;
while (my $hash = $sth->fetchrow_hashref) {
push @output, $hash;
}
return { Random_Loop => \@output }
}
This is a little different in that it will pick random link from the category you pass in, or any subcategory, not just the first level of subcategory.
To call this, just do:
<!--#include virtual="/cgi-bin/page.cgi?page=random&Random_CatID=1&Random_CatID=2"-->
to get 3 random links from anywhere inside category 1 or 2.
Cheers,
Alex
--
Gossamer Threads Inc.