Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Access Database - Drop Down Menu

Options
  • 07-05-2007 10:19pm
    #1
    Registered Users Posts: 1,465 ✭✭✭


    Just about to start pulling my hair out here. I need this done for college tomorrow. All I want is a drop down menu that lists the contents of a column in an MS Access table. Pretty straight forward, no? This is what I have so far. I have a table called employee and 2 columns named id_no and name:

    <?php
    $connection = odbc_connect('company_db','', '') or die('Could not Connect to ODBC Database!');
    $sql= 'SELECT * FROM employee';
    $results = odbc_exec($connection, $sql);
    while (odbc_fetch_row($results))
    {
    $id=$results["id_no"];
    $name=$results["name"];
    print"<option value='$id'>$name</option>"; }
    ?>
    <select name="name">
    <option value=0>Choose
    <?=$name?>
    </select>

    Any ideas?


Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    And what, pray tell, is the problem you're having?


  • Registered Users Posts: 1,465 ✭✭✭TheBigLebowski


    An empty drop down menu, that only says "choose"...


  • Registered Users Posts: 7,541 ✭✭✭irlrobins


    Wild stab here but is it because the "print"<option value='$id'>$name</option>";" is occurring before the <select> tag and not within it?


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Yep, that's it! Here's how it should be:

    [php]

    <?php
    $connection = odbc_connect('company_db','', '') or die('Could not Connect to ODBC Database!');
    $sql= 'SELECT * FROM employee';
    $results = odbc_exec($connection, $sql);
    ?>

    <select name="name">
    <option value=0>Choose</option>

    <?
    while (odbc_fetch_row($results))
    {
    $id=$results["id_no"];
    $name=$results["name"];
    echo '<option value='.$id.'>'.$name.'</option>';
    }
    ?>

    </select>

    [/php]


  • Registered Users Posts: 1,465 ✭✭✭TheBigLebowski


    Thanks for the replies. Good stuff. I know I'm pretty close now because the number of options in my drop down menu matches the number of employees on the database so my while loop is working but the part where it puts the values into the options isn't. Unfortunately the options are blank. Any idea where I'm going wrong.


  • Advertisement
  • Closed Accounts Posts: 1,200 ✭✭✭louie


    show us what you have.
    Also make sure the $id name coming from the database is "id_no" or whatever value you have in the code, like:
    [php]
    <?
    while (odbc_fetch_row($results))
    {
    $id=$results["id_no"];//this line here make sure the id_no is the name of the id field in the database
    $name=$results["name"];
    echo '<option value="'.$id.'">'.$name.'</option>';
    }
    ?>

    [/php]


  • Registered Users Posts: 1,465 ✭✭✭TheBigLebowski


    Thanks for the help. I asked my lecturer and I was going wrong with the lines:

    $id=$results["id_no"];
    $name=$results["name"];

    It should be:

    $name = odbc_result($results, 'name');
    $id = odbc_result($results, 'id_no');


    Pretty simple but enough to drive me to the edge of my sanity...


Advertisement