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

random link on category page

Quote Reply
random link on category page
Hi everbodyWink

i am using this global to display 4 random links on category page

i want to change this global that the result is to display 4 Random links

only of the category and subcategory on the category page

can someone help me?

PirateMarina

sub { my $tags = shift;
my $link_db = $DB->table('Links');
my $limit = $tags->{Random_Limit} || 4;
my (@output, $sth);
$link_db->select_options ('ORDER BY RAND()', "LIMIT $limit");
$sth = $link_db->select ( { isNew => 'Yes', isValidated => 'Yes'});
while (my $hash = $sth->fetchrow_hashref) { push @output, $hash; }
return \@output; }
Quote Reply
Re: [mkoenig] random link on category page In reply to
Should be pretty simple;

Code:
sub {
my $cat_id = $_[0];
my $link_db = $DB->table('CatLinks','Links');

my $limit = $_[1] || 4;
my (@output, $sth);

$link_db->select_options ('ORDER BY RAND()', "LIMIT $limit");
$sth = $link_db->select ( { CategoryID => $cat_id, isNew => 'Yes', isValidated => 'Yes'} );

while (my $hash = $sth->fetchrow_hashref) {
push @output, $hash;
}

$output ? return \@output : return '';

}

Call it with;

<%global_name($ID,4)%>

Hope that helps :)

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: Jan 12, 2004, 10:02 AM
Quote Reply
Re: [Andy] random link on category page In reply to
Hi Andy

i try your code but i get a server error:

Can't call method "fetchrow_hashref" on an undefined value at (eval 42) line 11.



TongueMarina

Quote Reply
Re: [mkoenig] random link on category page In reply to
Change this line in Andy's global:

$sth = $link_db->select ( { CategoryID => $cat_id, isNew => 'Yes', isValidated => 'Yes'} );

Edit:

Think you may also need to be using the correct tables

my $link_db = $DB->table('Links','CatLinks');

Last edited by:

afinlr: Jan 12, 2004, 8:44 AM
Quote Reply
Re: [afinlr] random link on category page In reply to
Sorry, please try the modified version. It was early.. and I obviously got a bit carried away with copy and pasting Tongue

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 category page In reply to
Hi Andy - still think it needs to be CategoryID rather than CatID?
Quote Reply
Re: [afinlr] random link on category page In reply to
Sorry, missed that one too Frown (it wasn't tested). Should be fixed in the above version now.

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 category page In reply to
Not meaning to be picky or anything Tongue

@outputb - in the last line?
Quote Reply
Re: [afinlr] random link on category page In reply to
Hi afinlr

What do you mean with outputb?

is there any possibility to display links from catid and the children cat_id's

Marina




Quote Reply
Re: [mkoenig] random link on category page In reply to
Andy updated his post and removed the 'b'.

If you want to include links from subcategories, this should work (hopefully!)

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', 'isNew', '=', 'Yes', 'CategoryID', 'IN', $all_ids);
$sth = $search_db->select ($cond);
while ($hash = $sth->fetchrow_hashref) { push @output, $hash;}
return \@output;
}
Quote Reply
Re: [afinlr] random link on category page In reply to
Sly it works

Thank you very much

MarinaWink
Quote Reply
Re: [afinlr] random link on category page In reply to
Hi afinlr

the global works very good

how can i get the category name and the link to the category of the links

MarinaSly
Quote Reply
Re: [mkoenig] random link on category page In reply to
This works for you? mmmh ...

All I'm getting returned is:

ARRAY(0x8612abc)

... calling it as <%random_children($ID,4)%> from the category page.
Quote Reply
Re: [CrazyGuy] random link on category page In reply to
Hi CrazyGuy

use <%loop random_children($ID,4)%>

MariinaWink
Quote Reply
Re: [CrazyGuy] random link on category page In reply to
Try;

Code:
<%random_children($ID,4)%>
<%GT::Template::dump%> (to see all the tags available to you)
<%endloop%>

Obviously replace the GT::Template::dump stuff with whatever you want to be shown there :)

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 category page In reply to
Nope - I'm obviously just not "getting this" at all.

Andy's suggestion does display all the variables etc and their content that have made up the category page (ideally with the final endloop removed) but there's nothing in there that suggests it has been returned by the global. The links loop contains the links that would be on that page, the cat loop contains the subcats - nothing that wouldn't be there without the global.

I've tried many combinations of the two suggestions - replacing the dump line with tags for title, url etc don't display anything, tried accessing loops within loops and combinations of loops. Usually nothing new displays, occassionally an endloop outside loop error. Not unexpected as I don't see anything actually returned by the global.

Maybe I'm missing something obvious about about the complete implementation.

Can someone tell me what should be added to the category.html template to display a one-row table with (say) the title and url fields of 4 random links drawn from the current and child categories?

I'll happily kick myself upon your behalf when it becomes clear Smile
Quote Reply
Re: [CrazyGuy] random link on category page In reply to
Further to the above ...

I have got a fully random global working, which I chose to do because it is similar code in the global and uses similar loop/endloop structure as expected in the template.

The main (critical) difference is the ability to restrain the random links to the current/children cats, which makes me look closer at that part of the code (identifying the child cats). Two things especially confuse me:

1) The interchanging of @all_ids and $all_ids
2) The use of single and double quotes when setting up SQL statements - different people seem to do different things.

... of course, if this code as printed above is working for other people, then those things must be ok too.

Crazy
Quote Reply
Re: [CrazyGuy] random link on category page In reply to
Closing this chapter (happily). This works for me:
Code:
sub {
my (@output,$sth,$hash,$cond);
my $catid = $_[0];
my $limit = 3;
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) { push (@output, $hash);}
return \@output;
}


and is called with:
Code:
<%loop random_children($ID)%>
<A HREF="<%URL%>"><%Title%></A><BR>
<%endloop%>


The only significant difference I can see is that I bracketed the two "push" statements - mainly because I was desperate and that's how I always write that command. Oh, I hardcoded the limit, but I've since reset that to be passed from the template and it works. Perhaps some bizarre Perl version sensitivity, I dunno.

All's well ...
Quote Reply
Re: [afinlr] random link on category page In reply to
Hi When trying this global I am getting:
ARRAY(0x85a92d4)
Any ideas?
Regards
KaTaBd

Users plug In - Multi Search And Remote Search plug in - WebRing plug in - Muslims Directory
Quote Reply
Re: [katabd] random link on category page In reply to
That's because it is returning an array so that you can use a loop.
You can use this instead:

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', 'isNew', '=', 'Yes', 'CategoryID', 'IN', $all_ids);
$sth = $search_db->select ($cond);
while ($hash = $sth->fetchrow_hashref) {
$output .= Links::SiteHTML::display ('link', $hash);
}
return $output;
}

Make sure that you put the ID of the category in the tag (and the number of links you want to show):
<%nameofglobal($ID,'2')%>
Quote Reply
Re: [afinlr] random link on category page In reply to
Thanks for all the help.

BUT I am getting an error:

A fatal error has occured:
GT::SQL::Table (28540): Wrong argument passed to this subroutine. Usage: Must pass category id to children at /path/cgi-bin/links/admin/Links/Category.pm line 293.

Please enable debugging in setup for more details.
The code I have is:
<%cat_random_link($1,'2')%>
Andc I tried:
<%cat_random_link(1,'2')%>
And <%cat_random_link($1,2)%>
Regards
KaTaBd

Users plug In - Multi Search And Remote Search plug in - WebRing plug in - Muslims Directory
Quote Reply
Re: [katabd] random link on category page In reply to
Wink You need either

<%cat_random_link('1','2')%>

or

<%cat_random_link($ID,'2')%>

The last one is for use on category pages where you have the ID tag available.
Quote Reply
Re: [afinlr] random link on category page In reply to
Both is returing a blank results..
Not printing anything..!
Regards
KaTaBd

Users plug In - Multi Search And Remote Search plug in - WebRing plug in - Muslims Directory
Quote Reply
Re: [katabd] random link on category page In reply to
Maybe because you don't have any new links? Try taking this out of the condition: 'isNew', '=', 'Yes',
Quote Reply
Re: [afinlr] random link on category page In reply to
Thanks The problem that my browser was for some reason copying in html form and the global has many errors in it

Thanks for all the help it is working just fine.
Regards
KaTaBd

Users plug In - Multi Search And Remote Search plug in - WebRing plug in - Muslims Directory