0

I have this code which works fine if the target variable is a single value

Assumption is $bank_country is a single value such as Brazil, there are no issue

<select class="form-control" multiple="1" name="country[]"> <option<? echo ($bank_country == 'Albania') ? ' selected="selected"' : ''; ?> value="Albania">Albania</option> <option<? echo ($bank_country == 'Algeria') ? ' selected="selected"' : '';?> value="Algeria">Algeria</option> <option<? echo ($bank_country == 'American Samoa') ? ' selected="selected"' : '';?> value="American Samoa">American Samoa</option> <option<? echo ($bank_country == 'Andorra') ? ' selected="selected"' : '';?> value="Andorra">Andorra</option> <option<? echo ($bank_country == 'Angola') ? ' selected="selected"' : '';?> value="Angola">Angola</option> 

However as my value is in multiple, it could be happen this way

Brazil*Germany*Spain 

whereby I have this code

if(strstr($bank_country,"*")) { $country_arr = explode("*",$bank_country); foreach($country_arr as $cca) { //code to echo out country with select on country that match } } 

How do I echo out my country when it have multiple value and it could have Brazil echo out with selected="selected" , so do Germany and Spain.

I been thinking for a while and got no idea how to append to my code to make it do Multiple if condition on this country before do a echo out with selected="selected"

Thanks for helping!!

2
  • How are you echoing out the actual <select>? Like the code provided? Commented Aug 2, 2014 at 10:28
  • Unrelated to your exact question- Why are you not just doing $all_countries = array('Albania', 'Algeria', 'American Samoa'); foreach ($all_countries as $country) { echo "<option value='$country' selected='" . ($country === $bank_country ? 'selected' : '') . "' >$country</option> } ? Commented Aug 2, 2014 at 10:34

3 Answers 3

2

You could use in_array. Check with !empty($country_arr) if the array exists, if not, you will only have one country.

if (strstr($bank_country,"*")) { $country_arr = explode("*", $bank_country); } <option<? echo ((!empty($country_arr) && in_array('Albania', $country_arr)) || $bank_country == 'Albania') ? ' selected="selected"' : ''; ?> value="Albania">Albania</option> 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the guide, its definitely save my day !! explode might not work because I do have singular country for $bank_country, could this code handle if $bank_country is an array (by * delimiter) or $bank_country is a single value ( without * delimiter )
2

You should go on with you code and change the if to this:

$selected_value = ""; // this line declares the variable outside the condition, otherwise the variable would only be available inside the condition. if(is_array($selected_countries)) { $coun = explode("8", $sel_countries); $selected_value = null; // DO NOT FORGET THIS, as it resets the selected value on each iteration if(in_array("brazil", $coun)) $selected_value = "selected='selected'"; } else { $selected_value = null; if("brazil" == $coun) $selected_value = "selected='selected'"; } <option <?php echo $selected_value; ?> value="brazil">brazil</option> 

It is more professional to just echo $selected_value inside the option and leave all operations and conditions before the HTML. With this, you can handle your code better in future. What we have done? We just echoed the result of our operation inside the option tag, but prior to it, we have compared all the values and have added something proper to $selected_value, or have left it no value, keeping our HTML and target items are clean and maintainable.

2 Comments

Thanks for the guide, its definitely save my day !! explode might not work because I do have singular country for $bank_country, could this code handle if $bank_country is an array (by * delimiter) or $bank_country is a single value ( without * delimiter )
see the updated answer. It solves all of your problems.
1

you can use in_array

$country_arr = explode("*",$bank_country); ?> <select class="form-control" multiple="1" name="country[]"> <option <?php echo (in_array('Albania', $country_arr)) ? ' selected="selected"' : ''; ?> value="Albania">Albania</option> <option <?php echo (in_array('Algeria', $country_arr)) ? ' selected="selected"' : '';?> value="Algeria">Algeria</option> <option <?php echo (in_array('American Samoa', $country_arr)) ? ' selected="selected"' : '';?> value="American Samoa">American Samoa</option> <option <?php echo (in_array('Andorra', $country_arr))? ' selected="selected"' : '';?> value="Andorra">Andorra</option> <option <?php echo (in_array('Angola', $country_arr)) ? ' selected="selected"' : '';?> value="Angola">Angola</option> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.