We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9728
    • 25 Posts
    hey there!

    this is my very first php coding. so be easy on me ;-)

    ok. i got a TV (file) that lets me upload and place a swf. the TV is called [*flash*]. now i want to create a snippet that puts the swf embed code into the template when i insert a swf via the TV or not if i don’t.

    so i created following code to check if the TV is empty or not:
    <?php
    $flash ="[*flash*]";
    	
    if( isset( $var ) ) echo "Variable Exists";
    if( empty( $var ) ) echo "Variable is Empty";
    
    ?>
    


    the result is "Variable Exists", even if i leave the TV blank.

    Then I tried to make a workaround, checking the length of the string. If I leave the TV blank it returns the nr 9 huh????????

    I also tried to check if it is an empty string:
    if ($flash == "") ...
    


    but no success as well.

    how can I solve my problem?

    best
    firewireflow
      • 10487 MODX Staff
      • 1,535 Posts
      Yeah, the main problem here is this line:
      $flash = "[*flash*]";

      MODX won’t parse the TV in PHP code when the TV is specified like that, you’ll need to use the MODx API to programatically get the TV output from the database. You could look at $modx->getTemplateVar() and $modx->getTemplateVarOutput(). (Check in the docs/wiki for info on what arguments you need pass into these functions).

      The count returns 9 because PHP is literally counting the number of characters in ’[*flash*]’ - which is 9 wink
        Garry Nutting
        Senior Developer
        MODX, LLC

        Email: [email protected]
        Twitter: @garryn
        Web: modx.com
        • 7231
        • 4,205 Posts
        Try something like this:

        <?php
        // define the TV name
        $tv = "flash";
        
        // get the TV value
        $flash = $modx -> getTemplateVarOutput($tv);
        
        // do the logic	
        if( isset( $flash ) ) 
            { return "Variable Exists";}
        else 
            { return "Variable is Empty";}
        
        ?>


        What is the output you want? You could use it in a chunk and display the chunk $modx->getChunk($chunk);

        There is also a snippet named TVConditionals that can handle this for you and you can also do it with PHx. Plenty of choices with MODx.
          [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

          Something is happening here, but you don&#39;t know what it is.
          Do you, Mr. Jones? - [bob dylan]
          • 9728
          • 25 Posts
          thanks ... but its still not solved ... i don’t really get it. although my TV text field is empty i still get output "Variable exists".

          here is what i want to do:

          i got a big picture on every page of the website. this is part of the design. when i edit a document i want either to have a picture or a swf file. so when i insert a swf it should dynamically write the code for the swf and the other way around. so i need to check what if the TV’s are empty or not.

          how do i achieve that?

          my thought of asking if the var’s length is below 1 might work, right? what do i do wrong?

          thanks a lot
          best
          firewireflow
            • 10487 MODX Staff
            • 1,535 Posts
            Yeah, using dev_cw’s code and then checking the var length should do what you want to do.
              Garry Nutting
              Senior Developer
              MODX, LLC

              Email: [email protected]
              Twitter: @garryn
              Web: modx.com
              • 9728
              • 25 Posts
              dev_cw’s code is not working for me for some reason. if i echo it, it displays "Array" huh

              this is getting really confusing!
                • 7231
                • 4,205 Posts
                I am sorry, I do this all the time rolleyes

                you need to change this:
                if( isset( $flash ) )


                to this:
                if( isset( $flash[$tv] ) )


                The problem is that it returns an array and you need to access the specific TV within the array, thus the $flash[$tv]
                  [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                  Something is happening here, but you don&#39;t know what it is.
                  Do you, Mr. Jones? - [bob dylan]