Gossamer Forum
Home : General : Perl Programming :

Select greater than...

Quote Reply
Select greater than...
Hi,

I'm modifying a global for Links SQL that selects records for the category table that has the following line:

my $sth = $cat_db->select ( {'FatherID' => 0, Cat_Include => 'Yes'},['Full_Name','Name'] );

I want to add one more condition where it will only select those records where the integer field called 'Number_of_Links' greater than 0 (or not equal to zero). I tried:

my $sth = $cat_db->select ( {'FatherID' => 0, Number_of_Links > 0, Cat_Include => 'Yes'},['Full_Name','Name'] );

but then I get the error:

Unable to compile: Bareword "Number_of_Links" not allowed while "strict subs" in use at...

Does anyone know how I can put in this extra condition for the Number_of_Links and make it work?

--FrankM
Quote Reply
Re: [FrankM] Select greater than... In reply to
Hi. Try;

Code:
my $cond = new GT::SQL::Condition->new(
'FatherID','=','0',
'Number_of_Links','>','0',
'Cat_Include','=','Yes',
);
my $sth = $cat_db->select ( ['Full_Name','Name'], $cond ) || return $GT::SQL::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!
Quote Reply
Re: [Andy] Select greater than... In reply to
Thanks very much Andy

That almost worked, but I forgot to include in my original post that I have another line right below that counts for the exact same conditions. I put it in red below:

my $sth = $cat_db->select ( ['Full_Name','Name'], $cond ) || return $GT::SQL::error;
my $total = $cat_db->count ( ['Full_Name','Name'], $cond ) || return $GT::SQL::error;

I think that this second line is causing the error that I get:

GT::SQL::Table (9296): Wrong argument passed to this subroutine.
Usage: Arguments to count() must either be a hash or a hash ref or GT::SQL::Condition object

If you know of a quick fix for this, that would be great, but I know you've always got a lot of stuff going on. (I put the full global code below, this is to create categories in two columns, but only those that meet the special conditions...)

--FrankM

sub {
my $width = int (100 / 2);
my $table_head = $CFG->{build_category_table} || '';
my $output=qq~<table $table_head><tr><td width="$width%" align="left" valign="top" nowrap>\n~;
my $cat_db = $DB->table('Category');
$cat_db->select_options('ORDER BY Name');
my $cond = new GT::SQL::Condition->new( 'FatherID','=','0', 'Number_of_Links','>','0', 'Cat_Include','=','Yes', );
my $sth = $cat_db->select ( ['Full_Name','Name'], $cond ) || return $GT::SQL::error;
my $total = $cat_db->count ( ['Full_Name','Name'], $cond ) || return $GT::SQL::error;
my $breakpoint = int (($total) / 2) + ( (($total) % 2) ? 1 : 0);
my $i=0;
while (my ($cat,$full_name) = $sth->fetchrow_array) {
my $url = $cat_db->as_url($cat);
($i > 0) and !($i % $breakpoint) and ($output .= qq~</td>\n<td width="$width%" align="left" valign="top" nowrap>\n~);
$i++;
$output .= qq~<span class="bodysmall"><b><font color="#FFAB00">&#8226;</font></b> &nbsp;<a href="$CFG->{build_root_url}/$url/">$full_name</a></span><br>~;
}
$output .= qq~</td></tr></table>\n~;
return $output;
}

Quote Reply
Re: [FrankM] Select greater than... In reply to
Hi. Try this;

Code:
my $total = $cat_db->count ( $cond ) || '0';

You can't pass in [] stuff to a count() call, as it is only counting, and not returning actual field values Tongue

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] Select greater than... In reply to
Thanks Andy! That worked perfectly. I appreciate your help.

--Frank