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

How to use this function in a CGI script

Quote Reply
How to use this function in a CGI script
Hi there,Wink

I have the following script:

Code:
sub query {
# --------------------------------------------------
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 {
"${prefix}_hits" => $num_found,
"${prefix}_loop" => \@loop
};
}

This script is called like so:

Code:

RC::Forum::query(
'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
);

How should I go to work to use the return values of the function if I use it in a CGI script? It is currently used for the dynamic HTML in the templates. I know the first return value is a number, I think the second is an 'array'. I am not allowed to change the function in anyway. Thanks for the help Smile


Sacrifice is not about what you lose,
it is about what you gain in the process.
Quote Reply
Re: [EZFrag] How to use this function in a CGI script In reply to
Oh, yea and after the function is called in the templates, the return values is used like this:

Code:
<%loop posts_loop%>
<tr>
<td class="ftablecol"><%if post_post_replies%>
<img src="/images/forum/with_replies.gif" width="13" height="15" border="0"
align="absmiddle" alt="With Replies">
<%else%>
<img src="/images/forum/no_replies.gif" width="13" height="15" border="0"
align="absmiddle" alt="No Replies">
<%endif%>
</td>
<td class="ftablecol"><a href="/cgi-bin/forum/gforum.cgi?post=<%post_post_id%>;page=unread#unread">
<%short_post_post_subject%></a></td>
<td class="ftablecol" align="center" style="border-right:0px"><%post_post_replies%></td>
</tr>
<%~endloop%>


Sacrifice is not about what you lose,
it is about what you gain in the process.
Quote Reply
Re: [EZFrag] How to use this function in a CGI script In reply to
Hi,

Hard to know really, but something like this maybe?

Code:
use RC::Forum;

my $prefix = 'whatever_';

my ($hits,$the_loop) = RC::Forum::query(
'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
}

Hope that helps.

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] How to use this function in a CGI script In reply to
ah, thanks Andy...

I'm having difficulty with the hashref's though. I haven't quite grasped that concept yetSmile
The function seems to work and "$hits" is returning content like "HASH(0x12761de0)".
Could get anything out of the "the_loop" variable....Frown


Sacrifice is not about what you lose,
it is about what you gain in the process.
Quote Reply
Re: [EZFrag] How to use this function in a CGI script In reply to
Hi,

Mmm.. try a Data::Dumper;


Code:
use Data::Dumper;

print qq~Value of hits: <br />~;
print Dumper($hits)
print qq~Value of the_loop: <br />~;
print Dumper($the_loop);

..to try and see whats being returned.

$hits *should* be a value - not a HASH :/

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] How to use this function in a CGI script In reply to
Hehe, I think we have an unexplainable computer programming anomaly hereLaugh

Here is the results of the dumper.

the_loop : internal system failure

hits : $VAR1 = { 'posts_loop' => [ { 'post_post_subject' => 'Major Server Hardware Upgrades', 'post_post_id' => '3150615', 'post_post_thread_hot' => undef, 'post_post_replies' => '0' }, { 'post_post_subject' => 'Site Outage - March 11, 2008', 'post_post_id' => '3150610', 'post_post_thread_hot' => undef, 'post_post_replies' => '0' }, { 'post_post_subject' => 'Open Forums', 'post_post_id' => '3111745', 'post_post_thread_hot' => undef, 'post_post_replies' => '128' }, { 'post_post_subject' => 'Eric (Tonto) Stephenson - Goodbye my friend', 'post_post_id' => '2994812', 'post_post_thread_hot' => undef, 'post_post_replies' => '249' }, { 'post_post_subject' => 'Amateur Skydiving Video Contest!', 'post_post_id' => '3137170', 'post_post_thread_hot' => undef, 'post_post_replies' => '0' } ], 'posts_hits' => 10 };


You don't perhaps know what is happening here? As for the variables, I'll just close my eyes and imagine that $hits is $the_loops.


Sacrifice is not about what you lose,
it is about what you gain in the process.
Quote Reply
Re: [EZFrag] How to use this function in a CGI script In reply to
Hi,

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!
Quote Reply
Re: [Andy] How to use this function in a CGI script In reply to
I would like to try it, but I shouldn't edit that file. a Lot of our templates are depending on it....

I made the output a little nicer.

Code:
{ 'posts_loop' =>
[
{ 'post_post_subject' => 'Major Server Hardware Upgrades',
'post_post_id' => '3150615', 'post_post_thread_hot' => undef,
'post_post_replies' => '0' },

{ 'post_post_subject' => 'Site Outage - March 11, 2008',
'post_post_id' => '3150610',
'post_post_thread_hot' => undef,
'post_post_replies' => '0' },

{ 'post_post_subject' => 'Open Forums',
'post_post_id' => '3111745',
'post_post_thread_hot' => undef,
'post_post_replies' => '128' },

{ 'post_post_subject' => 'Eric (Tonto) Stephenson - Goodbye my friend',
'post_post_id' => '2994812',
'post_post_thread_hot' => undef,
'post_post_replies' => '249' },

{ 'post_post_subject' => 'Amateur Skydiving Video Contest!',
'post_post_id' => '3137170',
'post_post_thread_hot' => undef,
'post_post_replies' => '0' }
],
'posts_hits' => 10 };

All I have to do now is use the right hashrefs, right?


Sacrifice is not about what you lose,
it is about what you gain in the process.
Quote Reply
Re: [EZFrag] How to use this function in a CGI script In reply to
Hi,

Ok, think I see whats happening there.

Try:
Code:
use RC::Forum;

my ($results) = RC::Forum::query(
'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
);

print qq|Hits: $results->{posts_hits} <br />|;

my $the_loop = $results->{posts_loop};

foreach (@$the_loop) {
# should have a hashref here
}

No guarantees though =)

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: Apr 3, 2008, 6:03 AM
Quote Reply
Re: [Andy] How to use this function in a CGI script In reply to
Andy,

You knew that would work, didn't you? SmileSmileSmile
Thanks alot for your help. I just have to remember this stuff.
I will need it againWink

Thank you again, Andy.


Sacrifice is not about what you lose,
it is about what you gain in the process.
Quote Reply
Re: [EZFrag] How to use this function in a CGI script In reply to
Hi,

Quote:
You knew that would work, didn't you? SmileSmileSmile

LOL - it worked then? :D

Quote:
Thanks alot for your help. I just have to remember this stuff.
I will need it againWink

NP - only reason I know how to do it, is cos I've had the same problems before - and either had help resolving it, or just had to soldier on - and loose some hair in the process LOL

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!