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

Passing LIMIT to a global from a tag

Quote Reply
Passing LIMIT to a global from a tag
Heya all,

I'm using the global below to generate a list of the 8 latest links.

Code:
sub {
my ($output,$sth,$link);
my $search_db = $DB->table('Links');
$search_db->select_options ('ORDER BY Add_Date DESC Limit 8');
$sth = $search_db->select ( { isNew => 'Yes', isValidated => 'Yes' });
while ($link = $sth->fetchrow_hashref) {
if (length $link->{Title} > 50) {
$link->{Title} = substr($link->{Title}, 0, 50) . '...';
}
$output .= Links::SiteHTML::display ('link1', $link);
}
return $output;
}

I simply call this with a tag: <%global_name%>

I'd like to use this same global on other pages but generate a different number of links, say 2 or 35. How can I pass the value of "limit" to the global from the tag?

Safe swoops
Sangiro
Quote Reply
Re: [sangiro] Passing LIMIT to a global from a tag In reply to
Try changing;

Code:
$search_db->select_options ('ORDER BY Add_Date DESC Limit 8');

...to

Code:
$search_db->select_options ("ORDER BY Add_Date DESC","LIMIT $_[0]");

...and then call with <%global_name('5')%>

... and that should show 5 results Smile

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: May 16, 2004, 2:28 AM
Quote Reply
Re: [Andy] Passing LIMIT to a global from a tag In reply to
Andy,

I tried this and when I build the pages I get:

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

Safe swoops
Sangiro
Quote Reply
Re: [sangiro] Passing LIMIT to a global from a tag In reply to
Quick check - have you definitely changed the single quotes to double quotes?
Quote Reply
Re: [afinlr] Passing LIMIT to a global from a tag In reply to
Have indeed. Smile

That's not it... I'll stare at this a while to see if I can spot some other syntax error but I suspect I have it exactly as Andy suggested.

Safe swoops
Sangiro
Quote Reply
Re: [sangiro] Passing LIMIT to a global from a tag In reply to
How about the modified version? (above). I think it doesn't like the fact the LIMIT and ORDER stuff was both in the same quote (I would normally do "ORDER BY Fo","LIMIT $_[0]"). Also, try changing;

Code:
$sth = $search_db->select ( { isNew => 'Yes', isValidated => 'Yes' });

...to;

Code:
$sth = $search_db->select ( { isNew => 'Yes', isValidated => 'Yes' }) || return $GT::SQL::error;

How does it work now? You should at least get an error message if it has something to do with the MySQL query itself :)

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] Passing LIMIT to a global from a tag In reply to
Andy,

You are a scholar and a gentleman, and there's only a damn few of us left! Wink

Thanks. Works like a charm.

Safe swoops
Sangiro
Quote Reply
Re: [sangiro] Passing LIMIT to a global from a tag In reply to
Glad to hear its working Smile

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] Passing LIMIT to a global from a tag In reply to
Ok - while I've got the figer let me grab the whole hand! Wink

Do you have any suggestions on how to change this global so it skips the first X number of links it finds? I.E. List all the latest links but don't display the first 2 most recent ones. Start listing at 3...

Safe swoops
Sangiro
Quote Reply
Re: [sangiro] Passing LIMIT to a global from a tag In reply to
You should be able to do something like this;

Change;

Code:
$search_db->select_options ("ORDER BY Add_Date DESC","LIMIT $_[0]");

...to;

Code:
$search_db->select_options ("ORDER BY Add_Date DESC","LIMIT 2,$_[0]");

That should offset it to start on the 3rd record :)

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] Passing LIMIT to a global from a tag In reply to
Thanks. I'll give that a go.

Will changing "LIMIT 2,$_[0]"); to "LIMIT $_[1],$_[0]"); allow me to pass the second value to the global from the tag as well? Not sure how the $_[0] part tells the global to expect the value from the tag... so I'm not sure how to expand on that concept on my own. Unsure

Safe swoops
Sangiro
Quote Reply
Re: [sangiro] Passing LIMIT to a global from a tag In reply to
Yeah, it should do.

$_[] is similar to shift.

If you pass in something like;

<%global_name('foo','bar','test')%>

... and then use something like;


Code:
my $test1 = $_[0] || 'nothing';
my $test2 = $_[1] || 'nothing';
my $test3 = $_[2] || 'nothing';

... or you could use;

Code:
my ($test1,$test2,$test3) = shift;

...or...

Code:
my ($test1,$test2,$test3) = @_;

The way I do it, just gives that little bit more flexability to it, which I prefer. I don't think there is much difference in speed etc.

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!