Results 1 to 2 of 2

Thread: Turning part of an array into a variable to output

  1. #1
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default Turning part of an array into a variable to output

    forum_amnesiac posted some code in another thread which is very similar to mine but I started a new thread as to not interfere with the other thread question.

    This also creates a dynamic dropdown list
    PHP Code:
     $carriers = array( 
     
    'Alltel' => '@message.alltel.com'
     
    'Ameritech' => '@paging.acswireless.com'
     
    'AT&T' => '@txt.att.net'
     
    'Bellsouth' => '@sms.bellsouth.com'
    ); 
    Function for select
    PHP Code:
    function carrierOptions($selected

     
    $html ''
     global 
    $carriers
     foreach(
    $carriers as $carrier => $email
     { 
      if(
    $selected==$carrier) {  $html .= '<option value="'.$carrier.'" SELECTED>'.$carrier.'</option>'; } 
      else {  
    $html .= '<option value="'.$carrier.'">'.$carrier.'</option>'; } 
     } 
     return 
    $html

    After submit I use the following vars to echo out who the message was sent to.
    PHP Code:
     $carrier $carriers[$carrier]; 
     
    $to $phone.$carrier
    $to would produce something like xxxxxxxxxx @ message.alltel.com (spaces to not create a link)
    Everything works fine
    but I would like to show the selected value from the dropdown also
    PHP Code:
    echo "Carrier selected was: ".$name_var." and your message was sent to ".$to."";
    //Carrier selected was: Alltel and and your message was sent to xxxxxxxxxx@message.alltel.com 
    With this array type I don't believe there is a way to show the carrier name that way since the array associates to the provider email address like => $email

    Could the above function be re-written maybe to dynamically just provide the Carrier name that was selected? I appreciate any help, thank you.
    Last edited by ?foru; 05-17-2009 at 04:57 AM.

  2. #2
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    I was able to get this working with

    PHP Code:
    $carrierto array_keys($carriers,$_POST['carrier']); 
    $carrierto to avoid confusion with the array value $carrier like...

    PHP Code:
    ...
    foreach(
    $carriers as $carrier => $email)
    ... 
    Then this worked to echo the result I needed.
    PHP Code:
    echo "Carrier selected was: ".$carrier." and your message was sent to ".$to.""
    Thank you

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
  •