-
- 70 Posts
I am using simple javascript datepicker to choose a date for my formit form. In datepicker the format to tell which dates are blocked and cannot be chosen is [0,0,0,0,0,0,1] in which 0 means available and 1 blocked, one number for each day, starting from Monday to Sunday.
I have 7 TV's (listbox, single-select), one for each day with input options Available==0||Blocked==1
Is it possible to have one check box TV with 7 boxes (one for each weekday) and for example if Mondays and Sundays were blocked (boxes checked) have it return [1,0,0,0,0,0,1]?
I have managed to have it return [1,1] where number of 1's depends of course how many days have been blocked, but I cannot figure out if it is possible to fill remaining "places" with 0's. How can I format the output so that each check box has its "own place" in output and if the box is not checked will return 0 in its place?
[ed. note: tsavage last edited this post 11 years, 8 months ago.]
-
- 5,888 Posts
you can try this snippet:
//[[getTvOptions? &tpl=`myChunk` &tvname=`myTv` &name=`cb1`]]
$tv = $modx->getObject('modTemplateVar',array('name'=>$tvname));
$options = explode('||',$tv->get('elements'));
$i = 1;
$count = count($options);
foreach ($options as $option){
$opt = explode ('==',$option);
$ph['value'] = count($opt)>1 ? $opt[1]: $opt[0];
$ph['text'] = $opt[0];
$ph['name'] = !empty($asArray) ? $name.'[]' : $name;
$ph['checked'] = '';
$ph['selected'] = '';
$ph['idx'] = $i;
$ph['_first'] = $i == 1 ? '1' : '0';
$ph['_last'] = $i == $count ? '1' : '0';
if ((is_array($_REQUEST[$name]) && in_array($ph['value'],$_REQUEST[$name])) || $_REQUEST[$name] == $ph['value']){
$ph['checked'] = 'checked="checked"';
$ph['selected'] = 'selected="selected"';
}
$output .= $modx->getChunk($tpl,$ph);
$i++;
}
return $output;
with a template-chunk like that:
[[+value:is=`1`:then=`1`:else=`0`]][[+_last:is=`0`:then=`,`:else=``]]
-
- 70 Posts
Thank you Bruno17! One question: What is &name=`cb1`?
[ed. note: tsavage last edited this post 11 years, 8 months ago.]
-
- 5,888 Posts
Thank you Bruno17! One question: What is &name=`cb1`?
&name is not needed in this case
-
- 70 Posts
If I remove the &name parameter from the call I get about 30 notices:
Notice: Undefined variable: name...
Notice: Undefined index:...
And then after all the notices it returns 1,1,1,1,1,1,1 - no matter what boxes are checked.
I guess I should remove the name-bit from the whole code but I don't quite understand how the whole thing works. I really appreciate all the help I can get. Thanks.
-
- 5,888 Posts
sorry, this doesn't work. My fault.
here is another one:
<?php
//[[getTvOptions? &tvname=`myTv` ]]
$tv = $modx->getObject('modTemplateVar',array('name'=>$tvname));
$options = explode('||',$tv->get('elements'));
$value = $modx->resource->getTVvalue($tvname);
$values = explode(',',$value); // not sure if you need ',' or '||' here
foreach ($options as $option){
$opt = explode ('==',$option);
$value = count($opt)>1 ? $opt[1]: $opt[0];
$text = $opt[0];
$output[] = in_array($value,$values) ? '1' : '0' ;
}
return implode(',',$output);
you will need input-options like that:
Monday==1||Thuesday==2||...........