Gossamer Forum
Home : Products : Gossamer Links : Discussions :

random links with conditions

Quote Reply
random links with conditions
Wink hi

To get some random links on my detailed page, I have created the following global :

sub {
my $tags = shift;
my $link_db = $DB->table('Links','CatLinks');
my $cat_id = $tags->{Random_CatID};
my $limit = $tags->{Random_Limit} || 5;
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;
}
return { Random_Loop => \@output }
}


then I include it on my detailed page via the following code :

<%set Random_CatID = $CategoryID%>
<%random_links%>
<%loop Random_Loop%>
<li><b><%Title%></b></li>
<%endloop%>




What I would like to do is to add a new condition, I want to loop to be done only on validated links (something like where validated="yes")

txs for your help

Regards
FMP

Quote Reply
Re: [fmp] random links with conditions In reply to
UnsureHi

Anyone know how to add this condition?

Txs for your help

FMP
Quote Reply
Re: [fmp] random links with conditions In reply to
Try with this code
Code:
if ($cat_id) {
$sth = $link_db->select ( { CategoryID => $cat_id , IsValidated => 'Yes'});
}
else {
$sth = $link_db->select(IsValidated => 'Yes');
}

If u have any trouble let me know.

Beck
Quote Reply
Re: [Beck] random links with conditions In reply to
I think it is isValidated rather than IsValidated.
Quote Reply
Re: [Beck] random links with conditions In reply to
Winkworks fine

txs

FMP
Quote Reply
Re: [Beck] random links with conditions In reply to
Hi

So here is my global :


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


It works fine, I just would like to add a last condition : I would like to compare the "Add_Date" with the Actual Date and then display my random links only if the périod is less than 30 days

Where do I have to add this condition and hoiw to do it?

is it something like the following?


sub {
my $tags = shift;
my $date = GT::Date::get_date();
my $limitdate = date_sub($date, 30);
my $link_db = $DB->table('Links','CatLinks');
my $cat_id = $tags->{Random_CatID};
my $limit = $tags->{Random_Limit} || 5;
my (@output, $sth); $link_db->select_options ('ORDER BY RAND()', "LIMIT $limit");
if ($cat_id) {
$sth = $link_db->select ( { CategoryID => $cat_id, IsValidated => 'Yes',Add_Date > $limitdate
});
}
else {
$sth = $link_db->select(IsValidated => 'Yes',Add_Date > $limitdate);
}
while (my $hash = $sth->fetchrow_hashref) {
push @output, $hash;
}
return { Random_Loop => \@output }
}




Txs
FMP
Quote Reply
Re: [fmp] random links with conditions In reply to
I think the first two lines you've added should be:

my $date = GT::Date::date_get();
my $limitdate = GT::Date::date_sub($date, 30);


To add the less than condition you'll need a condition statement.

my $cond = GT::SQL::Condition->new( 'CategoryID', '=', $cat_id, 'IsValidated', '='m 'Yes', 'Add_Date', '>', $limitdate);

my $cond1 = GT::SQL::Condition->new('IsValidated', '='m 'Yes', 'Add_Date', '>', $limitdate);
if ($cat_id) {
$sth = $link_db->select ( $cond);
}
else {
$sth = $link_db->select($cond1);
}