We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34183
    • 23 Posts
    Latest try with @BobRay's idea:

    $tvValue = $modx->resource->getTVValue(8);
    $array = explode(',', $tvValue);
    if (in_array($value,$array)) {
    return '<button type="button" value="'.$value.'" class="btn" data-toggle="button">'.$value.'</button>';
    } else {
    return 'Did not find ' . $value . ' in '. print_r($array,true);
    }
    


    Still no luck, it's seems like in_array isn't working on the exploded tv... Can anyone replicate?
      • 3749
      • 24,544 Posts
      Try just using strstr() on the raw (unexploded) TV value - that would be faster anyway.

      if (strstr($modx->resource->getTVValue(8), 'Guys') {
      }



      BTW, what is the default value of the TV and what is the Input Type?


      ------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using.
      MODX info for everyone: http://bobsguides.com/modx.html [ed. note: BobRay last edited this post 14 years, 6 months ago.]
        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
        • 3749
        • 24,544 Posts
        OK, I messed with it and I think it's kind of a bug (or two).

        This is what I get if both checkboxes are checked:

        $modx->resource->getTVValue(): GirlsGuys
        Raw $tv->get(): Girls||Guys
        Rendered tv->renderOutput(): GirlsGuys

        strstr() should work in all cases.

        If you want to use in_array, you'd have to get the TV itself, call its getValue() member and use '||' as a delimiter for explode() (or set the Delmiter to a comma in the TV's Output Options).


        $tv = $modx->getObject('modTemplateVar', 8);
        $value = $tv->getValue($modx->resource->get('id'));
        $valueArray = explode('||', $value);
        


        The reason I say bug(s) is that I don't think the delimiter should be removed in $resource->getTVValue(), and IIRC according to the docs, the default delimiter should be a comma.



        ------------------------------------------------------------------------------------------
        PLEASE, PLEASE specify the version of MODX you are using.
        MODX info for everyone: http://bobsguides.com/modx.html
          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
          • 34183
          • 23 Posts
          Thanks everyone, problem solved! When I went back to look at my TV input options it was
          "Girls || Guys" 

          not
          "Girls||Guys" 

          and the extra space characters were throwing it off.

          I ended up going with:
          [[show-button? &tv=`[[*product-gender]]` &value=`Guys`]]

          $array = explode(',', $tv);
          if (in_array($value,$array)) {
          return '<button type="button" value="'.$value.'" class="btn" data-toggle="button">'.$value.'</button>';
          } else {
          return;
          }


          Now it works great!