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

random link on main page from a specified category

Quote Reply
random link on main page from a specified category
Hello,

I would like to display (dynamic mode) a random link on main page from a specified category?
is there a global for that?

Many thanks
Antoine
Quote Reply
Re: [antoined] random link on main page from a specified category In reply to
This global is 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;
my $html = Links::SiteHTML::display('link', $link);
return $html;
}

How becomes if we need to display the random link from a specified category?

Waiting to hear from you

Thanks
Antoine
Quote Reply
Re: [antoined] random link on main page from a specified category In reply to
Hi,

You could try this;

<%global_name($ID)%>

..where $ID == the category ID.

Code:
sub {

my $current_cat = $_[0];

my $seed = localtime;
my $table = $DB->table('CatLinks');
$table->select_options('ORDER BY RAND($seed)','LIMIT 1');
my $linkid = $table->select( ['LinkID'], { CategoryID => $current_cat } )->fetchrow;

my $link = $DB->table('Links')->get($linkid);
my $html = Links::SiteHTML::display('link', $link);
return $html;

}

Untested, but should work :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!

Last edited by:

Andy: Apr 13, 2005, 1:11 AM
Quote Reply
Re: [Andy] random link on main page from a specified category In reply to
Hi

I am getting this error:


A fatal error has occured:
GT::SQL (17490): File '/home/www/my site/cgi-bin/znews/admin/defs/lsql_Catlinks.def' does not exist or the permissions are set incorrectly at (eval 14) line 5.

Please enable debugging in setup for more details.


Let me know please
Antoine






Quote Reply
Re: [antoined] random link on main page from a specified category In reply to
Ooops... try changing;

Code:
my $table = $DB->table('Catlinks');

..to;

Code:
my $table = $DB->table('CatLinks');

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] random link on main page from a specified category In reply to
Now it is working but it is displaying always the same link from that sepecified category.

what could be the problem?

Thank u again

Antoine
Quote Reply
Re: [antoined] random link on main page from a specified category In reply to
Try this global:

sub {
my ($output,$sth,$hash,$cond);
my $catid = $_[0];
my $limit = $_[1] || 4;
my $search_db = $DB->table('Links','CatLinks');
$search_db->select_options ("ORDER BY rand()", "LIMIT $limit");
my $all_ids = $DB->table('Category')->children($catid);
push @$all_ids, $catid;
$cond = GT::SQL::Condition->new('isValidated','=','Yes', 'CategoryID', 'IN', $all_ids);
$sth = $search_db->select ($cond);
while ($hash = $sth->fetchrow_hashref) {
$output .= Links::SiteHTML::display ('link_short', $hash);
}
return $output;
}
Regards
KaTaBd

Users plug In - Multi Search And Remote Search plug in - WebRing plug in - Muslims Directory
Quote Reply
Re: [katabd] random link on main page from a specified category In reply to
Hi Katadb and thank u for responding,

I am trying to display only one link on main page as random, so i changed the limit from 4 to 1 .

The problem that the system is displaying the same link all time after reloading.

Any idea?

Thanks
Antoine
Quote Reply
Re: [antoined] random link on main page from a specified category In reply to
Hi

I have it running fine on my site..

Make sure you have more that one lik in that very category (Not its subs)
Regards
KaTaBd

Users plug In - Multi Search And Remote Search plug in - WebRing plug in - Muslims Directory
Quote Reply
Re: [antoined] random link on main page from a specified category In reply to
Erm.. stupid question, but are you rebuilding your site? ;)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [antoined] random link on main page from a specified category In reply to
Just a suggestion - rand didn't work in older versions of mysql - can't remember which version though. If you have a new version then it is likely to be a seed issue.