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

Help with extracting only the start of a field value

Quote Reply
Help with extracting only the start of a field value
Hi!
Back after a holiday break with the urge to get DBMan SQL doing even greater things. Can anyone help me with the following task? I have a global called <%get_data%> which retrieves what is in a field in a particular record of a table:

sub {
my ($table, $field, $value) = @_;
my $sth = $DB->table($table)->select({ $field => $value });
my @output;
while (my $rs = $sth->fetchrow_hashref ) {
push @output, $rs;
}
return { loop_hash => \@output };
}

The global is called this way:
<%get_data('Articles','RecordID',21)%>
<%loop loop_hash%>
<%Content%>
<%endloop%>

This works fine, returning the ENTIRE content of the field <%Content%> in the record numbered 21 of table Articles.
What I would like to do is display >> only the first few paragraphs (say first 2) << of the field <%Content%>.

Now I already have a separate global that does this:
<%display_paras x,y%> # x=initial para, y=last para to show

sub {
my ($low, $high) = @_;
my ($tags) = GT::Template->tags;
my (@paras) = split /\r?\n/, $tags->{Content};
my $numberparas = @paras;
scalar @paras > $high or return join ("", @paras[$low..$numberparas]);
return join ("", @paras[$low..$high]);
}

The challenge is adapt the <%get_data%> global to display first few paragraphs of <%Content%>.
Thanks in advance for any assistance!
Charly
Quote Reply
Re: [charly] Help with extracting only the start of a field value In reply to
<G>

Did you figure this out?

I love playing with fancy globals, but only do so with Links SQL. The concepts are the same, though.

How about something like this:
Code:
sub {
my ($table, $field, $value, $low, $high, $parse_field) = @_;
my $sth = $DB->table($table)->select({ $field => $value });
my @output;
while (my $rs = $sth->fetchrow_hashref ) {
if ($parse_field) {
my (@paras) = split /\r?\n/, $rs->{$parse_field};
my $numberparas = @paras;
scalar @paras > $high or ( $rs->{$parse_field} = join ("", @paras[$low..$numberparas]) ) ;
$rs->{$parse_field} = join ("", @paras[$low..$high]);
}
push @output, $rs;
}
return { loop_hash => \@output };
}
The global is called this way:
<%get_data('Articles','RecordID',21,2,3,Content)%>
<%loop loop_hash%>
<%Content%>
<%endloop%>



Because $parse_field is the last parameter, and will only exist in the array if low and high are passed, and it will be non-numeric, it's a good test/flag to use for entering the loop. If you don't pass low, high and parse_field, that loop will be ignored, and you'll just get your unaltered records passed back.

All I did was merge your two routines, so if they both worked, they should still work -- with only minor tweaking here. I didn't test the routine, but it shouldn't do anything "evil" either.


PUGDOG´┐Ż Enterprises, Inc.

The best way to contact me is to NOT use Email.
Please leave a PM here.
Quote Reply
Re: [pugdog] Help with extracting only the start of a field value In reply to
Hi,
Many thanks indeed for your response. In fact I'd found a work-around since my post, but I'll test your proposed global and let you know! Thanks again.
CharlySmile