SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict peterb's Avatar
    Join Date
    Jun 2003
    Location
    Athens, Greece
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question repopulating drop-down lists

    Hi everyone,

    I am having a problem with repopulating drop-down lists.
    The results of this simply display the list without the correct info.

    <?PHP
    // start query
    ///////////////////////////////////////////////////////////////////
    test_db_connect()
    or die ("Cannot connect to server at this time....");

    $count = mysql_query("select state, name, year
    from testdb WHERE (name = '$name' and year = '$year') ")
    or exit ();

    $row = mysql_fetch_array($count);

    // fill variables from db record
    ////////////////////////////////////////////////////////////////
    $state = $row["state"];

    ?>

    <select name=state size=1>
    <option selected value="">click here
    <option value=ZZ>Outside U.S.
    <option value=AL>AL Alabama
    <option value=AK>AK Alaska
    <option value=AZ>AZ Arizona
    </option>
    </select>

    Shouldn't the variable $state reload the data and show the user their selection in the list?

    TIA

  2. #2
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Huh? You get some data and display some markup, what is supposed to happen? Is this the whole file?

  3. #3
    SitePoint Addict peterb's Avatar
    Join Date
    Jun 2003
    Location
    Athens, Greece
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think that the question is fairly clear and that including a whole file of html is not necessary.

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2003
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    bah. im also confused.

  5. #5
    SitePoint Addict peterb's Avatar
    Join Date
    Jun 2003
    Location
    Athens, Greece
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, what I am trying to do is create an edit screen portion to what I am working on.
    The data entry portion basically has this drop-down list which records the 2-letter code for the state in the database.
    When I try to edit the data though, the state variable is not being re-populated with the data from the database.

    Any clues, suggestions, code snippets appreciated.
    Thanks.

  6. #6
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi.
    Here's a suggestion
    PHP Code:
    test_db_connect() or die ('Cannot connect to server at this time..' . mysql_error());

    $sql = "SELECT state, name, year
              FROM testdb 
             WHERE name = '$name' 
               AND year = '$year'";
    $result = mysql_query($sql) or die(mysql_error());

    $options = '';
    while($row = mysql_fetch_array($result)) {
        $state = $row["state"];
        $name = $row['name'];
        $options .= "<option value=\"$state\">$state $name</option>\n";    
    }   
    ?>

    <select name=state size=1>
    <option selected value="">click here</option>
    <?php echo $opstions?>
    </select>
    I don't know if I've done what you're after, but I hope so.
    I've assumed that the name column in the db is the name of the state, and the state column is the two letter state code.

    -Helge

    A firendly tip: When people ask for more information it's clever to comply with their wish. For instance you can try to say the same thing with different words. If you have problems rewriting your question tell them so in a polite way.
    It's kind of not your call to decide if the problem is well enough described
    Last edited by Helge; Nov 3, 2003 at 08:53. Reason: Typo

  7. #7
    PHP manual bot bronze trophy Gaheris's Avatar
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,195
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you Helge, well said.

    The problem was that you (peterb) were talking about populating dropdown lists, but then you posted code which hadn't to do a lot with it except that there was some HTML markup. To get me into a completly <confused /> state you asked your last question which I still don't quite get.

    Anyway, Helge most likely answered your question in showing you how to populate dropdopwn lists with data from a MySQL database.

  8. #8
    SitePoint Addict peterb's Avatar
    Join Date
    Jun 2003
    Location
    Athens, Greece
    Posts
    283
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My apologies to all. I am very sorry and am grateful for the help. It's no excuse but trying to learn this on my own creates enormous frustration.

    I will analyze the code and try my best to make it work.
    Peter

  9. #9
    SitePoint Wizard
    Join Date
    Oct 2001
    Location
    Tucson, Arizona
    Posts
    1,858
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, so I think you want to have the value stored in the DB be the value that's selected upon displaying the drop-down, right? Why not make a function to do the work for you? This code should help you out some.


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
  •