Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: combo box

  1. #1
    New Coder
    Join Date
    Feb 2007
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts

    combo box

    I am allowing users insert data into a database using a form.
    I want one of the fields "department" to display all existing departments in the database in a combo box for users to choose from and also have a field in the combo box for them to add a new department.
    Is there a way for me to do this in using php?
    I am using php to access the database

    Thanks

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    HTML forms don't have an option for a true combo box (one with a drop-down list and a field to enter a value not in the list) but you can approximate this behavior with two separate input fields (a select input and a text input).

    Do a google on "html combo box" for implementation ideas. (here's one)

    As for the PHP side of things, you'll need to populate the list box based on the results of a query when the page loads. The query would be something like "select distinct(dept_value) from mytable".

  • #3
    New Coder
    Join Date
    Feb 2007
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Basically if i could put the values from the database in an array and then use the array to populate the combo.
    But i just don't know how to do that.

    Here is my php code and below is the html code.
    My biggest problem is taking the php array and using it with the html

    Any help would be much appreciated.
    This is driving me mad!

    <?php
    $con = mysql_connect(server,username,password);
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    echo "cannot connect";
    }

    mysql_select_db(db,$con);


    $result = mysql_query("SELECT * FROM Prods") or die(mysql_error());

    $var = 0
    while($row = @mysql_fetch_array($result))
    {
    $array[$var]=$row['Department'];
    $var = $var + 1;
    }

    mysql_close($con);
    ?>

    <select name="department" size="1">
    <option value= $var></option>
    </select></td>

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Let's break it down into the two processes: Build the array, and Build the list box values.

    Build the array out of a query using the "distinct" keyword.
    PHP Code:
    $query "SELECT DISTINCT(Department) FROM Prods";
    $result mysql_query($query);
    for (
    $i 0$i mysql_num_rows($result); $i++) {
        
    $deptArray[$i] = mysql_result($result0);

    All unique departments are now stored in $deptArray. To build the list box from this:
    PHP Code:
    <select name="deptSelect">
    <?php
    foreach ($deptArray as $val) {
        print 
    "<option value=\"$val\">$val</option>\n";
    }
    ?>
    </select>

  • #5
    New Coder
    Join Date
    Feb 2007
    Posts
    92
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you so much that works a treat.

    Two questions -

    the distinct is not working.any other reason why the same value in the database is appearing twice in the combo?

    is it possible to have a field in the combo for adding a new value to the database or not?

    thanks again for all the help

  • #6
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    the distinct is not working.any other reason why the same value in the database is appearing twice in the combo?
    You'd have to post examples of your data and query, because "distinct" does in fact work for everyone else in the free world. More than likely you have different capitalization, white space (\n, \t, \r, etc) or other dirty data that is listing more unique values than you expect.

    is it possible to have a field in the combo for adding a new value to the database or not?
    So did you google it like I suggested? In a few seconds I found three different websites with methods of doing that.


  •  

    Posting Permissions

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