Hi,
Mmm.. maybe try:
# --------------------------------------------------
my %opts = @_;
my $db = eval {
GT::SQL->new(
def_path => "$RC::CFG->{forum_admin_path}/defs", subclass => 0
)
};
$@ and return $@;
my $Post = $db->table('Post');
if ( my $forum_id_fk = $opts{forum_id_fk} ) {
my @forum_ids = split /\s*,\s*/, $forum_id_fk;
$opts{forum_id_fk} = \@forum_ids;
};
# Ensure that the system fixes the query to be exact normally
$opts{"route_category_id_fk-opt"} ||= '=';
$opts{"post_depth-opt"} ||= '=';
$opts{"post_root_id-opt"} ||= '=';
if ( my $days = $opts{days_within} ) {
$opts{"post_latest_reply-le"} = 2_000_000_000 - time + 86400 * $days;
}
# Allow specifying only specific columns
my $cols = delete $opts{cols};
if ($cols) {
my @selected = split /,/, $cols;
$opts{rs} = \@selected;
}
my $save;
if ($opts{index}) {
$save = $Post->{name};
$Post->{name} = "$Post->{name} /*!32312 USE INDEX ($opts{index}) */"; # MySQL hack to use the proper index
}
my $hits = $Post->query_sth( \%opts );
if ($save) {
$Post->{name} = $save;
}
my $num_found = $Post->hits;
my $prefix = $opts{prefix} || 'post';
my @loop;
while ( my $h = $hits->fetchrow_hashref ) {
$h = { map {( "${prefix}_$_" => $h->{$_} )} keys %$h };
push @loop, $h;
}
$prefix = $opts{prefix} || 'posts';
return ($num_found,@loop);
};
}
..then:
my $prefix = 'whatever_';
my ($hits,$the_loop) = RC::Forum::query2(
'forum_id_fk', '9,15,1,35,16,33,48,18,2,6,37,14,42,19,21,4,5,11,12,13,7,30,29,28,41',
"post_username", $user_username,
"post_root_id", 0,
"mh", 5,
"sb", "post_latest_reply",
"so", "asc",
"cols", "post_id,post_subject,post_replies,post_thread_hot",
"index", "p_rfl",
"days_within", 90
);
foreach (@$the_loop) {
# should have a hashref here
}
.and see what that does?
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!
Mmm.. maybe try:
Code:
sub query2 { # --------------------------------------------------
my %opts = @_;
my $db = eval {
GT::SQL->new(
def_path => "$RC::CFG->{forum_admin_path}/defs", subclass => 0
)
};
$@ and return $@;
my $Post = $db->table('Post');
if ( my $forum_id_fk = $opts{forum_id_fk} ) {
my @forum_ids = split /\s*,\s*/, $forum_id_fk;
$opts{forum_id_fk} = \@forum_ids;
};
# Ensure that the system fixes the query to be exact normally
$opts{"route_category_id_fk-opt"} ||= '=';
$opts{"post_depth-opt"} ||= '=';
$opts{"post_root_id-opt"} ||= '=';
if ( my $days = $opts{days_within} ) {
$opts{"post_latest_reply-le"} = 2_000_000_000 - time + 86400 * $days;
}
# Allow specifying only specific columns
my $cols = delete $opts{cols};
if ($cols) {
my @selected = split /,/, $cols;
$opts{rs} = \@selected;
}
my $save;
if ($opts{index}) {
$save = $Post->{name};
$Post->{name} = "$Post->{name} /*!32312 USE INDEX ($opts{index}) */"; # MySQL hack to use the proper index
}
my $hits = $Post->query_sth( \%opts );
if ($save) {
$Post->{name} = $save;
}
my $num_found = $Post->hits;
my $prefix = $opts{prefix} || 'post';
my @loop;
while ( my $h = $hits->fetchrow_hashref ) {
$h = { map {( "${prefix}_$_" => $h->{$_} )} keys %$h };
push @loop, $h;
}
$prefix = $opts{prefix} || 'posts';
return ($num_found,@loop);
};
}
..then:
Code:
use RC::Forum; my $prefix = 'whatever_';
my ($hits,$the_loop) = RC::Forum::query2(
'forum_id_fk', '9,15,1,35,16,33,48,18,2,6,37,14,42,19,21,4,5,11,12,13,7,30,29,28,41',
"post_username", $user_username,
"post_root_id", 0,
"mh", 5,
"sb", "post_latest_reply",
"so", "asc",
"cols", "post_id,post_subject,post_replies,post_thread_hot",
"index", "p_rfl",
"days_within", 90
);
foreach (@$the_loop) {
# should have a hashref here
}
.and see what that does?
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!