graham23s Posted May 8, 2007 Share Posted May 8, 2007 Hi Guys, on my site the users can edit they're profile, basically a page with drop down boxes, they can change country etc, what i was wanting to do is when they goto the edit page to edit say they're country for example , the country they signed up with is pre-selected (if someone signed up with Scotland) Scotland is automatically shown first. heres my basic code for one box: <select name=\"country\"> <option value=\"$country\" selected> $country <option value=\"UK\">UK</option> <option value=\"USA\">USA</option> <option value=\"Scotland\">Scotland</option> </select> needless to say it doesn't work, any help would be great:) Graham Quote Link to comment https://forums.phpfreaks.com/topic/50561-editing-a-users-profile/ Share on other sites More sharing options...
Caesar Posted May 8, 2007 Share Posted May 8, 2007 Try something like.... <?php while ($country = $db->fetch_row($countries)) { $selected = ''; if($userinfo[country] == $country[1]) { $selected = ' selected'; } echo"<option value=\"$country[1]\"$seected>$country[1]</option>"; } ?> That's asuming that $userinfo is an array with all your user's info...and that you have an array $countries that you pulled from your db, with all the country names. Quote Link to comment https://forums.phpfreaks.com/topic/50561-editing-a-users-profile/#findComment-248510 Share on other sites More sharing options...
per1os Posted May 8, 2007 Share Posted May 8, 2007 Are the countries stored in a DB? If not that would make it easy. For the sake of lack of code I will store them in an array. <?php $countries = array("USA", "UK", "Scotland"); $select = '<select name="country">'; foreach ($countries as $arrCountry) { $selected = ""; if ($arrCountry == $country) { $selected = " selected"; } $select .= '<option value="' . $arrCountry . '" ' . $selected . '>' . $arrCountry . '</option>'; } $select .= "</select>"; ?> That would work. Quote Link to comment https://forums.phpfreaks.com/topic/50561-editing-a-users-profile/#findComment-248516 Share on other sites More sharing options...
marcus Posted May 8, 2007 Share Posted May 8, 2007 Just do something, like: $sql = "SELECT * FROM `table` WHERE `id` =#"; //# to the variable for their ID $res = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_assoc($res); echo "<select name=\"country\">\n"; $sql2 = "SELECT * FROM `countries`"; $res2 = mysql_query($sql2) or die(mysql_error()); if(mysql_num_rows($res2) > 0){ while($row2 = mysql_fetch_assoc($res2){ if($row[country] == $row2[country]){ $var = " SELECTED"; }else { $var = ""; } echo "<option value=\"$row2[country]\"$var>$row[country]</option>\n"; } }else { //your own countries } echo "</select>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/50561-editing-a-users-profile/#findComment-248517 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.