We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33982 ☆ A M B ☆
    • 476 Posts
    So I use PHx all the time for if/then statements, but not sure how to accomplish this: when I have 3 T.V.’s that are empty on a given document, I need a certain output. If any 1 of the T.V.’s is not empty, then another output.

    For example, I would like to be able to say something along the lines of:

    - if [*tv1*], [*tv2*], [*tv3*] are all empty, then = x, but if one of the T.V.’s has a value, then = y

    Hope that makes sense. Generally as I mentioned, I use PHx for all my conditional statements, but I’m not finding an example for this scenario.

    Thanks!
      Precision Web Development ... SmashStack.com
      • 3749
      • 24,544 Posts
      I think this can be done with the "and" and "or" modifiers in PHX, but really, you’d be much better off with one fairly simple snippet. It would be *much* faster and more efficient.

      Something like this (just put the snippet call where you want the output):

      (untested)

      [!MySnippet? &tv1 = `[*templateVariable1*]` &tv2 = `[*templateVariable2*]` &tv3 = `[*templateVariable3*]`       !]
      And in the snippet:

      <?php
      $empty = "Output when they are empty";
      $notEmpty = "Output when one is not empty";
      
      $output = $notEmpty;
      
      $tvs = $tv1 . $tv2 . $tv3;
      $tvs = strip($tvs);
      if (empty($tvs) ) {  /* all are empty */
         $output  = $empty;
      }

      You could also put the two different output sections in chunks:

      <?php
      $empty = "name_of_empty_chunk";
      $notEmpty = "name_of_not_empty_chunk";
      
      $output = $notEmpty;
      
      $tvs = $tv1 . $tv2 . $tv3;
      $tvs = strip($tvs); 
      
      if (empty($tvs)) {  /* all are empty */
         $output  = $empty;
      }
      
      return $modx->getChunk($output);
      ?>
        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
        • 4041
        • 788 Posts
        I made this snippet recently and it sounds like it is close to what you are asking. I use it in the content section of the template to format the output depending on if the tv’s have content or not.

        <?php
        /*    Content_Formatter
              [!Content_Formatter!]
        
        */
        $thisid = $modx->documentIdentifier;
        $output ='';
        $tvcontentT = $modx->getTemplateVarOutput(array("R_Column_TContent"), $docid=$thisid);
        $tvcontentM = $modx->getTemplateVarOutput(array("R_Column_MContent"), $docid=$thisid);
        $tvcontentB = $modx->getTemplateVarOutput(array("R_Column_BContent"), $docid=$thisid);
        
        if($tvcontentT['R_Column_TContent'] =='' && $tvcontentM['R_Column_MContent'] =='' && $tvcontentB['R_Column_BContent'] ==''){
            // if all three tv's are empty, output this:  
           $output .='<h2>[*longtitle*]</h2>[*content*]
                      [*C_Column_BContent*]';
        }else{
           // if any of the tv's have content, then output this:
           $output .='<div style="float:left; width:500px;">
                        <h2>[*longtitle*]</h2>[*content*]
                        [*C_Column_BContent*]
                      </div>
                      <div style="float:right; width:200px;">
                        [*R_Column_TContent*]
                        [*R_Column_MContent*]
                        [*R_Column_BContent*]
                      </div>
                      <div style="clear:both;"></div>';
        }
        return $output;
        ?>


        Hope it helps send you in the right direction at least smiley
          xforum
          http://frsbuilders.net (under construction) forum for evolution
          • 33982 ☆ A M B ☆
          • 476 Posts
          Thanks for all your replies.

          BobRay, I tried your approach, but I was getting an error message. Unfortunately, I absolutely stink at PHP and therefore can’t debug when there’s a problem, so I played around with PHx some and came up with this that worked:
          [+phx:if=`[*pdf_doc_1*]`:is=``:and:if=`[*pdf_doc_2*]`:is=``:and:if=`[*pdf_doc_3*]`:is=``:then=``:else=`{{coupons}}`+]

          I have never tried the and:if statement with PHx...worked like a charm...thanks BobRay for pointing me in the right direction.
            Precision Web Development ... SmashStack.com
            • 3749
            • 24,544 Posts
            I’m glad you got it working. wink

            BTW, If you want to try the snippet approach again, let me know what the error message was and I’ll try to figure out what went wrong.

            I did notice a formatting error in my message that caused an error in the snippet call (fixed now).
              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
              • 33982 ☆ A M B ☆
              • 476 Posts
              BTW, If you want to try the snippet approach again, let me know what the error message was and I’ll try to figure out what went wrong.

              Unfortunately the site is live now so I don’t want to reinstate the snippet that was taking the site down, but the whole screen went white with 2 lines of error messages, so something pretty significant I guess wink
                Precision Web Development ... SmashStack.com
                • 3749
                • 24,544 Posts
                Quote from: flinx at Jul 29, 2009, 10:31 PM

                BTW, If you want to try the snippet approach again, let me know what the error message was and I’ll try to figure out what went wrong.

                Unfortunately the site is live now so I don’t want to reinstate the snippet that was taking the site down, but the whole screen went white with 2 lines of error messages, so something pretty significant I guess wink

                That could be just a typo in the PHP and the site was probably not actually down, but I can understand not wanting to mess with something that’s working. wink
                  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
                  • 33238
                  • 388 Posts
                  Quote from: flinx777 at Jul 29, 2009, 02:31 AM
                  Thanks for all your replies.

                  BobRay, I tried your approach, but I was getting an error message. Unfortunately, I absolutely stink at PHP and therefore can't debug when there's a problem, so I played around with PHx some and came up with this that worked:
                  [+phx:if=`[*pdf_doc_1*]`:is=``:and:if=`[*pdf_doc_2*]`:is=``:and:if=`[*pdf_doc_3*]`:is=``:then=``:else=`{{coupons}}`+]

                  I have never tried the and:if statement with PHx...worked like a charm...thanks BobRay for pointing me in the right direction.


                  Mate, Thank you. You save my life tongue
                    --
                    ysanmiguel.com