#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    77
    Rep Power
    15
    I have a database of contacts setup... one of the options is for new people to add their own information to the database. So some of the fields (such as Department) are added via a drop down box of values. However, users also need to be able to edit their information at later dates... When they do edit their information, there current record is shown in editable text form elements. The problem I am having is: how can I make it so that when the users edit the information, the drop down box is already at the correct option. I have no problem with the stardard text boxes, its just the drop down boxes that are giving me grief. Please help me someone! Thanks

    Dave
  2. #2
  3. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15
    you can achive this very easily ,

    i usually do like this.

    i create a seperate table for Department.and save all my department names in that.

    when i want to give the modify option to the user.it should populate all these values to the drop down menu.the value he already selected will be the selected value.

    here is an example,

    <?php

    $result=mysql_query("select name,age,dept_name from usertable where username='shiju',$con);
    //query for displaying your user information.

    $row=mysql_fetch_array($result);

    $result1=mysql_query("select dept_name from depertament",$con);

    //Query for populating all the department name to the drop down menu.


    ?>

    <select name="dept">

    <?

    do{
    if($sec_query_row["dept_name"]!= $row["dept_name"]){
    echo "<option value=".$sec_query_row["dept_name"].">".$sec_query_row["dept_name"]."</option>";

    // if dept name is already not selected then simply populate values to the drop down(ie, values that is not selected).

    }else{

    //else if it is selected then populate to the drop down menu and make it as selected.

    echo "<option value=".$row["dept_name"]." selected>".$row["dept_name"]."</option>";

    }
    }while($sec_query_row=mysql_fetch_array($result1));
    // Rotate the loop till popualting all the values to the drop down.

    ?>
    </select>


    #######-- This will solve your problem.i normally do like this. But you can save all the department names in an array and do same kind of action.

    #################-------#################
    One advantage using table is that you can update that table any time and that will populate value to the drop down menu insetead of changing the array value in your script.

    ################--------

    i hope you have understood the logic.if any problem tell me here!!


    Good Luck!!


    ------------------
    SR -
    shiju.dreamcenter.net

    "The fear of the LORD is the beginning of knowledge..."

    [This message has been edited by Shiju Rajan (edited May 31, 2000).]
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    77
    Rep Power
    15
    Thanks for the help, I have it working, it is displaying items from my database in a drop down box, however I changed it to use the current table. I don't want to start using multiple tables because then it may complicate what is supposed to be a relatively simple application.

    One problem I am having though, is items are appearing more than once. Say there are 2 people in the IT department, then my drop down box is having two items. Also the current item is not selected!! Can you help me with this?? Thanks in advance.

    Dave
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    77
    Rep Power
    15
    Actually on second thoughts, I did add the departments to another table. That part works well. The only problem I am having at the moment is that my desired item is not selected. I don't think the script actually specifies it... however I am unsure as I am only new to php. Can you help?? Here is the script that I modified (i am now looking for location). I have a new table setup called location.

    <?php

    $result1=mysql_query("select location from location",$db);

    //Query for populating all the department name to the drop down menu.
    ?>

    <select name="location">
    <?php

    do{
    if($sec_query_row["location"]!= $row["location"]){
    echo "<option value=".$sec_query_row["location"].">".$sec_query_row["location"]."</option>";

    // if dept name is already not selected then simply populate values to the drop down(ie, values that is not selected).

    }

    else

    {

    //else if it is selected then populate to the drop down menu and make it as selected.

    echo "<option value=".$row["location"]." selected>".$row["location"]."</option>";

    }
    } while($sec_query_row=mysql_fetch_array($result1));
    // Rotate the loop till popualting all the values to the drop down.

    // End drop box section
    ?>
    </select>

    Note: I don't have the initial select statement that you specified as the array is already passed to the page to begin with. So there was no need to search for a users details.

    Thanks in advance.

    Dave
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    77
    Rep Power
    15
    Cancel those last two posts... however my latest problem is that some of the items in the table have space in them. eg "District Manager". When i select these one from the box and click update... it only adds the first part ("District"). Therefore, next time you view the persons records nothing comes up as it doesn't match the drop-down box options. How do I pass the value including any spaces.

    Oh, I fixed the problem because I had it call $row[location] when it should have been $myrow[location]. Thanks again for your help.

    Dave
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    77
    Rep Power
    15
    Cancel the final one as well , I was a bit too quick off the mark. After a couple of hours or trying different things, I finally worked out that I needed to format the results in printf's rather than print or echo. That way it retains the formatting. Thank you very much for your help though, it was invaluable.

    Regards

    Dave
  12. #7
  13. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15

    Welcome Mr.Dave!!



    ------------------
    SR -
    shiju.dreamcenter.net

    "The fear of the LORD is the beginning of knowledge..."

Similar Threads

  1. Passing drop down variable
    By sylunt1 in forum PHP Development
    Replies: 1
    Last Post: February 16th, 2004, 03:15 AM
  2. displaying arrays in a drop down box
    By LOD_Dave in forum PHP Development
    Replies: 4
    Last Post: February 11th, 2004, 05:05 PM
  3. MySQL output to table using phpBB
    By CCCMTech in forum PHP Development
    Replies: 0
    Last Post: February 2nd, 2004, 03:43 PM
  4. retrieving data from a drop down box for modifying
    By swordfishy in forum PHP Development
    Replies: 0
    Last Post: January 18th, 2004, 09:07 PM
  5. Zip code validation
    By krwyth in forum MySQL Help
    Replies: 2
    Last Post: January 5th, 2004, 02:29 PM

IMN logo majestic logo threadwatch logo seochat tools logo