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

Will this Random Global of mine work???

Quote Reply
Will this Random Global of mine work???
Hello,

I'm just starting to learn how these Perl globals work so I've attepted to create the following global to perform the following tasks on my site:

1. Display 10 or so random templates from only links with is_partner => 'Yes' , isValidated => 'Yes', has_content => 'Yes'

2. Display No Duplicates

3. Loop Results

Here's the global I've pieced together from several other globals written by Alex and few others...

random_gallerys:

sub {
my $tags = shift;
my $link_db = $DB->table('Links');
my $limit = $tags->{Random_Limit} || 10;
my (@output, $sth);
$link_db->select_options ('ORDER BY RAND()', "LIMIT $limit");
$sth = $link_db->select ( { is_partner => 'Yes', isValidated => 'Yes', has_content => 'Yes' });
while (my $hash = $sth->fetchrow_hashref) { push @output, $hash; }
return { Random_Loop => \@output }
}

Called like:

<%Random_Link%>
<%loop Random_Loop%>
Random: <%Title%>: <%URL%> <%content1%> and a bunch of template stuff!<BR>
<%endloop%>



The goal is to have LinksSQL build my site daily to constantly display fresh content from the links listed at my site.

Should this global do the trick for me????

Is there a more efficient way of writing it??? (Seem like searches are begining to slow as I continue to add globals)

Random_Limit would have to be a column in the links table right??? So it's saying Random_Limit "or" 10 ( whatever number to limit results )?

Is there a way to include 'ORDER BY HITS DESC' along with 'ORDER BY RAND()' ??? I was hoping to have the ability to pull the random links AND display them looped in order by hits. It is not a must though.

Hopefully some of that made sense.

Thanks for any suggestion or comments you all would be willing to offer! I just can't test it out at this point but would like to be preparing my templates if it will work.

Jonze
Quote Reply
Re: [Jonze] Will this Random Global of mine work??? In reply to
Hi,

Yes, that looks like it should work. You can also do:

Code:
sub {
my $tags = shift;
my $link_db = $DB->table('Links');
my $limit = $tags->{Random_Limit} || 10;
my (@output, $sth);
$link_db->select_options ('ORDER BY RAND()', "LIMIT $limit");
$sth = $link_db->select ( { is_partner => 'Yes', isValidated => 'Yes', has_content => 'Yes' });
while (my $hash = $sth->fetchrow_hashref) { push @output, $hash; }
return \@output;
}


And then in the template just do:

<%loop Random_Links%>
Random: <%Title%>: <%URL%> <%content1%> and a bunch of template stuff!<BR>
<%endloop%>

The ability to loop on functions was added in 2.1.1 I believe. Makes it a bit easier to use.

Quote:
Seem like searches are begining to slow as I continue to add globals


The number of globals you have should not affect the speed of the search. The number of globals used in your search results would affect the speed, but just having globals isn't slowing anything down.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Will this Random Global of mine work??? In reply to
Code:
$sth = $link_db->select ( { is_partner => 'Yes', isValidated => 'Yes', has_content => 'Yes' });
while (my $hash = $sth->fetchrow_hashref) {
push @output, $hash;
}
return \@output;

Or how about just:

Code:
my $loop = $link_db->select ( { is_partner => 'Yes', isValidated => 'Yes', has_content => 'Yes' })->fetchall_arrayref({});
return $loop;

Last edited by:

Paul: Sep 11, 2002, 10:38 AM
Quote Reply
Re: [Paul] Will this Random Global of mine work??? In reply to
Make that fetchall_hashref and you got it. =) Edit: Oops, missed the {} to fetchall_arrayref. You should use hashref anyways as that has been depreciated in GT::SQL as it doesn't work on older DBD drivers.

Cheers,

Alex
--
Gossamer Threads Inc.

Last edited by:

Alex: Sep 11, 2002, 10:42 AM
Quote Reply
Re: [Alex] Will this Random Global of mine work??? In reply to
fetchall_arrayref({}) will work too :)
Quote Reply
Re: [Paul] Will this Random Global of mine work??? In reply to
Man your fast. =)

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Will this Random Global of mine work??? In reply to
Edit: Oops sorry I replied before reading your edit.

Last edited by:

Paul: Sep 11, 2002, 10:46 AM
Quote Reply
Re: [Alex] Will this Random Global of mine work??? In reply to
Awesome, Thanks a lot Alex and Paul! Wink

Alright let me try to recap cuz I was a little thrown off by the fetchrow_hashref, fetchall_hashref, and fetchall_arrayref .....

so I can do:

sub {
my $tags = shift;
my $link_db = $DB->table('Links');
my $limit = $tags->{Random_Limit} || 10;
my (@output, $sth);
$link_db->select_options ('ORDER BY RAND()', "LIMIT $limit");
$sth = $link_db->select ( { is_partner => 'Yes', isValidated => 'Yes', has_content => 'Yes' });
while (my $hash = $sth->fetchrow_hashref) { push @output, $hash; }
return \@output;
}

or

sub {
my $tags = shift;
my $link_db = $DB->table('Links');
my $limit = $tags->{Random_Limit} || 10;
my (@output, $sth);
$link_db->select_options ('ORDER BY RAND()', "LIMIT $limit");
my $loop = $link_db->select ( { is_partner => 'Yes', isValidated => 'Yes', has_content => 'Yes' })->fetchall_arrayref({});
return $loop;
}


The second one is the only one that needs to have fetchall_arrayref right? Those edits have got me a little confused. Crazy

Alex, thanks for your comments about how globals effect search speed. I've just added quite a few globals to return file attachment locations to my links.html template which is used in search results. I thought having a high traffic site with file attachments constantly being pulled from the .cgi would cause huge resource usage. ( I'm using static pages) That's why I added globals to return the exact file paths to use in my templates. Looks like I may have to change things around since those globals may have a worse effect.

Does pulling the file attachments like this use many resources?

<%if Extention like '.jpg'%>
<IMG SRC="<%db_cgi_url%>/jump.cgi?view=File&ID=<%ID%>">
<%endif%>

I may have to go back to it if those globals start to kill my search result speed.