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

Random link global

(Page 1 of 2)
> >
Quote Reply
Random link global
Hi

I have created a new global to display a random link:



sub {
my $db = $DB->table('Links');
my $total = $db->count ( 'isValidated' => 'Yes' );
my $rand = int( rand($total) );
$db->select_options ("LIMIT $rand, 1");
my $link = $db->select->fetchrow_hashref;
if (length $link->{Title} > 20) {
$link->{Title} = substr($link->{Title}, 0, 20) . '...';
}
my $html = Links::SiteHTML::display('link', $link);
return $html;
}


How to I limit that global to pick up only a specific links? lets say only the once that has Description? so links without one will not be randomed..
Regards
KaTaBd

Users plug In - Multi Search And Remote Search plug in - WebRing plug in - Muslims Directory
Quote Reply
Re: [katabd] Random link global In reply to
Implement the following condition into your original code:
Code:
my $cond = GT::SQL::Condition->new('Description', '=', "");
$cond->not;

EDITED: I don't know if it is possible to use regexps in condition value.
Try out to use: new('Description', '=', "\s*");

Let me know if it worked.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...

Last edited by:

webmaster33: Jul 24, 2002, 12:17 AM
Quote Reply
Re: [webmaster33] Random link global In reply to
>>
my $cond = GT::SQL::Condition->new('Description', '=', "");
<<

You'd want:

my $cond = GT::SQL::Condition->new('Description', 'IS NOT', \'NULL');
Quote Reply
Re: [Paul] Random link global In reply to
Yes, thanks. Your solution is the good one.
However, the GT docs says $cond->not; is also working, to invert the condition.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Random link global In reply to
Yes $cond->not works, but the description column is NULL if it isn't filled so I think IS NOT NULL may just be more accurate, rather than <> "".
Quote Reply
Re: [Paul] Random link global In reply to
Yep, I aggree you solution is better. That why I told, your solution is the good one.
Just mentioned that the "$cond->not" solution still works.

Best regards,
Webmaster33


Paid Support
from Webmaster33. Expert in Perl programming & Gossamer Threads applications. (click here for prices)
Webmaster33's products (upd.2004.09.26) | Private message | Contact me | Was my post helpful? Donate my help...
Quote Reply
Re: [webmaster33] Random link global In reply to
Thanks Both for the reply

But neither your solution nor Paul's seems to fix it..

What i am trying to do is create a global that will return a dandom banners of all sites that I have added a banner to (through a links table field "banner")..

The logic seems fine but nothing shows up not even errors...Just blank...
Regards
KaTaBd

Users plug In - Multi Search And Remote Search plug in - WebRing plug in - Muslims Directory
Quote Reply
Re: [katabd] Random link global In reply to
What code do you have?...have to tried printing $GT::SQL::error incase there's an error?
Quote Reply
Re: [Paul] Random link global In reply to
Hi



I have

sub {
my $db = $DB->table('Links');
my $total = $db->count ( 'isValidated' => 'Yes' );
my $rand = int( rand($total) ); my $cond = GT::SQL::Condition->new('Banner', '=', "");
$db->select_options ("LIMIT $rand, 1");
my $link = $db->select->fetchrow_hashref;
if (length $link->{Title} > 20) {
$link->{Title} = substr($link->{Title}, 0, 20) . '...';
}
my $html = Links::SiteHTML::display('link', $link);
return $html;
}



and no no errors are logged..
Regards
KaTaBd

Users plug In - Multi Search And Remote Search plug in - WebRing plug in - Muslims Directory
Quote Reply
Re: [katabd] Random link global In reply to
Code:
sub {
my $db = $DB->table('Links');
my $total = $db->count ( { 'isValidated' => 'Yes' } );
my $rand = int rand $total;
my $cond = GT::SQL::Condition->new('Banner', '=', '');
$db->select_options ("LIMIT $rand, 1");
my $link = $db->select->fetchrow_hashref;

if (length $link->{Title} > 20) {
$link->{Title} = substr($link->{Title}, 0, 20) . '...';
}
return Links::SiteHTML::display('link', $link);
}

See if that helps. It will select a record only if the Banner field is empty, is that right?
Quote Reply
Re: [Paul] Random link global In reply to
Actually the gobal should select a record only is there is a banner in the banner field... then display that banner so will:

sub {
my $db = $DB->table('Links');
my $total = $db->count ( { 'isValidated' => 'Yes' } );
my $rand = int rand $total;
my $cond = GT::SQL::Condition->new('Banner', '', '');
$db->select_options ("LIMIT $rand, 1");
my $link = $db->select->fetchrow_hashref;

if (length $link->{Title} > 20) {
$link->{Title} = substr($link->{Title}, 0, 20) . '...';
}
return Links::SiteHTML::display('link', $link);
}



work to achieve that?
Regards
KaTaBd

Users plug In - Multi Search And Remote Search plug in - WebRing plug in - Muslims Directory
Quote Reply
Re: [katabd] Random link global In reply to
I think you will need....

my $cond = GT::SQL::Condition->new('Banner', 'IS NOT', \'NULL');
Quote Reply
Re: [Paul] Random link global In reply to
Thanks Paul

That did not do it either..although it sounds right:

sub {
my $db = $DB->table('Links');
my $total = $db->count ( { 'isValidated' => 'Yes' } );
my $rand = int rand $total;
my $cond = GT::SQL::Condition->new('Banner', 'IS NOT', \'NULL');
$db->select_options ("LIMIT $rand, 1");
my $link = $db->select->fetchrow_hashref;

if (length $link->{Title} > 20) {
$link->{Title} = substr($link->{Title}, 0, 20) . '...';
}
return Links::SiteHTML::display('link', $link);
}
Regards
KaTaBd

Users plug In - Multi Search And Remote Search plug in - WebRing plug in - Muslims Directory
Quote Reply
Re: [katabd] Random link global In reply to
You need to print $GT::SQL::error...it must tell you something if it isn't doing what you expect.
Quote Reply
Re: [Paul] Random link global In reply to
I think the problem was that a condition was defined, but never used in select!
Quote Reply
Re: [xpert] Random link global In reply to
Anyone have other ideas whyt eh above global don't work?
Quote Reply
Re: [katabd] Random link global In reply to
Did you look at GT's code for jumping to a random link?

The problem is the random number you return may not be a valid link. You need to loop until you find a valid number.


PUGDOG� Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [xpert] Random link global In reply to
What about if you add {isValidated => 'Yes'} to the select/condition statement.
Quote Reply
Re: [afinlr] Random link global In reply to
Hmm.. the global is very frustrating, and i see all kinds of problems with it.. like it even picks sites from unvalidated...
Quote Reply
Re: [xpert] Random link global In reply to
I do think that it should work if you make sure that you have the same conditions in the select statement that you are using in the count - so that the random number that you are creating is always picking a valid link. If you set up the condition at the beginning then you can use it in the count and select statements to make sure you are using the same links in the count and select.

Something like this

sub {
my $db = $DB->table('Links');

my $cond = GT::SQL::Condition->new('Banner', 'IS NOT', \'NULL', 'isValidated', '=', 'Yes');

my $total = $db->count ( $cond );
my $rand = int rand $total;
$db->select_options ("LIMIT $rand, 1");
my $link = $db->select($cond)->fetchrow_hashref;

if (length $link->{Title} > 20) {
$link->{Title} = substr($link->{Title}, 0, 20) . '...';
}
return Links::SiteHTML::display('link', $link);
}

Last edited by:

afinlr: May 12, 2003, 4:36 AM
Quote Reply
Re: [Paul] Random link global In reply to
Hi Paul,



can I ask you how to use your global:

sub {
my $db = $DB->table('Links');
my $total = $db->count ( { 'isValidated' => 'Yes' } );
my $rand = int rand $total;
my $cond = GT::SQL::Condition->new('Banner', '=', '');
$db->select_options ("LIMIT $rand, 1");
my $link = $db->select->fetchrow_hashref;

if (length $link->{Title} > 20) {
$link->{Title} = substr($link->{Title}, 0, 20) . '...';
}
return Links::SiteHTML::display('link', $link);
}


so that it only selects a link that does not have the value 'link' in the 'Image' field og the lsql_Link table ?



Sorry for being no good in perl... Blush
Quote Reply
Re: [nt6] Random link global In reply to
Try changing:

my $cond = GT::SQL::Condition->new('Banner', '=', '');

to

my $cond = GT::SQL::Condition->new('Banner', '=', '', 'Image', 'NOT LIKE', '%link%');
Quote Reply
Re: [Paul] Random link global In reply to
Thanks Paul Smile
Quote Reply
Re: [nt6] Random link global In reply to
Thx for the help, unfortunately it still doesn't work. The global still randomized all links, and not selecting only links with the Banner field filled!
Quote Reply
Re: [xpert] Random link global In reply to
>>
and not selecting only links with the Banner field filled!
<<

Well no because you are using:

'Banner', '=' . ''

...so it will only select where the banner field is empty.

Change:

=

to

<>
> >