We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26334
    • 77 Posts
    OK I am writing a custom snippet and it refers to a TV which varies from page to page to represent different resorts.

    The different resorts have an id which is a number so to check the status of a value I am checking its status like this:
    $downhill = ’[+resortinfo_Downhill_[*resort_id*]+]’;

    If I echo this I get either "yes" or "no" as an output (correctly) depending on which resort I am referring to via the TV.

    Now if I do a
    strlen($downhill);

    The result is 37 which has me baffled. The database field is a varchar(3) so it can’t be any longer than 3.

    I figured perhaps it is only resolving to a string on echo somehow so I have even tried (string) $downhill and it is still stringlength 37.

    All I want to do is a simple if then comparision

    if ($downhill == ’yes’) {
    do this
    }

    But it never equates to yes, even though on echo it does. I have tried trim($downhill) with no effect, still strlen 37.

    I know this is cumbersome but I am trying to identify every step to work out what is happening.

    $downhill =  '[+resortinfo_Downhill_[*resort_id*]+]';
    $downhill1 = (string) $downhill;
    $downhill2 = trim($downhill1);
    
    echo 'downhill : -'.$downhill2.'-<br>';
    


    results in : downhill : -yes-

    If I look at the source html there are no returns or anything hiding there between the two -’s just -yes-

    Yet if I compare $downhill2 == ’yes’ it always fails and I suspect the string length is the issue but I just can’t get my head around why the stringlength would be 37 when the database entry is a varchar(3) and has a value of yes and that is returning!

    Any ideas, would be greatly appreciated.
      • 10449
      • 956 Posts
      You can’t use any of the {{}}, [[]], [**], [++] syntax in PHP. You have to use the API functions instead: http://wiki.modxcms.com/index.php/Category:API_Function

      Get a [+placeholder+] with:
      http://www.modxcms.com/getplaceholder.html

      Get a [*TV*] output with:
      http://wiki.modxcms.com/index.php/API:getTemplateVarOutput
      http://modxcms.com/gettemplatevaroutput.html
        • 26334
        • 77 Posts
        OK thanks. It is interesting that it actually works (sort of) in that it does echo the correct value and if I echo the TV it gives the correct ID number. Weird.

        OK then one for the dummies, I am new to this. Why is it an array back?

        I just want the value of in this case say [+resortinfo_Downhill_4+]

        So [*resort_id*] = 4

        Using the API functions it says it returns an array so this wouldn’t work?

        $st_r_id = 'resort_id';
        $resort_id = $modx->getTemplateVarOutput($st_r_id, $docid="", $published="1");
        $downhill = $modx->getPlaceholder($resort_id);


        as $resort_id in this instance would be an array. An array of what? The result should be a single integer return.

        How would I code this?

        Sorry but I don’t follow the nomenclature and there are no examples in those links, just trying to get my head around the syntax.

        Once I have it sorted I will add it to the wiki for others.

        Thanks.


          • 26334
          • 77 Posts
          OK sorted it.

          The return is an associative array. So

          #string to get TV for
          $string_id = 'resort_id';
          #get TV
          $resort_id = $modx->getTemplateVarOutput($string_id);
          
          #string to get placeholder for - value of associative array for the TV
          $downhill_string = 'resortinfo_Downhill_'.$resort_id[$st_r_id];
          #get placeholder
          $downhill = $modx->getPlaceholder($downhill_string);
          


          All working. Thanks.