Gossamer Forum
Quote Reply
Change Global
Hi,

Im using the following code to generate a list of random links of a specific category.
I cannot get it to grab say the last 3 for example.

This is what Ive got:

Code:
sub {
#Place a random magazine on home page
my $table = $DB->table('Links');
$table->select_options ('ORDER BY RAND() LIMIT 1');
my $link = $table->select( { 'isValidated' => 'Yes', 'isRevista' => 'Yes'} )->fetchrow_hashref;
$link = Links::SiteHTML::tags('link',$link);
my $html = Links::SiteHTML::display('link_revista_home', $link);
return $html;
}

This code works and it grabs random, Id like to get the last 3. Im always getting a fetchrow_hashref error, but dunno how to fix it.

Andy, does your multi-global thingie do this?

Thanks,

JC
Quote Reply
Re: [Gorospe] Change Global In reply to
Hi,

It doesn't quite do that - try this though:

get_random_links_per_category
Code:
sub {

my $cat = $_[0];
my $limit = $_[1] || 5;

my $all_ids = $DB->table('Category')->children($cat);
push @$all_ids, $cat;

my $db_obj = $DB->table('Links','CatLinks','Category');
$db_obj->select_options ("ORDER BY RAND() DESC Limit $limit");

my $cond = GT::SQL::Condition->new('CategoryID', 'IN', $all_ids);
my $cond2 = GT::SQL::Condition->new('isValidated','=','Yes');
my $sth = $db_obj->select (['Links.*'], $cond, $cond2 ) || die $GT::SQL::error;

my @cats;
while (my $hit = $sth->fetchrow_hashref) {

if ($CFG->{build_detailed}) { $hit->{detailed_url} = $CFG->{build_root_url} . "/" . $DB->table('Links')->detailed_url( $hit->{ID} ); }

if ($hit->{isNew} eq "Yes") { $hit->{isNew} = 1; } else { $hit->{isNew} = 0; }
if ($hit->{isPopular} eq "Yes") { $hit->{isPopular} = 1; } else { $hit->{isPopular} = 0; }
if ($hit->{isChanged} eq "Yes") { $hit->{isChanged} = 1; } else { $hit->{isChanged} = 0; }

push @cats, $hit;
}

return { random_links_per_category => \@cats }

}

Code:
<%get_random_links_per_category($ID)%>
<%loop random_links_per_category%>
<%include link.html%>
<%endloop%>

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] Change Global In reply to
Thans Andy, as always.

Ive been using that code for years and it works.... Ill try this new one.

What about NOT random but YES latest or newest X amount?

Thanks
Quote Reply
Re: [Gorospe] Change Global In reply to
Quote:
What about NOT random but YES latest or newest X amount?

Try changing:

my $cond2 = GT::SQL::Condition->new('isNew','=','Yes');

and:

$db_obj->select_options ("ORDER BY Add_Date DESC Limit $limit");

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] Change Global In reply to
Seems pretty basic, but :) I cant get it to work...

On home id like to display the isNew from field isRevista or category ID 229.

I can get it random, but not just isNew.

Here is what I am now using:

Code:
sub {

my $cat = $_[0];
my $limit = $_[1] || 5;

my $all_ids = $DB->table('Category')->children($cat);
push @$all_ids, $cat;

my $db_obj = $DB->table('Links','CatLinks','Category');
$db_obj->select_options ("ORDER BY Add_Date DESC Limit $limit");

my $cond = GT::SQL::Condition->new('CategoryID', 'IN', $all_ids);
my $cond2 = GT::SQL::Condition->new('isValidated','=','Yes');
my $cond3 = GT::SQL::Condition->new('isNew','=','Yes');
my $cond4 = GT::SQL::Condition->new('isRevista','=','Yes');
my $sth = $db_obj->select (['Links.*'], $cond, $cond2, $cond3, $cond4 ) || die $GT::SQL::error;

my @cats;
while (my $hit = $sth->fetchrow_hashref) {

if ($CFG->{build_detailed}) { $hit->{detailed_url} = $CFG->{build_root_url} . "/" . $DB->table('Links')->detailed_url( $hit->{ID} ); }

if ($hit->{isNew} eq "Yes") { $hit->{isNew} = 1; } else { $hit->{isNew} = 0; }
if ($hit->{isPopular} eq "Yes") { $hit->{isPopular} = 1; } else { $hit->{isPopular} = 0; }
if ($hit->{isChanged} eq "Yes") { $hit->{isChanged} = 1; } else { $hit->{isChanged} = 0; }

push @cats, $hit;
}

return { latest_links_per_category => \@cats }

}
Calling with:

<%get_latest_magazines_per_category($ID)%>
<%loop latest_magazines_per_category%>
<%include link_revista_home.html%>
<%endloop%>

Thanks Andy
Quote Reply
Re: [Gorospe] Change Global In reply to
Something like this should do the trick (call it the same way you tried before)

Code:
sub {

my $cat = $_[0];
my $limit = $_[1] || 5;

my $all_ids = $DB->table('Category')->children($cat);
push @$all_ids, $cat;

my $db_obj = $DB->table('Links','CatLinks','Category');
$db_obj->select_options ("ORDER BY Add_Date DESC Limit $limit");

my $cond = GT::SQL::Condition->new('CategoryID', 'IN', $all_ids);
my $cond2 = GT::SQL::Condition->new('isValidated','=','Yes','isNew','=','Yes','isRevista','=','Yes');
$cond2->bool("AND");
my $sth = $db_obj->select (['Links.*'], $cond, $cond2 ) || die $GT::SQL::error;

my @cats;
while (my $hit = $sth->fetchrow_hashref) {

if ($CFG->{build_detailed}) { $hit->{detailed_url} = $CFG->{build_root_url} . "/" . $DB->table('Links')->detailed_url( $hit->{ID} ); }

if ($hit->{isNew} eq "Yes") { $hit->{isNew} = 1; } else { $hit->{isNew} = 0; }
if ($hit->{isPopular} eq "Yes") { $hit->{isPopular} = 1; } else { $hit->{isPopular} = 0; }
if ($hit->{isChanged} eq "Yes") { $hit->{isChanged} = 1; } else { $hit->{isChanged} = 0; }

push @cats, $hit;
}

return { latest_links_per_category => \@cats }

}

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] Change Global In reply to
Im getting this error:

A fatal error has occured:
GT::SQL::Table (32655): Wrong argument passed to this subroutine. No category id passed to children at /home/consulto/public_html/medicosdeelsalvador/cgi-bin/medicos/admin/Links/Table/Category.pm line 446.
This is what Ive got:

latest_magazines_per_category

Code:
sub {

my $cat = $_[0];
my $limit = $_[1] || 5;

my $all_ids = $DB->table('Category')->children($cat);
push @$all_ids, $cat;

my $db_obj = $DB->table('Links','CatLinks','Category');
$db_obj->select_options ("ORDER BY Add_Date DESC Limit $limit");

my $cond = GT::SQL::Condition->new('CategoryID', 'IN', $all_ids);
my $cond2 = GT::SQL::Condition->new('isValidated','=','Yes','isNew','=','Yes','isRevista','=','Yes');
$cond2->bool("AND");
my $sth = $db_obj->select (['Links.*'], $cond, $cond2 ) || die $GT::SQL::error;

my @cats;
while (my $hit = $sth->fetchrow_hashref) {

if ($CFG->{build_detailed}) { $hit->{detailed_url} = $CFG->{build_root_url} . "/" . $DB->table('Links')->detailed_url( $hit->{ID} ); }

if ($hit->{isNew} eq "Yes") { $hit->{isNew} = 1; } else { $hit->{isNew} = 0; }
if ($hit->{isPopular} eq "Yes") { $hit->{isPopular} = 1; } else { $hit->{isPopular} = 0; }
if ($hit->{isChanged} eq "Yes") { $hit->{isChanged} = 1; } else { $hit->{isChanged} = 0; }

push @cats, $hit;
}

return { latest_links_per_category => \@cats }

}

Calling with:

<%get_latest_magazines_per_category($ID)%>
<%loop latest_magazines_per_category%>
<%include link_revista_home.html%>
<%endloop%>
Quote Reply
Re: [Gorospe] Change Global In reply to
You sure? I just tested the same global you just posted, and it worked fine.

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] Change Global In reply to
Well, deleted all code and named the global, copied the global and called it exactly as its on my last post, and I get an error.

Any ideas?
Quote Reply
Re: [Gorospe] Change Global In reply to
Mmm, shoot over GLinks details and I'll take a quick look (and an example URL, where I can see the error)

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!