Need help with a function

Discussion in 'PHP' started by rob7676, Nov 6, 2009.

  1. #1
    I am trying to display some data from a submitted form. I can get it to display, but need some help with the way it echos the data if multiple responses are available. Not to sure if I need to make a function or not, but this is what I have right now.
    
    if ($row['txtNotificationAgency1'] == "GLO") {
    echo "GLO ";
    } 
    if ($row['txtNotificationAgency2'] == "TCEQ") {
    echo "TCEQ ";
    }
    if ($row['txtNotificationAgency3'] == "RRC") {
    echo "RRC ";
    }
    Code (markup):
    What I would like to happen is say if "GLO" & "RRC" the the output display GLO / RRC, but if only say "RRC" then output display RRC. I'm not very knowledgeable in PHP, but I think I need some kind of function but not sure how to go about it. Can anyone help?!?!?
     
    rob7676, Nov 6, 2009 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $out = array();
    if ($row['txtNotificationAgency1'] == "GLO") {
        $out[] = "GLO ";
    } 
    if ($row['txtNotificationAgency2'] == "TCEQ") {
        $out[] = "TCEQ ";
    }
    if ($row['txtNotificationAgency3'] == "RRC") {
        $out[] = "RRC ";
    }
    echo implode(' / ', $out);
    PHP:
    Something like that should do it
     
    JAY6390, Nov 6, 2009 IP
    rob7676 likes this.
  3. rob7676

    rob7676 Peon

    Messages:
    82
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, I'll give it a try.
     
    rob7676, Nov 6, 2009 IP
  4. TSH-Nathan

    TSH-Nathan Greenhorn

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #4
    You beat me to it JAY

    if ($row['txtNotificationAgency1'] == "GLO")
    	$output[] = "GLO";
    
    if ($row['txtNotificationAgency2'] == "TCEQ")
    	$output[] = "TCEQ";
    
    if ($row['txtNotificationAgency3'] == "RRC")
    	$output[] = "RRC";
    
    $str = implode(' / ',$output);
    PHP:
     
    TSH-Nathan, Nov 6, 2009 IP
    rob7676 likes this.
  5. rob7676

    rob7676 Peon

    Messages:
    82
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Worked perfectly, thanks again. Could be very useful with some other projects. After your replies I read about it on php.net and kinda understand it, but not totally. Sure I'll understand more as I use it more.
     
    rob7676, Nov 6, 2009 IP
  6. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The $out[] assigns each of the items to the array $out, then the implode puts each of the texts into one string and separates them with the /
     
    JAY6390, Nov 6, 2009 IP
  7. rob7676

    rob7676 Peon

    Messages:
    82
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I kinda got that part. I think it lost me when it made arrays out of 3 different if statements, didn't know that could be done. Thats why I am liking PHP, every day I learn something new.
     
    rob7676, Nov 6, 2009 IP
  8. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #8
    the [] means add to the array, so it inserts that value as a new element into the array
     
    JAY6390, Nov 6, 2009 IP