takn25 Posted March 7, 2011 Share Posted March 7, 2011 Hi, what I want to figure out is for instance a person has registered with their country e.g. England. Now if I echo the country in a select box giving the person an option to change their country and Showing the person which currently they have selected already. The select box shows two England`s to select from. Could some one tell me how can I have one of each country and echo their already selected country from the database. I don't know how to explain any better what I am after but just basically there are two Englands showing one which is already selected (echoing from the mysql database) and one is already in the select box. Any help is much appreciated thank you. Link to comment https://forums.phpfreaks.com/topic/229853-help-with-a-select-box/ Share on other sites More sharing options...
Vermillion Posted March 7, 2011 Share Posted March 7, 2011 It would be helpful to see the code you are using for echoing your countries. We will figure something out from there. Link to comment https://forums.phpfreaks.com/topic/229853-help-with-a-select-box/#findComment-1183885 Share on other sites More sharing options...
takn25 Posted March 7, 2011 Author Share Posted March 7, 2011 Basically I am just getting the users country from mysql database and echoing it in the select boxes Value I dont really know another way of doing it. This is my code <select id="country" name="country"> <option value="-2" selected="selected"><?php echo $country; ?></option> <option value="-1">Select One:</option> <option value="Antarctica" >Antarctica</option> <option value="Antigua and Barbuda" >Antigua and Barbuda</option> <option value="Argentinar" >Argentina</option> <option value="Australia" >Australia</option> <option value="England" >England</option> <option value="Spain" >Spain</option> </select> Link to comment https://forums.phpfreaks.com/topic/229853-help-with-a-select-box/#findComment-1183913 Share on other sites More sharing options...
flolam Posted March 7, 2011 Share Posted March 7, 2011 what is the problem with this code? Link to comment https://forums.phpfreaks.com/topic/229853-help-with-a-select-box/#findComment-1183925 Share on other sites More sharing options...
takn25 Posted March 7, 2011 Author Share Posted March 7, 2011 The problem is for example I am echoing the country and the country is for example England. When I view the select box there will be 2 England`s in the select box one from the mysql database and one from the html code. I only want one there to be one. Meaning if the country is England in the database I want it to select England from the html code rather than there be 2 one from mysql and one from html code. Link to comment https://forums.phpfreaks.com/topic/229853-help-with-a-select-box/#findComment-1183940 Share on other sites More sharing options...
flolam Posted March 7, 2011 Share Posted March 7, 2011 This will make the option from the db preselected: <select> <?php $countries = array("Antarctica", "Australia", "England"); foreach ($countries as $c) { $output = '<option value="'.$c.'"'; if ($c == $country) { //$country is the one from the database $output .= ' selected="selected"'; } $output .= " >{$c}</option>"; echo $ouput; } ?> </select> Link to comment https://forums.phpfreaks.com/topic/229853-help-with-a-select-box/#findComment-1183944 Share on other sites More sharing options...
takn25 Posted March 7, 2011 Author Share Posted March 7, 2011 Hi, thanks for the help but it doesnt seem to work I am not the best at PHP so its kinda hard for me to point out if there is an error could you kindly check thanks the Select box doesnt seem to contain anything. Link to comment https://forums.phpfreaks.com/topic/229853-help-with-a-select-box/#findComment-1183967 Share on other sites More sharing options...
flolam Posted March 7, 2011 Share Posted March 7, 2011 Sorry, my code contains a mistake, it should be echo $output; of course Link to comment https://forums.phpfreaks.com/topic/229853-help-with-a-select-box/#findComment-1184020 Share on other sites More sharing options...
takn25 Posted March 8, 2011 Author Share Posted March 8, 2011 works super thanks! Link to comment https://forums.phpfreaks.com/topic/229853-help-with-a-select-box/#findComment-1184298 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.