[FX.php List] Checkbox
Michael Layne
fx at 9degrees.com
Tue Apr 4 12:35:52 MDT 2006
Hi all,
Here's what I did... first, since I never really got comfortable with
using or the use of value lists - and to make FileMaker as much a 'place
of storage' as possible (as opposed to using FileMaker specific features
such as "Value Lists") , more so as a back-end to a web app... here goes:
I have simple storage fields:
answer02 (text)
answer10 (text)
I have associative arrays that contains all the possible values for the
check boxes OR radio buttons (keys to store, values to display):
$ans02 = array ('n0' => 'less than 20','20' => '20-30','30' =>
'30-40','40' => '40-60','60' => 'over 60');
$ans10 = array ('TP' => 'Top Producer','MSOffice' => 'Microsoft Office',
'Publisher' => 'Publisher','FMLS/MLS' =>
'FMLS/MLS','SalesCloser' => 'Sales Closer');
Since I use the same PHP template to add and modify records, I use the
handy switch; '$mod'. So, based on whether the user is adding or
editing the record, I do this:
RADIO BUTTONS (one)
if($mod == '1') {
foreach($ans02 as $value => $label) {
$checked = $dJP['answer02'][0] == $value ? "checked=\"checked\"" : "";
echo "<input name=\"answer02\" type=\"radio\" value=\"" . $value
. "\" $checked />" . $label . "\n";
}
} else {
foreach($ans02 as $value => $label) {
echo "<input name=\"answer02\" type=\"radio\" value=\"" . $value
. "\" />" . $label . "\n";
}
}
CHECK BOXES (multi) - Allyson - I didn't look closely at your example,
but I too use 'eregi'
if($mod == '1') {
$count10 = 1; // count10 because there are 15 questions on the form,
this is No. 10...
foreach($ans10 as $value => $label) {
$checked = ( eregi($value,$dJP['answer10'][0])) ?
"checked=\"checked\"" : ""; // real logic here...
echo "<input name=\"answer10[]\" type=\"checkbox\" value=\"" .
$value . "\" $checked />" . $label . "\n";
if ($count10 == 6) { //this is simply to adhere to a content
width and keep things looking nice...
echo "<br />\n";
}
$count10++;
}
} else {
$count10 = 1;
foreach($ans10 as $value => $label) {
echo "<input name=\"answer10[]\" type=\"checkbox\" value=\"" .
$value . "\" />" . $label . "\n";
if ($count10 == 6) {
echo "<br />\n";
}
$count10++;
}
}
If 'anwser10' value = "MSOffice, Publisher, SalesCloser", then this will
really display:
[ ] Top Producer [x] Microsoft Office [x] Publisher [ ]
FMLS/MLS [x] Sales Closer
Now, some of you may have a more elegant way of doing this, and I might
too If I did it today (it's 2 years old), but I do know IT WORKS. It's
in a production environment on a pretty busy site.
HTH,
Michael
Michael Layne | 9 degrees
development | www.9degrees.com | 404.226.7835 | Skype: LayneBay
Rob H. Christensen wrote:
> I can see, that I am not the only one who runs into problems here.
>
> My original solution to show the data from FM wads taken from "FX.php in 8
> hours" (page 49)
>
> <?php
> $state_value=$findData['state'][0];
> foreach($listsResult['valueLists']['state'] as $key=>$value){
> if($state_value==$value){
> $selected="checked";
> }else{
> $selected="";
> }
> ?>
> <input type="checkbox" name="state" value="<?php echo $value;
> ?>"<?php echo $selected; ?>><?php echo $value; ?>
> <?php
> }
> ?>
>
>
> That is the same as the one for radio buttons on the previous page just with
> the word radio changed to checkbox.
>
> It does not work.
>
> Then I gave it a thought, looked up the definitions for checkboxes and found
> out, I had to do something else.
> I tried
>
> <?PHP
> $TRK=$Data['Trykkeri'][0];
> foreach($Vis2ListResult['valueLists']['Trykkeri'] as $key=>$value){
> if($TRK==$value){ $Select="checked";}else{$Select="";} ?>
> <input type="checkbox" name="Trykkeri" value="<?PHP echo $value;
> ?>""<?PHP echo $Select;?>"><?PHP echo $value;
> }?>
>
> Does not work
>
> I modified this in several steps ending up with:
>
>
> <?PHP
> $TRK=$Data['Trykkeri'][0];
> $TRK2=list(",",$TRK
> foreach($Vis2ListResult['valueLists']['Trykkeri'] as $key=>$value){
> if(in_array($value,$TRK2,true){$Select="checked";}else{$Select="";} ?>
> <input type="checkbox" name="Trykkeri" value="<?PHP echo $value;
> ?>""<?PHP echo $Select;?>"><?PHP echo $value;
> }?>
>
> It does not work.
>
> The point is, as long as there is but one out of five checkboxes checked, it
> works. But as soon as more then one is checked (and that is what checkboxes
> are for) it does not work.
>
> Somehow the data coming from FileMaker is not what I think it is. If I
> display the array in the page (print_r), it contains the names of the
> checked boxes in $Data['Trykkeri'][0] like Kniv010 and 4F014, when I spilt
> the array later on, it suddenly contained until several more of this kind,
> mirroring my experiments with the names of the checkboxes (the valuelist in
> FMP)
>
>
> Now, how do I compare the content of $Data['Trykkeri'] with the content of
> the valuelist, in order to check the boxes on the webpage?
>
> Allyson,
>
> Your solution:
>
> <?php foreach($Vis2ListResult['valueLists']['Trykkeri'] as $key=>$value){ ?>
>
> <input type="checkbox" name="Trykkeri" value="<?php echo $value; ?>"
> <?php if(in_array($value, $Data['Trykkeri'][0])) echo '"checked"'; ?> />
> <?php echo $value; } ?>
>
>
> Works only for one box checked not for more, just as the one you wrote in
> "FX.php in 8 hours"
>
>
>> Note however that this is also going to result in you losing data when this
>> is posted back to the db because you're using the name 'Trykkeri' for each
>> of the check boxes, so whichever is the last checkbox selected is what that
>> variable will enter when posted back to the db.
>>
>
> In FileMaker (6 at least) the checkbox is a valuelist with one name and a
> field with one name (I use the same name in both cases) set as a checkbox
> taking its values from the valuelist.
>
> If I understand you correct in html you want a different name for each value
> in the checkbox. I did not come so far as to edit FileMaker from this
> particular page yet , so I am wondering. The difenitions about checkboxes
> say, that several of them can have the same name:
>
> (definitions from GoLive)
> "Several checkboxes in a form may share the same control name. Thus, for
> example, checkboxes allow users to select several values for the same
> property. The INPUT element is used to create a checkbox control."
>
> Rob
>
>
>
> _______________________________________________
> FX.php_List mailing list
> FX.php_List at mail.iviking.org
> http://www.iviking.org/mailman/listinfo/fx.php_list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.iviking.org/pipermail/fx.php_list/attachments/20060404/f36db359/attachment-0001.html
More information about the FX.php_List
mailing list