Jump to content

[SOLVED] Loop through a <select> ?


imperialized

Recommended Posts

Ok, the title may be a bit misleading, but I dont know how else to phrase it.

 

Here is my problem:

I have a <select> list with all the countries.

 

ex:

<SELECT NAME=\"location\" id=\"country_id\" SIZE=\"1\">
<OPTION VALUE=\"1\"> Choose One </OPTION>
<OPTION VALUE=\"United Kingdom\">United Kingdom</OPTION>
<OPTION VALUE=\"United States\">United States</OPTION>
<OPTION VALUE=\"Australia\">Australia</OPTION>
<OPTION VALUE=\"New Zealand\">New Zealand</OPTION>
<OPTION VALUE=\"Canada\">Canada</OPTION>

 

Ok, so.. on the registration form, this is fine.. however, when I want to pull the info on the account update page i want it to auto choose the one that they have.

 

Since I am not pulling this info from a database I dont know how to do it.. if i was dynamically creating the list from a db i could just loop it and select it. I hope this makes sense..

Link to comment
Share on other sites

you say you want it to autoselect the one they have.  how do you know which one they have if it isn't in a database?

 

this is an example.  say their country was a variable $userCountry

you should put the pre-determined values into an array then check it as such

 

<?php
$countries = array("United Kingdom","United States","Australia","New Zealand","Canada");
echo "<SELECT NAME='location' id='country_id'>";
foreach($countries as $country) {
   if($country = $userCountry) {
      echo "<OPTION value='" . $country . "' selected='selected'>" . $country . "</OPTION>";
   }
   else {
      echo "<OPTION value='" . $country . "'>" . $country . "</OPTION>"; 
   }
}
echo "</SELECT>";
?>

Link to comment
Share on other sites

ah, I wasn't clear enough, My apologies. My select list is already made. I think i'm going to have to pull all the names out of the list though and put them either in an array or database though.

 

The country they are from is in the database, so I will just have to dynamically create the select list. I got ya, thanks :D

 

//Edit

Considering I am terrible with regex, any suggestions to get me started pulling them out? If each was set like this

<OPTION VALUE=\"Canada\">Canada</OPTION>

How would I pull out canada? (granted all of the countries change as the list progresses)

 

But on another note,

if($x[gender] == "Male"){
//They are male
print "<img src=\"images/boy.gif\" alt=\"Dude\" width=10 height=10 border=0><input type=\"radio\" name=\"gender\" Value=\"Male\" checked> I'm a Dude<br>
<img src=\"images/girl.gif\" alt=\"Woman\" width=10 height=10 border=0><input type=\"radio\" name=\"gender\" Value=\"Female\"> I'm a Girl";		
} else {
//They are female
print "<img src=\"images/boy.gif\" alt=\"Dude\" width=13 height=13 border=0> <input type=\"radio\" name=\"gender\" Value=\"Male\"> I'm a Dude<br>
<img src=\"images/girl.gif\" alt=\"Woman\" width=13 height=13 border=0> <input type=\"radio\" name=\"gender\" Value=\"Female\" checked> I'm a Girl";	
}

 

$x[gender] prints Female, however, it still selects Male -- any ideas?

Link to comment
Share on other sites

ah, I wasn't clear enough, My apologies. My select list is already made. I think i'm going to have to pull all the names out of the list though and put them either in an array or database though.

 

The country they are from is in the database, so I will just have to dynamically create the select list. I got ya, thanks :D

 

//Edit

Considering I am terrible with regex, any suggestions to get me started pulling them out? If each was set like this

<OPTION VALUE=\"Canada\">Canada</OPTION>

How would I pull out canada? (granted all of the countries change as the list progresses)

 

 

I dont understand what you are after here.  Why would you need regex to pull out Canada ?  Are they stored in the database this way ?

Link to comment
Share on other sites

ah, I wasn't clear enough, My apologies. My select list is already made. I think i'm going to have to pull all the names out of the list though and put them either in an array or database though.

 

The country they are from is in the database, so I will just have to dynamically create the select list. I got ya, thanks :D

 

//Edit

Considering I am terrible with regex, any suggestions to get me started pulling them out? If each was set like this

<OPTION VALUE=\"Canada\">Canada</OPTION>

How would I pull out canada? (granted all of the countries change as the list progresses)

 

 

I dont understand what you are after here.  Why would you need regex to pull out Canada ?  Are they stored in the database this way ?

 

 

The countries list is not stored in a database at all. I include it..

 

include("country_list.php");

 

 

I want to write a script to automate pulling all of the countries out so I dont have to copy n paste 300 countries one by one..

 

//Edit: Don't know why the gender thing wasnt working at first it is now.

 

 

Link to comment
Share on other sites

The only way I could see you do this is place the contents of the file into a variable, and then replace their country line with the selected version of the line.  Why aren't you using a database for this?

 

This is assuming the countries.php would just have a list like in your first post.

<?php
$countries = file_get_contents(countries.php);

//mysql select their country, comes out as "Canada" (str)
$userCountry = "Canada";
  $search = '<OPTION VALUE=\"' . $userCountry . '\">' . $userCountry . '</OPTION>';
  $replace = '<OPTION VALUE=\"' . $userCountry . '\" SELECTED>' . $userCountry . '</OPTION>';

str_replace($search, $replace, $countries);

//print list
echo $countries;

?>

 

not tested.

Link to comment
Share on other sites

country_list.php

 

print "
<SELECT NAME=\"location\" id=\"country_id\" SIZE=\"1\">
<OPTION VALUE=\"1\"> Choose One </OPTION>
<OPTION VALUE=\"United Kingdom\">United Kingdom</OPTION>
<OPTION VALUE=\"United States\">United States</OPTION>
<OPTION VALUE=\"Australia\">Australia</OPTION>
<OPTION VALUE=\"New Zealand\">New Zealand</OPTION>
<OPTION VALUE=\"Canada\">Canada</OPTION>
<OPTION VALUE=\"France\">France</OPTION>
<OPTION VALUE=\"South Africa\">South Africa</OPTION>
<OPTION VALUE=\"Afghanistan\">Afghanistan</OPTION>
<OPTION VALUE=\"Albania\">Albania</OPTION>
<OPTION VALUE=\"Algeria\">Algeria</OPTION>
<OPTION VALUE=\"American Samoa\">American Samoa</OPTION>
<OPTION VALUE=\"Andorra\">Andorra</OPTION>
<OPTION VALUE=\"Angola\">Angola</OPTION>
<OPTION VALUE=\"Anguilla\">Anguilla</OPTION>
<OPTION VALUE=\"Antarctica\">Antarctica</OPTION>
<OPTION VALUE=\"Antigua and Barbuda\">Antigua and Barbuda</OPTION>
<OPTION VALUE=\"Argentina\">Argentina</OPTION>
<OPTION VALUE=\"Armenia\">Armenia</OPTION>
<OPTION VALUE=\"Aruba\">Aruba</OPTION>
<OPTION VALUE=\"Austria\">Austria</OPTION>
<OPTION VALUE=\"Azerbaijan\">Azerbaijan</OPTION>
<OPTION VALUE=\"Bahamas\">Bahamas</OPTION>
<OPTION VALUE=\"Bahrain\">Bahrain</OPTION>
<OPTION VALUE=\"Bangladesh\">Bangladesh</OPTION>
<OPTION VALUE=\"Barbados\">Barbados</OPTION>
<OPTION VALUE=\"Belarus\">Belarus</OPTION>
<OPTION VALUE=\"Belgium\">Belgium</OPTION>
<OPTION VALUE=\"Belize\">Belize</OPTION>
<OPTION VALUE=\"Benin\">Benin</OPTION>
<OPTION VALUE=\"Bermuda\">Bermuda</OPTION>
<OPTION VALUE=\"Bhutan\">Bhutan</OPTION>
<OPTION VALUE=\"Bolivia\">Bolivia</OPTION>
<OPTION VALUE=\"Bosnia and Herzegowina\">Bosnia and Herzegowina</OPTION>
<OPTION VALUE=\"Botswana\">Botswana</OPTION>
<OPTION VALUE=\"Bouvet Island\">Bouvet Island</OPTION>
<OPTION VALUE=\"Brazil\">Brazil</OPTION>
<OPTION VALUE=\"British Indian Ocean Terr.\">British Indian Ocean Terr.</OPTION>
<OPTION VALUE=\"Brunei Darussalam\">Brunei Darussalam</OPTION>
<OPTION VALUE=\"Bulgaria\">Bulgaria</OPTION>
<OPTION VALUE=\"Burkina\">Burkina Faso</OPTION>
<OPTION VALUE=\"Burundi\">Burundi</OPTION>
<OPTION VALUE=\"Cambodia\">Cambodia</OPTION>
<OPTION VALUE=\"Cameroon\">Cameroon</OPTION>
<OPTION VALUE=\"Cape Verde\">Cape Verde</OPTION>
<OPTION VALUE=\"Cayman Islands\">Cayman Islands</OPTION>
<OPTION VALUE=\"Central African Republic\">Central African Republic</OPTION>
<OPTION VALUE=\"Chad\">Chad</OPTION>
<OPTION VALUE=\"Chile\">Chile</OPTION>
<OPTION VALUE=\"China\">China</OPTION>
<OPTION VALUE=\"Christmas Island\">Christmas Island</OPTION>
<OPTION VALUE=\"Cocos (Keeling) Islands\">Cocos (Keeling) Islands</OPTION>
<OPTION VALUE=\"Colombia\">Colombia</OPTION>
<OPTION VALUE=\"Comoros\">Comoros</OPTION>
<OPTION VALUE=\"Congo\">Congo</OPTION>
<OPTION VALUE=\"Cook Islands\">Cook Islands</OPTION>
<OPTION VALUE=\"Costa Rica\">Costa Rica</OPTION>
<OPTION VALUE=\"Cote d'Ivoire\">Cote d'Ivoire</OPTION>
<OPTION VALUE=\"Croatia (Hrvatska)\">Croatia (Hrvatska)</OPTION>
<OPTION VALUE=\"Cuba\">Cuba</OPTION>
<OPTION VALUE=\"Cyprus\">Cyprus</OPTION>
<OPTION VALUE=\"Czech Republic\">Czech Republic</OPTION>
<OPTION VALUE=\"Denmark\">Denmark</OPTION>
<OPTION VALUE=\"Djibouti\">Djibouti</OPTION>
<OPTION VALUE=\"Dominica\">Dominica</OPTION>
<OPTION VALUE=\"Dominican Republic\">Dominican Republic</OPTION>
<OPTION VALUE=\"East Timor\">East Timor</OPTION>
<OPTION VALUE=\"Ecuador\">Ecuador</OPTION>
<OPTION VALUE=\"Egypt\">Egypt</OPTION>
<OPTION VALUE=\"El Salvador\">El Salvador</OPTION>
<OPTION VALUE=\"Equatorial Guinea\">Equatorial Guinea</OPTION>
<OPTION VALUE=\"Eritrea\">Eritrea</OPTION>
<OPTION VALUE=\"Estonia\">Estonia</OPTION>
<OPTION VALUE=\"Ethiopia\">Ethiopia</OPTION>
<OPTION VALUE=\"Falkland Islands/Malvinas\">Falkland Islands/Malvinas</OPTION>
<OPTION VALUE=\"Faroe Islands\">Faroe Islands</OPTION>
<OPTION VALUE=\"Fiji\">Fiji</OPTION>
<OPTION VALUE=\"Finland\">Finland</OPTION>
<OPTION VALUE=\"France\">France</OPTION>
<OPTION VALUE=\"France, Metropolitan\">France, Metropolitan</OPTION>
<OPTION VALUE=\"French Guiana\">French Guiana</OPTION>
<OPTION VALUE=\"French Polynesia\">French Polynesia</OPTION>
<OPTION VALUE=\"French Southern Terr.\">French Southern Terr.</OPTION>
<OPTION VALUE=\"Gabon\">Gabon</OPTION>
<OPTION VALUE=\"Gambia\">Gambia</OPTION>
<OPTION VALUE=\"Georgia\">Georgia</OPTION>
<OPTION VALUE=\"Germany\">Germany</OPTION>
<OPTION VALUE=\"Ghana\">Ghana</OPTION>
<OPTION VALUE=\"Gibraltar\">Gibraltar</OPTION>
<OPTION VALUE=\"Greece\">Greece</OPTION>
<OPTION VALUE=\"Greenland\">Greenland</OPTION>
<OPTION VALUE=\"Grenada\">Grenada</OPTION>
<OPTION VALUE=\"Guadeloupe\">Guadeloupe</OPTION>
<OPTION VALUE=\"Guam\">Guam</OPTION>
<OPTION VALUE=\"Guatemala\">Guatemala</OPTION>
<OPTION VALUE=\"Guinea\">Guinea</OPTION>
<OPTION VALUE=\"Guinea-Bissau\">Guinea-Bissau</OPTION>
<OPTION VALUE=\"Guyana\">Guyana</OPTION>
<OPTION VALUE=\"Haiti\">Haiti</OPTION>
<OPTION VALUE=\"Heard & McDonald Is.\">Heard & McDonald Is.</OPTION>
<OPTION VALUE=\"Honduras\">Honduras</OPTION>
<OPTION VALUE=\"Hong Kong\">Hong Kong</OPTION>
<OPTION VALUE=\"Hungary\">Hungary</OPTION>
<OPTION VALUE=\"Iceland\">Iceland</OPTION>
<OPTION VALUE=\"India\">India</OPTION>
<OPTION VALUE=\"Indonesia\">Indonesia</OPTION>
<OPTION VALUE=\"Iran\">Iran</OPTION>
<OPTION VALUE=\"Iraq\">Iraq</OPTION>
<OPTION VALUE=\"Ireland\">Ireland</OPTION>
<OPTION VALUE=\"Israel\">Israel</OPTION>
<OPTION VALUE=\"Italy\">Italy</OPTION>
<OPTION VALUE=\"Jamaica\">Jamaica</OPTION>
<OPTION VALUE=\"Japan\">Japan</OPTION>
<OPTION VALUE=\"Jordan\">Jordan</OPTION>
<OPTION VALUE=\"Kazakhstan\">Kazakhstan</OPTION>
<OPTION VALUE=\"Kenya\">Kenya</OPTION>
<OPTION VALUE=\"Kiribati\">Kiribati</OPTION>
<OPTION VALUE=\"South Korea\">South Korea</OPTION>
<OPTION VALUE=\"Kuwait\">Kuwait</OPTION>
<OPTION VALUE=\"Kyrgyzstan\">Kyrgyzstan</OPTION>
<OPTION VALUE=\"Lao People's Dem. Rep.\">Lao People's Dem. Rep.</OPTION>
<OPTION VALUE=\"Latvia\">Latvia</OPTION>
<OPTION VALUE=\"Lebanon\">Lebanon</OPTION>
<OPTION VALUE=\"Lesotho\">Lesotho</OPTION>
<OPTION VALUE=\"Liberia\">Liberia</OPTION>
<OPTION VALUE=\"Libyan Arab Jamahiriya\">Libyan Arab Jamahiriya</OPTION>
<OPTION VALUE=\"Liechtenstein\">Liechtenstein</OPTION>
<OPTION VALUE=\"Lithuania\">Lithuania</OPTION>
<OPTION VALUE=\"Luxembourg\">Luxembourg</OPTION>
<OPTION VALUE=\"Macau\">Macau</OPTION>
<OPTION VALUE=\"Macedonia\">Macedonia</OPTION>
<OPTION VALUE=\"Madagascar\">Madagascar</OPTION>
<OPTION VALUE=\"Malawi\">Malawi</OPTION>
<OPTION VALUE=\"Malaysia\">Malaysia</OPTION>
<OPTION VALUE=\"Maldives\">Maldives</OPTION>
<OPTION VALUE=\"Mali\">Mali</OPTION>
<OPTION VALUE=\"Malta\">Malta</OPTION>
<OPTION VALUE=\"Marshall Islands\">Marshall Islands</OPTION>
<OPTION VALUE=\"Martinique\">Martinique</OPTION>
<OPTION VALUE=\"Mauritania\">Mauritania</OPTION>
<OPTION VALUE=\"Mauritius\">Mauritius</OPTION>
<OPTION VALUE=\"Mayotte\">Mayotte</OPTION>
<OPTION VALUE=\"Mexico\">Mexico</OPTION>
<OPTION VALUE=\"Micronesia\">Micronesia</OPTION>
<OPTION VALUE=\"Moldova\">Moldova</OPTION>
<OPTION VALUE=\"Monaco\">Monaco</OPTION>
<OPTION VALUE=\"Mongolia\">Mongolia</OPTION>
<OPTION VALUE=\"Montserrat\">Montserrat</OPTION>
<OPTION VALUE=\"Morocco\">Morocco</OPTION>
<OPTION VALUE=\"Mozambique\">Mozambique</OPTION>
<OPTION VALUE=\"Myanmar\">Myanmar</OPTION>
<OPTION VALUE=\"Namibia\">Namibia</OPTION>
<OPTION VALUE=\"Nauru\">Nauru</OPTION>
<OPTION VALUE=\"Nepal\">Nepal</OPTION>
<OPTION VALUE=\"Netherlands\">Netherlands</OPTION>
<OPTION VALUE=\"Netherlands Antilles\">Netherlands Antilles</OPTION>
<OPTION VALUE=\"New Caledonia\">New Caledonia</OPTION>
<OPTION VALUE=\"New Zealand\">New Zealand</OPTION>
<OPTION VALUE=\"Nicaragua\">Nicaragua</OPTION>
<OPTION VALUE=\"Niger\">Niger</OPTION>
<OPTION VALUE=\"Nigeria\">Nigeria</OPTION>
<OPTION VALUE=\"Niue\">Niue</OPTION>
<OPTION VALUE=\"Norfolk Island\">Norfolk Island</OPTION>
<OPTION VALUE=\"Northern Mariana Is.\">Northern Mariana Is.</OPTION>
<OPTION VALUE=\"Norway\">Norway</OPTION>
<OPTION VALUE=\"Oman\">Oman</OPTION>
<OPTION VALUE=\"Pakistan\">Pakistan</OPTION>
<OPTION VALUE=\"Palau\">Palau</OPTION>
<OPTION VALUE=\"Panama\">Panama</OPTION>
<OPTION VALUE=\"Papua New Guinea\">Papua New Guinea</OPTION>
<OPTION VALUE=\"Paraguay\">Paraguay</OPTION>
<OPTION VALUE=\"Peru\">Peru</OPTION>
<OPTION VALUE=\"Philippines\">Philippines</OPTION>
<OPTION VALUE=\"Pitcairn\">Pitcairn</OPTION>
<OPTION VALUE=\"Poland\">Poland</OPTION>
<OPTION VALUE=\"Portugal\">Portugal</OPTION>
<OPTION VALUE=\"Puerto Rico\">Puerto Rico</OPTION>
<OPTION VALUE=\"Qatar\">Qatar</OPTION>
<OPTION VALUE=\"Reunion\">Reunion</OPTION>
<OPTION VALUE=\"Romania\">Romania</OPTION>
<OPTION VALUE=\"Russian Federation\">Russian Federation</OPTION>
<OPTION VALUE=\"Rwanda\">Rwanda</OPTION>
<OPTION VALUE=\"Saint Kitts and Nevis\">Saint Kitts and Nevis</OPTION>
<OPTION VALUE=\"Saint Lucia\">Saint Lucia</OPTION>
<OPTION VALUE=\"Samoa\">Samoa</OPTION>
<OPTION VALUE=\"San Marino\">San Marino</OPTION>
<OPTION VALUE=\"Sao Tome & Principe\">Sao Tome & Principe</OPTION>
<OPTION VALUE=\"Saudi Arabia\">Saudi Arabia</OPTION>
<OPTION VALUE=\"Senegal\">Senegal</OPTION>
<OPTION VALUE=\"Seychelles\">Seychelles</OPTION>
<OPTION VALUE=\"Sierra Leone\">Sierra Leone</OPTION>
<OPTION VALUE=\"Singapore\">Singapore</OPTION>
<OPTION VALUE=\"Slovakia\">Slovakia </OPTION>
<OPTION VALUE=\"Slovenia\">Slovenia</OPTION>
<OPTION VALUE=\"Solomon Islands\">Solomon Islands</OPTION>
<OPTION VALUE=\"Somalia\">Somalia</OPTION>
<OPTION VALUE=\"South Africa\">South Africa</OPTION>
<OPTION VALUE=\"Spain\">Spain</OPTION>
<OPTION VALUE=\"Sri Lanka\">Sri Lanka</OPTION>
<OPTION VALUE=\"St. Helena\">St. Helena</OPTION>
<OPTION VALUE=\"Sudan\">Sudan</OPTION>
<OPTION VALUE=\"Suriname\">Suriname</OPTION>
<OPTION VALUE=\"Swaziland\">Swaziland</OPTION>
<OPTION VALUE=\"Sweden\">Sweden</OPTION>
<OPTION VALUE=\"Switzerland\">Switzerland</OPTION>
<OPTION VALUE=\"Syrian Arab Republic\">Syrian Arab Republic</OPTION>
<OPTION VALUE=\"Taiwan\">Taiwan</OPTION>
<OPTION VALUE=\"Tajikistan\">Tajikistan</OPTION>
<OPTION VALUE=\"Tanzania\">Tanzania</OPTION>
<OPTION VALUE=\"Thailand\">Thailand</OPTION>
<OPTION VALUE=\"Togo\">Togo</OPTION>
<OPTION VALUE=\"Tokelau\">Tokelau</OPTION>
<OPTION VALUE=\"Tonga\">Tonga</OPTION>
<OPTION VALUE=\"Trinidad and Tobago\">Trinidad and Tobago</OPTION>
<OPTION VALUE=\"Tunisia\">Tunisia</OPTION>
<OPTION VALUE=\"Turkey\">Turkey</OPTION>
<OPTION VALUE=\"Turkmenistan\">Turkmenistan</OPTION>
<OPTION VALUE=\"Tuvalu\">Tuvalu</OPTION>
<OPTION VALUE=\"Uganda\">Uganda</OPTION>
<OPTION VALUE=\"Ukraine\">Ukraine</OPTION>
<OPTION VALUE=\"United Arab Emirates\">United Arab Emirates</OPTION>
<OPTION VALUE=\"United Kingdom\">United Kingdom</OPTION>
<OPTION VALUE=\"U.S. Minor Outlying Is.\">U.S. Minor Outlying Is.</OPTION>
<OPTION VALUE=\"Uruguay\">Uruguay</OPTION>
<OPTION VALUE=\"Uzbekistan\">Uzbekistan</OPTION>
<OPTION VALUE=\"Vanuatu\">Vanuatu</OPTION>
<OPTION VALUE=\"Vatican\">Vatican</OPTION>
<OPTION VALUE=\"Venezuela\">Venezuela</OPTION>
<OPTION VALUE=\"Viet Nam\">Viet Nam</OPTION>
<OPTION VALUE=\"Virgin Islands (British)\">Virgin Islands (British)</OPTION>
<OPTION VALUE=\"Virgin Islands (U.S.)\">Virgin Islands (U.S.)</OPTION>
<OPTION VALUE=\"Western Sahara\">Western Sahara</OPTION>
<OPTION VALUE=\"Yemen\">Yemen</OPTION>
<OPTION VALUE=\"Yugoslavia\">Yugoslavia</OPTION>
<OPTION VALUE=\"Zaire\">Zaire</OPTION>
<OPTION VALUE=\"Zambia\">Zambia</OPTION>
<OPTION VALUE=\"Zimbabwe\">Zimbabwe</OPTION>
</SELECT>

 

The reason I am not using a DB for the countries is b/c I found a country list and just modified it to suit my needs. All I need to do is pull the values and print em out so I can put them in an array

Link to comment
Share on other sites

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.