Why cant I select multple in Php

Hi,

I have a user account form, which form has drop down lists.

When the user returns to their account page I want to auto select the drop down list items that they have previously selected and were thus saved in their records. However when the page is generated by Php somehow ONLY the 1st previously selected item is selected and not the rest. Can someone tell me what the HEK is going on and what is the solution?

FYI, here is a sample of the related page:

and here is the sample Php code for it:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> Testing In Array</TITLE>

  <META NAME="Description" CONTENT="">
 </HEAD>

 <BODY>
 <?php 

$country = 'United States, Australia, United Kingdom';
$country_array = explode(',', $country);


?>

<form>

<select name="country[]" size="6"  multiple>
                          <option value="-1" <?php if (isset($country_array) AND (count($country_array) > 0)) {echo ''; } else {echo 'selected';} ?>>Not Selected</option>

                          <option value="United States" <?php if (isset($country_array) AND (in_array('United States', $country_array))) {echo 'selected'; } ?>> United States </option>

						  <option value="United Kingdom" <?php if (isset($country_array) AND (in_array('United Kingdom', $country_array))) {echo 'selected'; } ?>>United Kingdom</option>

						  <option value="Australia" <?php if (isset($country_array) AND (in_array('Australia', $country_array))) {echo 'selected'; } ?>>Australia</option>

						  <option value=Canada <?php if (isset($country_array) AND (in_array('Canada', $country_array))) {echo 'selected'; } ?>>Canada</option>

                          <option value=Algeria>Algeria</option>
                          <option value="American Samoa">American Samoa</option>
		</select>


</form>



 </BODY>
</HTML>

The contents of the array will be this:

array(‘United States’,’ Australia’,’ United Kingdom’);

Notice the leading space for Australia and United Kingdom.

Use array_map() to trim the white space.


<?php

$country = 'United States, Australia, United Kingdom';
$country_array = /*array_map('trim',*/explode(',', $country)/*)*/;

echo '<pre>',var_dump($country_array),'</pre>';


$country = 'United States, Australia, United Kingdom';
$country_array = array_map('trim',explode(',', $country)); 

echo '<pre>',var_dump($country_array),'</pre>';

?>


array(3) {
  [0]=>
  string(13) "United States"
  [1]=>
  string(10) " Australia"
  [2]=>
  string(15) " United Kingdom"
}

array(3) {
  [0]=>
  string(13) "United States"
  [1]=>
  string(9) "Australia"
  [2]=>
  string(14) "United Kingdom"
}

Hi,

Well this array I am showing here is just a sample.
The actual array is a field in a MySQL Table.
So when the field from MySQL is selected, then the result is something like this:
$country = ‘United States, Australia, United Kingdom’;

which then I am converting into an array with:

$country_array = explode(‘,’, $country);

so since explode does not produce this which you have suggested:
array(‘United States’,’ Australia’,’ United Kingdom’);

How am I supposed to go from:
$country = ‘United States, Australia, United Kingdom’;
to that array which you have suggested that explode()
does not produce?

Regards,

Your code works :slight_smile:

I donot know how. But it is working.
My hats off to you Sir :slight_smile:

array_map() is basically the same thing as [URL=“http://php.net/manual/en/function.call-user-func.php”]call_user_func but the only difference is array_map takes the array and trims all the white space for us where as with call_user_func we would have to write our own foreach loop and trim the white space manually.

Thanks for that info.