We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17389
    • 19 Posts
    Hi all, I’m currently putting together a basic snippet to format the output of a text input TV for a dropdown list. For some reason though, my comma delimited list from the TV won’t explode. Also, I’m no PHP expert so if I’ve done something dumb please let me know!
    Here’s my code:
    // Create array variable from colourSelect TV
    
    $cs_rawInput = "[*colourSelect*]";
    
    $cs_colourList = explode(", ", $cs_rawInput);
    
    // For each value in the array, print an option for a select box
    foreach ($cs_colourList as $value) {
         print "<option value=\"$value\">$value</option>";
    };

    Now, lets say the contents of my TV are "red, green, blue". Well, this string never explodes. That is, I get one dropdown option with "red, green, blue" in it.

    However, for testing purposes, when I manually type in "red, green, blue" as a value for $cs_rawInput in my snippet, I get three separate dropdown options just like I want.
    Does anyone have any idea why this is the case and what I might have done wrong? As far as I can tell, the string that my TV outputs is exactly the same as the one I manually type.
      • 4041
      • 788 Posts
      Give this one a try:

      <?php
      
      $cs_rawInput = $modx->getTemplateVarOutput('colourSelect');
      $cs_colourList = explode(',', $cs_rawInput['colourSelect']);
      
      // For each value in the array, print an option for a select box
      foreach ($cs_colourList as $value) {
           print '<option value="'.$value.'">'.$value.'</option>';
      };
      ?>
        xforum
        http://frsbuilders.net (under construction) forum for evolution
        • 3749
        • 24,544 Posts
        Quote from: LogiqCreative at Jan 22, 2010, 01:34 PM

        // Create array variable from colourSelect TV
        $cs_rawInput = "[*colourSelect*]";
        

        Does anyone have any idea why this is the case and what I might have done wrong?
        For future reference, you can’t use MODx tags (e.g. [*colourSelect*]) in PHP code because the MODx parser that replaces those tags doesn’t process the code in snippets. You need to use a PHP function to get the value of the TV as Breezer suggests.
          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
          • 32699 ☆ A M B ☆
          • 427 Posts
          Or you can use the variable in the snippet call

          [!snippetName?var=`[*colourSelect*]`!]

          and then catch it inside the snippet as it is already set to $var if var was the sending variable name.
            Get your copy of MODX Revolution Building the Web Your Way http://www.sanitypress.com/books/modx-revolution-building-the-web-your-way.html

            Check out my MODX || xPDO resources here: http://www.shawnwilkerson.com
            • 17389
            • 19 Posts
            Thanks for all of your help guys. I have very limited experience with PHP and rolling my own snippets in modx, so little tips like this go a long way! wink