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

Global to return Random Category Name

Quote Reply
Global to return Random Category Name
Hi:

I wanted to write a global to return the Full Name of a Random Category... kind of a weird idea I had. Anyway, I played around with some code, and got as far as this, but was not completely working. Anyone want to see if they can spot my mistake?

Quote:


sub {
no strict;
my $db = $DB->table('Category');
my $total = $db -> count;
my $rand = int( rand($total) );
my $sth = $obj->select ( { ID => $rand } );
my $html = $sth->{'Full_Name'};
return $html;
}


Thanks!



dave
dave

Big Cartoon DataBase
Big Comic Book DataBase
Quote Reply
Re: [carfac] Global to return Random Category Name In reply to
Wrong forum, buddy...need to post it in the Links SQL Forums...not in the flat file Links 2.0 forums.
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [carfac] Global to return Random Category Name In reply to
Out of interest, why did you turn strict "off" ?

Try:

Code:
sub {
my $count = int(rand($DB->table('Category')->count)) || 1;
return $DB->table('Category')->select( { ID => $count }, ['Full_Name'] )->fetchrow;
}

I moved the thread by order of Heckler :)

Last edited by:

Paul: Mar 21, 2002, 11:14 AM
Quote Reply
Re: [Paul] Global to return Random Category Name In reply to
Paul:



Thanks for the reply. I was not sure what Strict Off did, but I saw it somewhere else, and thought I should do that. Ignorance is a dangerous thing!



I think I started out in the right forum, but after logging in and all, ended up at the wrong forum- thanks for moving!



dave
dave

Big Cartoon DataBase
Big Comic Book DataBase
Quote Reply
Re: [carfac] Global to return Random Category Name In reply to
Paul:



Ran it, worked GREAT- thanks!



ave
dave

Big Cartoon DataBase
Big Comic Book DataBase
Quote Reply
Re: [carfac] Global to return Random Category Name In reply to
No problem.
Quote Reply
Re: [Paul] Global to return Random Category Name In reply to
Paul:



I KNOW why I did the Strict now.... I am running it 5 times on a page, and want a different random category 5 times... That is right, isn't it?



Dave
dave

Big Cartoon DataBase
Big Comic Book DataBase
Quote Reply
Re: [carfac] Global to return Random Category Name In reply to
It should work fine as is one or more times per page.

Last edited by:

Paul: Mar 21, 2002, 12:01 PM
Quote Reply
Re: [Paul] Global to return Random Category Name In reply to
Paul:



Sure does. Thanks!



I added a bit more... since the returned code has spaces aznd periods, and the actual pages do not (they have underscores), I changed it to:



Quote:
my $count = int(rand($DB->table('Category')->count)) || 1;
my $link = $DB->table('Category')->select( { ID => $count }, ['Full_Name'] )->fetchrow;
$link =~ s/\s/_/g;
$link =~ s/\./_/g;
return $link;


(Did not realize this would be a problem in creating a link to the random category until I tried it!)



dave
dave

Big Cartoon DataBase
Big Comic Book DataBase