We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 34183
    • 23 Posts
    Hello MODX-ers,

    I am trying to find if a value is in a multi select template variable and output code based on that value.

    Here is an example page I am working on: http://trucreator.com/shop/things-you-wear/boom-bap.html

    I am using MIGX and have a tv called "product-gender" that accepts "Girls" and "Guys" with the output set to delimited, outputting "Girls,Guys". I want to display the Girls/Guys radio buttons only if that option is available.

    I initially attempted to use the 'inarray' output filter with 'if' like this:
    [[!if? 
    &subject=`Guys`
    &operator=`inarray`
    &operand=`[[*product-gender]]`
    &then=`<button type="button" value="Guys" class="btn" data-toggle="button">Guys</button>`
    ]]
    


    but to no avail... So I went the snippet route and created a snippet that accepts the tv like this:

    Snippet Call:
    [[tv-lookup? 
    &tv=`[[*product-gender]]`
    &value=`Guys`
    ]]
    


    Snippet Code (currently being output on the sample page):
    $array = explode(',', $tv);
    //$array = array("Girls","Guys");
    
    if (in_array($value, $array)) {
     echo("Found in Array"); 
    }else{
     echo("Not Found in Array");
    };
    echo "
    TV: ";
    echo $tv;
    echo "
    Array: ";
    print_r($array);
    echo "
    Value: ";
    print_r($value);
    echo "
     Array 0: ";
    echo $array[0];
    echo "
     Array 1: ";
    echo $array[1];
    echo "
    ";
    


    What seems weird is that when I switch the commented out $array sections then the if statement passes and everything else seems identical.

    So, my questions are:
    A. Is there something fundamentally wrong in the way I am going about solving this problem in the first place? Suggestions welcome smiley

    B. Is there some reason why in_array won't work on the exploded tv array but will on the manually created one (currently commented out)?
    C. Is MIGX to blame, or possibly involved since I am not outputting the tv as text?

    Thanks!
    Roger [ed. note: [email protected] last edited this post 14 years, 6 months ago.]
      • 3749
      • 24,544 Posts
      What do you see if you just put this in the snippet:


      $array = explode(',', $scriptProperties['tv']);
      return (print_r($array, true));
      




      ------------------------------------------------------------------------------------------
      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
        Hi Bob,

        I updated the snippet to include only the two lines you posted and get the following:
        Array ( [0] => Girls [1] => Guys )


        It's updated on the site link now: http://trucreator.com/shop/things-you-wear/boom-bap.html

        I did try passing it through the if statement again and it does return false.

        I am on MODx 2.2.0-pl2, mySQL 5.1.61-log, PHP 5.3.6, on Linux with SkyToaster.

        Thanks for your help,
        Roger
          • 4172
          • 5,888 Posts
          I've the feeling to miss some info here. What does MIGX in this scenario do?
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 34183
            • 23 Posts
            Hi Bruno,

            Sorry, I misspoke about that, I am using MIGX for another part of that page. I just looked to verify that the "product-gender" tv is not associated with MIGX and is assigned directly to the template I am using.

            I have updated the original post to reflect this.
              • 18373 ☆ A M B ☆
              • 3,141 Posts
              How about calling this snippet with &tv=`[[*product-gender]]` and &value=`Guys`?
              
              $array = explode(',', $scriptProperties['tv']);
              if (in_array($value,$array)) { return '<button type="button" value="'.$value.'" class="btn" data-toggle="button">'.$value.'</button>'; } else { return 'Didn't find ' . $value . ' in '. print_r($array,true); }
                Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                • 34183
                • 23 Posts
                Hey Mark,

                I have updated the snippet to:
                $array = explode(',', $scriptProperties['tv']);
                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);
                }
                


                and kept the snippet call:
                [[tv-lookup? &tv=`[[*product-gender]]` &value=`Guys`]]
                


                However, it is still not passing: http://trucreator.com/shop/things-you-wear/boom-bap.html

                The code seems like it should work right? Is there maybe another approach I should take?
                  • 18373 ☆ A M B ☆
                  • 3,141 Posts
                  Huh?
                    Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                    Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                    • 4172
                    • 5,888 Posts
                    your snippet should work.
                    when I try this:

                    [[tv-lookup? &tv=`Guys,Girls` &value=`Guys`]]


                    I get correctly:
                    <button data-toggle="button" class="btn" value="Guys" type="button">Guys</button>


                    [ed. note: Bruno17 last edited this post 14 years, 6 months ago.]
                      -------------------------------

                      you can buy me a beer, if you like MIGX

                      http://webcmsolutions.de/migx.html

                      Thanks!
                      • 34183
                      • 23 Posts
                      Bruno/Mark,

                      In my original code you can see I had line two commented out, whenever I switched it and commented out line one instead, then it worked fine. In Bruno's example he is passing in a delimited list which works fine for me as well, however, when it is a template variable then it fails.

                      Would one of you try creating a template variable with the delimited list output and pass that to the snippet to see if it is happening to you too?