Jump to content

Recommended Posts

Hi everyone,

 

On my website i have a drop down box on the account information page that has a list of countries. In the example below, im using the php to do 2 things, 1: if the user changes the country and clicks submit but an error is found on the page, rather than revert back to the original country it stays as the newly selected one. 2: if the user does not change the country, the country from the database is simple displayed.

 

As you can see below it is taking up quite a bit of code. I was wondering if anybody has any ideas how i can make this smaller?

 

<select name="country">
			<option value="select" selected>Select...</option>
			<option value="Afganistan" <?php if ($submit) { if ($country == "Afganistan"){ echo "selected";}} else {if ($row['country'] == "Afganistan") {echo "selected";}}  ?>>Afghanistan</option>
			<option value="Albania" <?php if ($submit) {if ($country == "Albania"){echo "selected";}} else {if ($row['country'] == "Albania") {echo "selected";}} ?>>Albania</option>
			<option value="Algeria" <?php if ($submit) {if ($country == "Algeria"){echo "selected";}} else {if ($row['country'] == "Algeria") {echo "selected";}} ?>>Algeria</option>
			<option value="American Samoa" <?php if ($submit) {if ($country == "American Samoa"){echo "selected";}} else {if ($row['country'] == "American Samoa") {echo "selected";}} ?>>American Samoa</option>
			<option value="Andorra"<?php if ($submit) {if ($country == "Andorra"){echo "selected";}} else {if ($row['country'] == "Andorra") {echo "selected";}} ?>>Andorra</option>
			<option value="Angola"<?php if ($submit) {if ($country == "Angola"){echo "selected";}} else {if ($row['country'] == "Angola") {echo "selected";}} ?>>Angola</option>

 

Thanks

What you'll want to do is store your countries in an array

$countries = array('Afganistan', 'Albania', 'Algeria', 'American Samoa', 'Andorra', 'Angola',  etc..);

 

Then use a loop to output each country into an <option></option>. Within this loop you'll have an if statement which selects the chosen country.

<select name="country">
<?php
$selectedCountry = isset($_POST['country']) ? $_POST['country'] : $row['country']; // get the chosen country
foreach($countries as $country):
    $selected = ($selectedCountry == $country) ? ' selected="selected"' : null; // if the current country equals the chosen country then select it.
?>
<option value="<?php echo $country; ?>"<?php echo $selected; ?>><?php echo $country; ?></option>
<?php endforeach; ?>
</select>

Hi wildteen,

 

Thanks for that it looks perfect. My windows 7 messed up yesterday shortly after you posted so i havent had a chance to try it yet. As soon as iv got everything re-loaded ill give it a go.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.