Gossamer Forum
Quote Reply
Random Adjustment
Hi,

This global gives a random link according to the category number given in 'my $cat_id'
This works fine but I wondered if any cleaver programmers can make this give the random link from the top category listed and its subcategories. As it is, it only gives the random link from the 1 category listed.



sub {
my $tags = shift;
my $link_db = $DB->table('Links','CatLinks');
my $cat_id = 1;
my $limit = $tags->{Random_Limit} || 1;
my (@output, $sth);
$link_db->select_options ('ORDER BY RAND()', "LIMIT $limit");
$sth = $link_db->select ( { CategoryID => $cat_id });
while (my $hash = $sth->fetchrow_hashref) {
push @output, $hash;
}
return { Random_Loop => \@output }
}

--------------------------------
Privacy Software
Quote Reply
Re: [BLOOD] Random Adjustment In reply to
Can no one suggest how to get the random links from the subcategories of the main category? There are solutions to this elsewhere BUT only for calling the global via SSI.

Any ideas?

--------------------------------
Privacy Software
Quote Reply
Re: [BLOOD] Random Adjustment In reply to
Ok I have this working now. Smile

--------------------------------
Privacy Software
Quote Reply
Re: [BLOOD] Random Adjustment In reply to
Hi,

When you get something like this working, please post the working version, otherwise you'll drive people nuts when they find this message later on :)


PUGDOG´┐Ż Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] Random Adjustment In reply to
 
I agree!

I managed to hack around with the ideas given here:

http://www.gossamer-threads.com/...i?post=209030#209030

to produce this:

sub {
my $tags = shift;
my $link_db = $DB->table('Links','CatLinks');
my $cat_id = $tags->{Random_CatID};
my $limit = $tags->{Random_Limit} || 1;
my (@output, $sth);
$link_db->select_options ('ORDER BY RAND()', "LIMIT $limit");
if ($cat_id) {
$sth = $link_db->select ( { CategoryID => $cat_id });
}
else {
$sth = $link_db->select;
}
while (my $hash = $sth->fetchrow_hashref) {
push @output, $hash;
}
my $cat_db = $DB->table('Category');
my $sth2 = $cat_db->select ( ['ID'],{ FatherID => $cat_id });
while (my ($child_id) = $sth2->fetchrow_array){
my $sth3 = $link_db->select ( { CategoryID => $child_id, isValidated => 'Yes' });
while (my $hash2 = $sth3->fetchrow_hashref) {
push @output, $hash2;

}
@output = sort {return (rand > 0.5) ? 1 : -1;} @output;
splice (@output,$limit);
}
return { Random_Loop => \@output }
}


then call the random's via this:

<%set Random_CatID = 1%>
<%Rand_Link%>
<%loop Random_Loop%>
<%Title%>
<%endloop%>

-------------------------------

It may not be the most efficient code, but it works!

--------------------------------
Privacy Software
Quote Reply
Re: [BLOOD] Random Adjustment In reply to
Well done on working this out. Using the post from Alex at the bottom of the thread you gave, I think this is more straightforward (hope there aren't too many errors):

sub {
my $tags = shift;
my $link_db = $DB->table('Links','CatLinks');
my $cat_id = $tags->{Random_CatID};
my $all_ids = $DB->table('Category')->children($cat_id);
push @$all_ids, $cat_id;
my $limit = $tags->{Random_Limit} || 1;
my (@output, $sth);
$link_db->select_options ('ORDER BY RAND()', "LIMIT $limit");
my $cond = GT::SQL::Condition->new('isValidated','=','Yes', 'CategoryID', 'IN', \@$all_ids);
if ($cat_id) {
$sth = $link_db->select ( $cond);
}
else {
$sth = $link_db->select({isValidated=>'Yes'});
}
while (my $link = $sth->fetchrow_hashref) {
push @output, $link;
}
return { Random_Loop => \@output }
}