Gossamer Forum
Quote Reply
Second Sort on Rand?
How can the result of the RAND function in this global then sort the result by ASC.
Can this be done?


sub {
# lists categories
my $tags = shift;
my $cat_db = $DB->table('Category');
$cat_db->select_options ("ORDER BY RAND()", "LIMIT 50");
my $sth = $cat_db->select ( { CatRoot => 1}, ['Full_Name','Name'] );
my @output;
while (my $cat = $sth->fetchrow_hashref) {
$cat->{URL} = "$CFG->{build_root_url}/" . $cat_db->as_url($cat->{Full_Name}) . "/";
push @output, $cat;
}
return {catrand => \@output};
}

~ ERASER


Free JavaScripts @ Insight Eye

Last edited by:

Eraser: Feb 11, 2007, 8:28 AM
Quote Reply
Re: [Eraser] Second Sort on Rand? In reply to
Try this:


$cat_db->select_options ("ORDER BY RAND ASC()", "LIMIT 50");
Quote Reply
Re: [rascal] Second Sort on Rand? In reply to
Many thanks, but I didn't think this would work as rand doesn't have any options with it.

Any other ideas folks on how to output the results of this global sorted by ASC?

~ ERASER


Free JavaScripts @ Insight Eye

Last edited by:

Eraser: Feb 19, 2007, 2:10 AM
Quote Reply
Re: [Eraser] Second Sort on Rand? In reply to
Sorry to keep coming back to this, but I haven't yet found a solution. I'm sure its just a small code change but I'm completely baffled by this!

~ ERASER


Free JavaScripts @ Insight Eye
Quote Reply
Re: [Eraser] Second Sort on Rand? In reply to
Hi,

Its not as simple as that (at least I couldn't get it to work with just changing the SQL query). However, I did manage to tweak the global so it should work (not ideal on speed, but works a charm =));

Code:
sub {

# lists categories
my $cat_db = $DB->table('Category');
$cat_db->select_options ("ORDER BY RAND()", "LIMIT 50");
my $sth = $cat_db->select ( ['Full_Name','Name'], { FatherID => 0 } );
my @output;
my $names;
while (my $cat = $sth->fetchrow_hashref) {
$names->{$cat->{Name}} = $cat->{Full_Name};
}

map {
my $cat = $cat_db->get( { Full_Name => $_ } );
$cat->{URL} = "$CFG->{build_root_url}/" . $cat_db->as_url($cat->{Full_Name}) . "/";
push @output, $cat;
} sort keys %$names;

return { catrand => \@output };
}

Hope that helps? (it orders the listings by their "Name" value) using sort keys %$names.

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] Second Sort on Rand? In reply to
Hey Andy that looks really interesting -- many thanks for your help! CoolCool

~ ERASER


Free JavaScripts @ Insight Eye