0

This is my multidropdown

<select style="width:250px"name="locatie[]"id="contact" data-    
    placeholder="contactpersoon" class="chosen-select" id="e9"multiple tabindex="4"> 
        <option value=""></option> 
        <option value="United States, aaa, bbb, ccc">United States</option> 
        <option value="United Kingdom">United Kingdom</option> 
        <option value="Afghanistan">Afghanistan</option>
</select>

this is my php code

$arr = $_POST["contact"];
reset($arr);
while (list(, $value) = each($arr)) {
    echo "Value: $value<br />\n";
}

When i select all the options,

OUTPUT

Value: United States, aaa, bbb, ccc

Value: United Kingdom

Value: Afghanistan

how can i seperate the first value (united states, aaa, bbb, ccc) TO

Value: United States

Value: aaa

Value: bbb

Value: ccc

Value: United Kingdom

Value: Afghanistan

Community
  • 1
  • 1
hans
  • 29
  • 1
  • 6

3 Answers3

1

Take a look here: Can an Option in a Select tag carry multiple values?

You could try using a JSON array instead of a comma-delimited list.

Community
  • 1
  • 1
Mike DeMille
  • 342
  • 1
  • 5
  • 17
0

You need to loop through the array first, split the string and add all the values to a new array:

<?php

$original = $_POST['contact'];
$output = array();

reset($original);

foreach($original as $value) {
    if(strpos($value, ',') === false)
        $output[] = $value; // add value straight away if it doesn't need splitting
    else {
        // split string by comma and trim whitespace from pieces
        $bits = explode(',', $value);
        foreach($bits as $bit) {
            $output[] = trim($bit);
        }
    }
}

// now loop through $output array to get desired result:
foreach($output as $value) {
    echo "Value: " . $value . "<br>\n";
}
?>
scrowler
  • 24,273
  • 9
  • 60
  • 92
0

I am not quite sure that I follow what you want and need,, BUT to separate the first value into individual items and list them in the order in which you demonstrate your list you should split and trim the string that represents your first value... while in the while loop...

To split the string with a string you can look at function explode in PHP

http://us3.php.net/manual/en/function.explode.php

To trim, look at this

http://php.net/manual/en/function.trim.php

Your string "United States, aaa, bbb, ccc" has to be split by a comma character "," and then elements of the split array have to be trimmed to get rid of the white space that precedes them. As far as the code goes I will give you sort of a pseudo code that you can adjust for your needs.

  $arr = $_POST["contact"];
  reset($arr);

  while (list(, $value) = each($arr)) {

      // when you split the string you get an array and if the number of elements in an array is more then 1 that means your string contains a comma.. You split a string into subvalues and echo them in a loop...

      if (count(explode(",", $value)) > 1) { 

          $subvalues = explode(",", $value);
          foreach ($subvalue in $subvalues){ 
          echo "Value: trim($subvalue)<br />\n"; 
          }    
      } else {
          echo "Value: $value<br />\n";
      }

  }
GRowing
  • 4,629
  • 13
  • 52
  • 75