Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/50561-editing-a-users-profile/
Share on other sites

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.

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.

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";

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.