perky416 Posted May 14, 2011 Share Posted May 14, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/236409-looking-for-a-better-way-to-remember-display-drop-down-box-data/ Share on other sites More sharing options...
wildteen88 Posted May 14, 2011 Share Posted May 14, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/236409-looking-for-a-better-way-to-remember-display-drop-down-box-data/#findComment-1215414 Share on other sites More sharing options...
perky416 Posted May 15, 2011 Author Share Posted May 15, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236409-looking-for-a-better-way-to-remember-display-drop-down-box-data/#findComment-1215637 Share on other sites More sharing options...
perky416 Posted May 18, 2011 Author Share Posted May 18, 2011 Thanks mate it works perfectly and has saved me hundreds of lines of code Quote Link to comment https://forums.phpfreaks.com/topic/236409-looking-for-a-better-way-to-remember-display-drop-down-box-data/#findComment-1217072 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.