SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Member
    Join Date
    Jan 2005
    Location
    Crystal
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to Put Multiple If Statements in One Variable??

    How do you put multiple if statements in a single variable? if statement like this..

    PHP Code:
    if ($p1 ) {  
        if (
    $p1_color) {echo $tab.$p1_color.'<br>';} 
        if (
    $p1_Blue) { echo "$tab $p1_Blue Blue <Br>";} 
        if (
    $p1_Green) { echo "$tab $p1_Green Green <Br>";} 

        } 
    else { echo 
    "blah blah";} 
    Is there any way to assign one variable to this and when it is called, it only returns the results. I thought something like...

    PHP Code:
    $p1all = array( 
    if (
    $p1 ) {  
        if (
    $p1_color) {echo $tab.$p1_color.'<br>';} 
        if (
    $p1_Blue) { echo "$tab $p1_Blue Blue <Br>";} 
        if (
    $p1_Green) { echo "$tab $p1_Green Green <Br>";} 

        } 
    else { echo 
    "blah blah";} 
    ); 
    or
    PHP Code:
    $p1all = [ 
    if (
    $p1 ) {  
        if (
    $p1_color) {echo $tab.$p1_color.'<br>';} 
        if (
    $p1_Blue) { echo "$tab $p1_Blue Blue <Br>";} 
        if (
    $p1_Green) { echo "$tab $p1_Green Green <Br>";} 

        } 
    else { echo 
    "blah blah";} 
    );] 
    dumb I bet.. not sure what to do.. but I am just trying to convey my idea.
    I want the results all in one simple variable.


    __________________
    Chris

  2. #2
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    Germany
    Posts
    550
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $str '';
    if (
    $p1 ) {
    if (
    $p1_color) {$str .= $tab.$p1_color.'<br />';}
    if (
    $p1_Blue) { $str .= $tab.' '.$p1_Blue.' Blue <br />';}
    if (
    $p1_Green) { $str .= $tab.' '.$p1_Green.' Green <br />';}

    }else

      
    $str .= 'blah blah';
    }
    echo 
    $str
    Something like this ?

  3. #3
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try the ternary operator too:
    PHP Code:
    $str $p1 ? (
        ( 
    $p1_color $tab.$p1_color.'<br />' '' )
      . (
    $p1_Blue $tab.' '.$p1_Blue.' Blue <br />' '' )
      . (
    $p1_Green $tab.' '.$p1_Green.' Green <br />' '')
    )  : 
    'blah blah' 
    It's safer to keep the parenthesis.
    Some like it, some don't...
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  4. #4
    SitePoint Member
    Join Date
    Jan 2005
    Location
    Crystal
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What if someone has chosen 2 out of three options? Will these methods still work? I am not really sure what is happening in those examples. Would someone be kind enough to break it down into plain English what is happening step by step? I really want to understand it.

  5. #5
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    Germany
    Posts
    550
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Daimaju
    PHP Code:
    $str '';
    if (
    $p1 ) {
    if (
    $p1_color) {$str .= $tab.$p1_color.'<br />';}
    if (
    $p1_Blue) { $str .= $tab.' '.$p1_Blue.' Blue <br />';}
    if (
    $p1_Green) { $str .= $tab.' '.$p1_Green.' Green <br />';}

    }else

      
    $str .= 'blah blah';
    }
    echo 
    $str
    Translation (line by line):
    $str is an empty string
    if $p1 is set
    if $p1_color is set add "$tab.$p1_color</br />" to $str;
    if $p1_Blue is set add "$tab $p1_Blue Blue <br />" to $str;
    if $p1_Green is set add "$tab $p1_Green Green <br />" to $str;
    if $p1 is not set add 'blah blah' to $str
    echo $str;

    If someone chooses 2 optiones, e.g. blue and green the oputput will be
    "$tab $p1_Blue Blue <br />$tab $p1_Green Green <br />"

    hth

  6. #6
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by revivalfire
    What if someone has chosen 2 out of three options? Will these methods still work? I am not really sure what is happening in those examples. Would someone be kind enough to break it down into plain English what is happening step by step? I really want to understand it.
    For the code, I think it's really a basic thing, would be the same in numerous programing languages.
    Just, I'm puzzled by your question. Perhaps you could explain us a little bit more of what you like to do ?
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  7. #7
    SitePoint Addict
    Join Date
    Nov 2001
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think what you're looking for is called a function

    If I understand what you're trying to do, then this function does what you want:
    PHP Code:
       function p1all() {
      global 
    $p1$p1_color$p1_Blue$p1_Green$tab;
      
    $retval "";
           if (
    $p1 ) {   
           if (
    $p1_color$retval $tab.$p1_color.'<br>';
           if (
    $p1_Blue$retval .= "$tab $p1_Blue Blue <Br>";
           if (
    $p1_Green$retval .= "$tab $p1_Green Green <Br>";  
     
          }  else 
    $retval "blah blah";
           return 
    $retval;
       } 
    Then the "value" of p1all would be the result of that those if statements.

    PHP Code:
       $value p1all();  // $value would be the result of the if statements 
    Is that what you want? It appends each of your echo statements into $retval, then returns it. Keep in mind that although this should work, it's not the best way to do it.

  8. #8
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Shakin,
    Why you don't put all that $p1... stuff as function arguments ?
    Then we see result depends on entries.

    Anyway, perhaps that's what RevivalFire wants, you're right.
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  9. #9
    SitePoint Addict
    Join Date
    Nov 2001
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by toggg
    Shakin,
    Why you don't put all that $p1... stuff as function arguments ?
    Then we see result depends on entries.

    Anyway, perhaps that's what RevivalFire wants, you're right.
    Yes, you're absolutely right, but I wanted to keep it as simple as possible for my demonstration. If revivalfire doesn't know how to use functions then I at least wanted to make it simple to read (eg. no wondering what the variables are doing in two places). Maybe I'm sending him off in the wrong direction like that, so here's a better version.

    PHP Code:
      function p1all($p1$p1_color$p1_Blue$p1_Green$tab) {
       
    $retval "";
       if (
    $p1 ) {   
            if (
    $p1_color$retval $tab.$p1_color.'<br>';
            if (
    $p1_Blue$retval .= "$tab $p1_Blue Blue <Br>";
            if (
    $p1_Green$retval .= "$tab $p1_Green Green <Br>";  
      
       }  else { 
    $retval "blah blah" };
       return 
    $retval;
     }
      
      
    // $value would be the result of the if statements  
       
    $value p1all($p1$p1_color$p1_Blue$p1_Green$tab); 
    Last edited by shakin; Jan 26, 2005 at 12:44.

  10. #10
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Check-in shakin

    Good done shakin,
    I didn't mind.
    Better understand what really wants revivalfire
    {bracket the else block?}
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  11. #11
    SitePoint Addict
    Join Date
    Nov 2001
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by toggg
    {bracket the else block?}
    meh. I'm not very good at off-the-cuff code

    Actually, do you really need those brackets? Since it's only a one-liner I don't think so, just like how you can do "if (condition) expression;". I normally use brackets anyway for better readability, so I haven't tested the one-liner method on 'else' before.

  12. #12
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, nobody needs those if/else/for/foreach/switch ... brackets.
    It's also making no difference for PHP interpreter.

    Just making a difference for people who will read that code later, inclusive who wrote it. (better showing the conditional blocks limits)

    Just making a difference if the code is somewhere wrong, and I like to easy debug with some echo but under pressure when it comes I don't like to recreate blocks.

    Ok, just a little quizz: how do you make a block in COBOL ?
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  13. #13
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Quote Originally Posted by toggg
    Ok, just a little quizz: how do you make a block in COBOL ?
    ouch, quit it.
    I have no recolection from classes in 1990, but a says:

    Quote Originally Posted by IBM Enterprise COBOL site
    The following is the format for a COBOL method definition.

    Format: COBOL method definition

    Read syntax diagramSkip visual syntax
    Code:
    diagram>>-+-IDENTIFICATION-+--DIVISION.-------------------------------->
       '-ID-------------'
    
    >--METHOD-ID--+---+--method-name-1--+---+----------------------->
                  '-.-'                 '-.-'
    
    >--+---------------------------------------+-------------------->
       '-other-identification-division-content-'
    
    >--+------------------------------------------------------------+-->
       '-ENVIRONMENT DIVISION.--method-environment-division-content-'
    
    >--+----------------------------------------------+------------->
       '-DATA DIVISION.--method-data-division-content-'
    
    >--+--------------------------------------------------------------------------+-->
       '-method-procedure-division-header.--+-----------------------------------+-'
                                            '-method-procedure-division-content-'
    
    >--END METHOD--method-name-1.----------------------------------><

    METHOD-ID
    Identifies a method definition. See METHOD-ID paragraph for details.
    method-procedure-division-header
    Indicates the start of the procedure division and identifies method parameters and the returning item, if any. See The procedure division header for details.
    END METHOD
    Specifies the end of a method definition.

    Methods defined in an object definition are instance methods. An instance method in a given class can access:

    * Data defined in the data division of the object paragraph of that class (instance data)
    * Data defined in the data division of that instance method (method data)

    An instance method cannot directly access instance data defined in a parent class, factory data defined in its own class, or method data defined in another method of its class. It must invoke a method to access such data.

    Methods defined in a factory definition are factory methods. A factory method in a given class can access:

    * Data defined in the data division of the factory paragraph of that class (factory data)
    * Data defined in the data division of that factory method (method data)

    A factory method cannot directly access factory data defined in a parent class, instance data defined in its own class, or method data defined in another method of its class. It must invoke a method to access such data.

    Methods can be invoked from COBOL programs and methods, and they can be invoked from Java programs. A method can execute an INVOKE statement that directly or indirectly invokes itself. Therefore, COBOL methods are implicitly recursive (unlike COBOL programs, which support recursion only if the RECURSIVE attribute is specified in the program-ID paragraph.)
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  14. #14
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    He, you kill !
    I mean get a few commands packed in block (in PROCEDURE DIVISION)
    You guy, in 1990 I was coding Pascal, was that begin ... end ? (semicolumns ?)
    No, I mean punched COBOL, divised and sectionned (blocked ?) statements...
    It's a good quizz.
    Last edited by toggg; Jan 26, 2005 at 14:01. Reason: No answer up to now
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  15. #15
    SitePoint Addict
    Join Date
    Nov 2001
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by toggg
    Ok, just a little quizz: how do you make a block in COBOL ?
    In 1999 my employer sat me down in front of some COBOL source code and asked me to fix the Y2K bug that they found during a clock roll-forward test. I had never seen COBOL before or since and I couldn't fix the bug.

    I know COBOL has IF[condition] and END-IF. My memory of procedures is a bit fuzzy, probably because they confused me at the time. I believe procedures are enclosed in periods. Maybe it's parameters that really confused me

    eg.
    Code:
       DisplayText.
      	DISPLAY "Hellow World".

  16. #16
    SitePoint Addict toggg's Avatar
    Join Date
    Jan 2005
    Location
    Auvergne/France
    Posts
    253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bravo !
    You make a block just omitting the period betwenn statements: then they are considered as a block as {} or begin end.
    That's why I'm quite blind now: just imagine how many hours just searching after a period too much or too less: Horror !
    bertrand Gugger toggg.com linux, PHP, Auvergne/France open source

  17. #17
    SitePoint Member
    Join Date
    Jan 2005
    Location
    Crystal
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the late reply. Been under the weather a bit and was frankly frustrated with the script, but after coming back to it and looking at the post by Daimaju, it seemed to work for me.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •