We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 30711
    • 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.]
      • 4172
      • 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=``]]
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 30711
        • 70 Posts
        Thank you Bruno17! One question: What is &name=`cb1`? [ed. note: tsavage last edited this post 11 years, 8 months ago.]
          • 4172
          • 5,888 Posts
          Thank you Bruno17! One question: What is &name=`cb1`?

          &name is not needed in this case
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 30711
            • 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.
              • 4172
              • 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||...........
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
                • 30711
                • 70 Posts
                Thank you very much Bruno, much appreciated. This works!

                For: // not sure if you need ',' or '||' here - It is ','.

                As the output from the snippet needs to be in square brackets and you cannot wrap the snippet call with brackets? I changed the last row of the snippet to:

                $days = implode(',', $output);
                echo "[$days]";


                I am not php guru and don't know if this is the right/best way to do it but at least it works.

                Thanks again! smiley