SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    add a 'Selected' to a select-box from a database

    Oh well...

    here's another problem.
    I have a database with employees... three of the tables look like this:

    employee (UserID, SurName, FirstName)
    works_as (UserID, FunctionID)
    function (FunctionID, FunctionName)

    An employee can have one or more (!!) functions.
    I want to show them in a <select>-options box, so I can edit the function easily.

    I've written the following code, which works partly. It does show all the functions available in the database, but it does not select the ones that the employee has.

    PHP Code:
            $sql     "SELECT * FROM function ORDER BY FunctionName";
            
    $result mysql_query($sql);
            while (
    $row mysql_fetch_array($result)) {
                   
    extract($row);
                   
                   
    $funcCheck "SELECT * FROM works_as WHERE UserID = $UserID AND FunctionID = $FunctionID";
                   
    $funcNum   mysql_query($funcCheck);
                   
                   if(
    mysql_num_rows($funcNum) > 0) { $select 'selected';}
                   else { 
    $select '';}
                   
                   print 
    "<option value='$FunctieID$select>$FunctionName</option>";
            } 
    So the part from $funcCheck to the end of the else-statement doesn't function properly...

    Does anyone know what I'm doing wrong?
    Please advise!

  2. #2
    SitePoint Member
    Join Date
    Mar 2001
    Location
    Idaho
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you have a typo...
    print "<option value='$FunctieID' $select>$FunctionName</option>";

    print "<option value='$FunctionID' $select>$FunctionName</option>";
    and you aren't creating the var $FunctionName

    Sample of 1 I've done, might have to change row #'s ...
    mysql_connect($DBhost,$DBuser,$DBPass) or die("Unable to connect to database");
    mysql_select_db("$DBName") or die("Unable to select database $DBName");
    $result = mysql_query( "SELECT * from works_as WHERE UserID = $UserID AND FunctionID = $FunctionID" or die ("Screwed Up: " . mysql_error());
    print "<form method="post' action='any.php' name='Function'><select name='function'>";
    while ($row = mysql_fetch_row($result))
    {
    echo "<option value='function.php?function=$row[2]'>$row[2]</option>";

    Terry
    Last edited by tilt; Jun 6, 2001 at 14:36.

  3. #3
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The way I use which looks a wee bit simpler is this:

    PHP Code:
    $sql mysql_query ("SELECT stuff etc");

        
    while (
    $row mysql_fetch_array($sql)) {
        
    extract ($row);
        echo 
    "<option value=\"$sid\"";

    // $tempsid is the $sid I already had, so that it isn't replaced
    // by the sid it is processing

        
    if ( $sid == $tempsid) {
            echo 
    ' selected';
        }

    echo 
    ">$name</option>";
    }
    ?> 
    I think the above way is slightly easier as it doesn't use any arrays or anything.

    Just put in your SQL query and your variables etc

  4. #4
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're right... that does work.
    Even when I have multiple lines selected.
    I have another question, though...

    If I print the select-box, how do I get the first option to be at the top of the list. (The size is about 5, but it has 25 options in it)

  5. #5
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you say first option, I'm not quite sure what you mean. If you give us some fake data, and tell us how you would like it ordered. I'm thinking that you want the items in the select menu to be ordered by something in them.

    So like:
    <select name="blah">
    <option value="" selected>This will be the selected one</option>
    <option value="">This will be the next one</option>
    <option value="">This will be the second one</option>
    <option value="">And the third</option>
    </select>

    And each of the next one, second one, and third will be ordered by a particular aspect?

    This is getting a little more complex, as you would have to pull the data out, and then extract the selected data, and then sort the rest of the existing data and print it out.

  6. #6
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hey Timnz,

    sorry, my question wasn't very clear, was it?
    What I meant is...

    If I have the following code:
    <select name='blah' size='3' multiple>
    <option value='1'>1</option>
    <option value='2'>2</option>
    <option value='3'>3</option>
    <option value='4'>4</option>
    <option value='5'>5</option>
    <option value='6' selected>6</option>
    <option value='7'>7</option>
    <option value='8'>8</option>
    </select>

    Now, as you can see: Option 6 has been selected (the whole php-script has run thru the database, and found the option 6 has to be selected)
    As you will notice... the size of this <select> is 3 (this is just an example, the real one has at least 25 options). So, 6 won't show when the <select> is printed.
    What I meant with my last question is: what code do I use so, in this case, option 6 is visible (or at the top) in the <select>?


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
  •