SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru Angry Coder's Avatar
    Join Date
    May 2002
    Location
    Canada
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How can I have the user seleced option selected in a select-one list?!

    Hello,

    I have a country list with more than 100 country, so I want when a user try to update his profile see the previously selected country selected!

    It's NOT logic to create more than 100 variable on of them has the value " selected"!!!

    So what is the proper way to do that?!

    Any help would be appreciated.
    Why It Doesn't Work?!

  2. #2
    Free your mind Toly's Avatar
    Join Date
    Sep 2001
    Location
    Panama
    Posts
    2,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could do the following:
    PHP Code:
    <select size="1" name="country_id">
    <?php
    // Retrieve user's country ID
    $sql "SELECT country_id FROM users WHERE ID = '$id'";
    $done mysql_query($sql);
    if (!
    $done) {
    echo(
    "Error" );
    exit;
    }
    $country_id mysql_result($done,0,"country_id" );

    // retrieve countries
    $sql "SELECT country_id, country_name FROM countries ORDER BY country_name";
    $done mysql_query($sql);
    if (!
    $done) {
    echo(
    "Error" ); 
    exit;
    }
    while (
    $data mysql_fetch_array($done)) {
    $cid $data["country_id"];
    $cname $data["country_name"];
    if (
    $cid == $country_id) {
    echo(
    "<option value=\"$cid\" selected>$cname\n" );
    } else {
    echo(
    "<option value=\"$cid\">$cname\n" );
    }
    }
    ?>
    </select>
    Community Guidelines | Community FAQ

    "He that is kind is free, though he is a slave;
    he that is evil is a slave, though he be a king." - St. Augustine

  3. #3
    SitePoint Guru Angry Coder's Avatar
    Join Date
    May 2002
    Location
    Canada
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't want to store the country list in the database [img]images/smilies/frown.gif[/img]

    I have so many lists!

    Is there any other way?
    Why It Doesn't Work?!

  4. #4
    Free your mind Toly's Avatar
    Join Date
    Sep 2001
    Location
    Panama
    Posts
    2,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry. I made the assumption that you did have them stored in a database.

    I don't know any other way.
    Community Guidelines | Community FAQ

    "He that is kind is free, though he is a slave;
    he that is evil is a slave, though he be a king." - St. Augustine

  5. #5
    SitePoint Guru Angry Coder's Avatar
    Join Date
    May 2002
    Location
    Canada
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks anyway...

    Where is SP experts?
    Why It Doesn't Work?!

  6. #6
    HI silver trophy Silverado4x4's Avatar
    Join Date
    Jan 2001
    Location
    USA
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the user has submitted the form (or you're pulling the previously selected input from a database), just have it selected at the top of the list.

    Say the list is:

    Canada
    Germany
    USA
    Switzerland

    And they select USA, have the form display like this:

    USA SELECTED
    Canada
    Germany
    USA
    Switzerland

    Hopefully this makes sense.

  7. #7
    Free your mind Toly's Avatar
    Join Date
    Sep 2001
    Location
    Panama
    Posts
    2,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That would be the solution. I remember I used it like this 3 years ago on a website but now I just store the countries in a database.
    Community Guidelines | Community FAQ

    "He that is kind is free, though he is a slave;
    he that is evil is a slave, though he be a king." - St. Augustine

  8. #8
    SitePoint Evangelist Daijoubu's Avatar
    Join Date
    Oct 2002
    Location
    Canada QC
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could just use a str_replace to adds in the selected

  9. #9
    SitePoint Guru Angry Coder's Avatar
    Join Date
    May 2002
    Location
    Canada
    Posts
    599
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Silverado4x4,

    That's a great solution! I will try that.

    Many thanks to all...
    Why It Doesn't Work?!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •