We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38462
    • 39 Posts
    I have a template variable, checkbox input type with multiple input option values, e.g. option1||option2||option3...

    Using the following snippet, i'm trying to return a list checked options.

    $tv = $modx->resource->getTVValue($tvname);
    $options = explode('||', $tv);
      
    foreach ($options as $option){
        $params['name'] = $option;
        $output = $modx->getChunk($tpl,$params);
    }
      
    return $output;


    However, this only results in a string with the options combined, e.g. option1option2option3.
    I'm eager to learn why this is happening?

    This question has been answered by northernape. See the first response.

      • 3749
      • 24,544 Posts
      It helps to see what you're getting before writing the code to display it:

      $tv = $modx->resource->getTVValue($tvname);
      return $tv;
      



        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 38462
        • 39 Posts
        Oh. I've now tried that and probably as you expected it returns a string with the options combined.

        I would be most grateful if you can explain why this happens? When the value within the database table is different, e.g. option1||option2||option3...
        • discuss.answer
          • 38462
          • 39 Posts
          I changed the TV output type to || delimiter and now working as intended. Although only after updating the snippet to:

          $tv = $modx->resource->getTVValue($tvname);//get tv value of current resource by name
          $options = explode("||", $tv);
          
          $output = '';
          
          foreach ($options as $option){
              $params = ['name' => $option];
           
              $output .= $modx->getChunk($tpl,$params);
          }
            
          return $output;


          When the TV output type is set to default, the value within the database table is delimited with || but when output it strips these. Apparently, this is a combination of legacy and different use cases requiring different behaviour. There's an issue about it on GitHub.
            • 3749
            • 24,544 Posts
            I'm glad you got it sorted. smiley
            Thanks for reporting back.
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting