We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28033
    • 925 Posts
    <?php
    $css = isset($css) ? $css : 'tab3';
    
    $link = preg_replace('/QU/','?',$link);
    $link = preg_replace('/EQ/','=',$link);
    $link = preg_replace('/AM/','&',$link);
    $data = preg_replace('/SB/','<b>',$data);
    $data = preg_replace('/EB/','</b>',$data);
    
    if (isset($extra)) {
        return '<td class="'.$css.'" width="'.$width.'" colspan="'.$colspan.'" rowspan="'.$rowspan.'">'.$data.'</td>';
    } else {
    
    if (isset($link)) {
    
    if (isset($pop)) {
        return '<td class="'.$css.'"><a href="'.$link.'" target="_blank">'.$text.'</a></td>';
    } else {
        return '<td class="'.$css.'">'.$data.'</td>';
    }
    
    } else {
        return '<td class="'.$css.'">'.$data.'</td>';
    }
    
    }
    ?>


    For &pop, I always know it will use that code is that is there, and if it isn’t there, the snippet won’t even "see" that code, and will check to see if &link exists, either doing that IF or going to the ELSE at the end.

    Thus, in this case, is the ELSE statement even needed, or will I break the script for some odd reason doing that?
      My Snippets
      -> PopUpChunk v1.0
      • 33372
      • 1,611 Posts
      So then it seems like the question should be why do you even need the $pop var at all (or the if...else)?
        "Things are not what they appear to be; nor are they otherwise." - Buddha

        "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

        Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
        • 3749
        • 24,544 Posts
        Quote from: Soshite at Nov 07, 2007, 08:41 PM

        <?php
        $css = isset($css) ? $css : 'tab3';
        
        $link = preg_replace('/QU/','?',$link);
        $link = preg_replace('/EQ/','=',$link);
        $link = preg_replace('/AM/','&',$link);
        $data = preg_replace('/SB/','<b>',$data);
        $data = preg_replace('/EB/','</b>',$data);
        
        if (isset($extra)) {
            return '<td class="'.$css.'" width="'.$width.'" colspan="'.$colspan.'" rowspan="'.$rowspan.'">'.$data.'</td>';
        } else {
        
           if (isset($link)) {
        
              if (isset($pop)) {
                 return '<td class="'.$css.'"><a href="'.$link.'" target="_blank">'.$text.'</a></td>';
              } else {
                 return '<td class="'.$css.'">'.$data.'</td>';
              }
        
           } else {
              return '<td class="'.$css.'">'.$data.'</td>';
           }
        
        }
        ?>


        For &pop, I always know it will use that code is that is there, and if it isn’t there, the snippet won’t even "see" that code, and will check to see if &link exists, either doing that IF or going to the ELSE at the end.

        Thus, in this case, is the ELSE statement even needed, or will I break the script for some odd reason doing that?
        Soshite,

        As I read it (and if I’m right about which "else" you mean), if you take out that else, if $link is set and $pop is not it will return nothing. Notice that I changed your indenting so it’s easier to see what’s going on.

        If I’m understanding what you want to do, I think you could do it much more cleanly with:

        if (isset($extra)) { // extra is set. We're done.
            return '<td class="'.$css.'" width="'.$width.'" colspan="'.$colspan.'" rowspan="'.$rowspan.'">'.$data.'</td>';
        }  
        
        If (isset($link) && isset($pop)) {  // both are set
            return '<td class="'.$css.'"><a href="'.$link.'" target="_blank">'.$text.'</a></td>';
        } else {   // at least one is not set
            return '<td class="'.$css.'">'.$data.'</td>';
        }
        


        Many old-school coders would also complain that the code has three exit points instead of one. To solve that, you could use a variable, $output, and set it like this:

        $output = "";
        If (isset($extra)) {
          $output .=  '<td class="'.$css.'" width="'.$width.'" colspan="'.$colspan.'" rowspan="'.$rowspan.'">'.$data.'</td>';
        } elseif (isset($link) && (isset($pop)) {
          $output .= '<td class="'.$css.'"><a href="'.$link.'" target="_blank">'.$text.'</a></td>'; 
        } else {
          $output .= '<td class="'.$css.'">'.$data.'</td>';
        }
        
        return $output;


        The .= (concatenation) operator tacks whatever is on the right onto the end of the variable on the left. This is a good practice in case you ever want to massage $output before or after your if statements. For example, if you wanted to wrap the html code in a <div class="something"></div>, you’d just put:

        $output = '<div class="something">'; // at the beginning

        and
        $output .= '</div>'; // at the end just before the return.
        


        Using $output and a single return also makes it easier to debug and (I think) takes some strain off the server since it doesn’t have to interpret the return code.

        To refine it a little further, since all three return strings start with ’<td class="’.$css.’", you could make that the initial value of $output and leave that out of the later statements, but it would be easy to make a typo that would screw things up.

        I hope this makes sense and isn’t TOO much more than you wanted to know. . . wink

        Bob
          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
          • 28033
          • 925 Posts
          Output...that’s something new. Seems like it’ll be useful. Two quick questions from your post...

          1) With IFELSE, can I have more than one of them if I wanted? Or is it only IF/IFELSE/ELSE that can each be used only once?
          2) In the third to last code box, you have output w/ "". But in the last two code boxes, it’s ’’. I’m guessing I should use ’’?
            My Snippets
            -> PopUpChunk v1.0
            • 3749
            • 24,544 Posts
            Quote from: Soshite at Nov 08, 2007, 07:38 PM

            Output...that’s something new. Seems like it’ll be useful. Two quick questions from your post...

            1) With IFELSE, can I have more than one of them if I wanted? Or is it only IF/IFELSE/ELSE that can each be used only once?
            2) In the third to last code box, you have output w/ "". But in the last two code boxes, it’s ’’. I’m guessing I should use ’’?

            1) Be careful, it’s elseif, not ifelse.

            You can use as many as you need, although if you plan your code well, it’s rare that you need more then one or two:

            if (something) {
            
            } elseif (something else) {
            
            } elseif (something different) {
            
            } elseif (some third thing) {
            
            } else {
            
            }
            

            2) I’m not sure I understand you here, but I think your asking about why the earlier line is $output="" with no dot.

            That sets the value of $output to an empty string. The later statements ($output .= stuff) add things to the end of that string. Here’s an example:

            $output = "";   // output set to empty string
            
            $output .= "<p>hello";  // output is now "<p>hello"
            $output .= " goodbye"; // output is now "<p>hello goodbye"
            $output .= " see you tomorrow</p>";  // output is now "<p>hello goodbye see you tomorrow</p>"
            
            return $output;  // returns "<p>hello goodbye see you tomorrow</p>" from the snippet
            


            Bob

              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
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              If you find yourself with more than one or two "elseif" statements, you’re better off learning about the "switch" statement. Very nice for cases where you might have several things to check.
              switch($name) {
                case "Joe": // if $name == 'Joe'
                  echo "Hello, Joe.";
                  break;
                case "John": // elseif $name == 'John"
                  echo "Get lost, John.";
                  break;
                case "Susan": // elseif $name == 'Susan'
                  echo "Well, hello there, gorgeous!";
                  break;
                default: // anything else
                  echo "Do I know you?";
              }
              

              http://il2.php.net/switch
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 3749
                • 24,544 Posts
                Note, though, that the switch statement is only good if you have a single thing to test that can have multiple values (as in Sotwell’s example).

                It won’t work if you want to test several different things like in Soshite’s example of testing several different variables to see if they’re set.

                (Hmmm. . . adding that php forum is sounding better and better) wink.

                Bob
                  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