Jump to content

Editing a users profile


graham23s

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.