Gossamer Forum
Home : Products : DBMan SQL : Discussion :

Retrieving data from checkboxes

Quote Reply
Retrieving data from checkboxes
Hi,
DBMan SQL offers the possibility of inputing data to a record column as checkbox choices, hence multiple possibilities. Very useful but how does one determine which boxes have been checked? I presume ALL the different values are stored with some kind of seperator ? Thanks for any light of this (perhaps dumb) question!
Quote Reply
Re: [charly] Retrieving data from checkboxes In reply to
If I understand what you're asking, the separator is a linefeed - \n. The column stores each value selected, separated by linefeeds. Hope that helps.

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [hennagaijin] Retrieving data from checkboxes In reply to
Thanks. To be precise, I want to verify in a template what choices have been made in a checkbox field of a record. For example, the field/column 'Pictures' has seven possibilities, 1 to 7.
ie. <%if checkboxes 'includes' (?) #5%>... <%endif%>
This is what I had written as a global (excuse the rustic approach.)

sub {
my $tags = shift;
my $sep = chr(35);
$tags->{Pictures} =~ s/$sep/ /g;
return $tags->{Pictures};
}

but when I include <%checkboxes%> in my template I only obtain the first, lowest numbered, check box number, not ALL of those that have been chosen.
Thanks for your help
Quote Reply
Re: [charly] Retrieving data from checkboxes In reply to
I haven't seen that chr(35) before - is that supposed to be a linefeed? Try replacing it with \n and see what happens:

sub {
my $tags = shift;
my $pictures = $tags->{Pictures};
$pictures =~ s/\n/ /g;
return $pictures;
}

Fractured Atlas :: Liberate the Artist
Services: Healthcare, Fiscal Sponsorship, Marketing, Education, The Emerging Artists Fund
Quote Reply
Re: [charly] Retrieving data from checkboxes In reply to
Hi Charly,

You can use 'view_checked' template global below to determine which boxes have been checked:

sub {
my ($col, $val) = @_;
($col) or return;

my $tags = GT::Template->tags;
my $cols = $tags->{home}->{db}->cols;
($cols->{$col}->{form_type} =~ /checkbox/i ) or return;

my $f_values = $cols->{$col}->{form_values};
my $f_names = $cols->{$col}->{form_names};
my @values = split(/\n/, $val);

my @output;
foreach my $i (0..$#$f_names) {
my $checked = 0;
foreach my $v (@values) {
if ( $v eq $f_names->[$i] ) {
$checked = 1;
last;
}
}
push @output, { name => $f_values->[$i], value => $f_names->[$i], checked => $checked };
}

return { "loop_$col" => \@output };
}

Now, you can call it within your modify_form

<%view_checked('Pictures', $Pictures)%>
<%loop loop_Pictures%>
<input type="checkbox" name="Pictures" value="<%value%>" <%if checked%>checked<%endif%>><%Name%><br>
<%endif%>

Hope that helps.

TheStone.

B.
Quote Reply
Re: [TheStone] Retrieving data from checkboxes In reply to
Thanks a lot - I'll try this out, but I want to be sure: In the global you propose, what needs replacing for my use if my table is called 'Archive' and the column is called 'Pictures' ? anything else to change? (This will be the longest global yet for my table!!)
Cheers!
Quote Reply
Re: [charly] Retrieving data from checkboxes In reply to
You do not have to change anything.

TheStone.

B.
Quote Reply
Re: [TheStone] Retrieving data from checkboxes In reply to
The Stone's global works fine - with two small corrections to the calling routine: <%name%< instead of <%Name%> and with a final <%endloop%> (<%endif%> generates an missing endif error)

Therefore:
<%view_checked('Pictures', $Pictures)%>
<%loop loop_Pictures%>
<input type="checkbox" name="Pictures" value="<%value%>" <%if checked%>checked<%endif%>><%name%><br>
<%endloop%>

Thanks indeed TheStone!!