retain value in multi-select box on edit page

Posted on 2005-04-28
Last Modified: 2013-12-12
I have a multi-select box on my edit page which doesn't retain the values that are in the database when I edit it.  the selection is stored as an array in the db.  so a subject could have an array of 9, 10, 25 and so forth...just means that it belongs to more than one topic.

here's the code I have so far:

<SELECT name='id[]' multiple size=5>
 <?php                $tQuery = mysql_query("SELECT id, topic FROM topics ORDER BY topic");
                      while ($sRow = mysql_fetch_row($tQuery))
if( $id == $sRow[0] ) {
print ("<OPTION SELECTED value='$sRow[0]'>$sRow[1]</OPTION>");
} else {
print ("<OPTION value='$sRow[0]'>$sRow[1]</OPTION>");
} // end of conditions
} // end of while loop
Question by:lienny
    LVL 6

    Expert Comment

    Hi lienny,

    ok, let's assume that you have selected values in array $selectedIDs

        $selectedIDs = array(9, 10, 25);
    <select name='id[]' multiple size=5>
        $result = mysql_query("SELECT id, topic FROM topics ORDER BY topic");
        while ($row = mysql_fetch_row($result)) {
            if (in_array($row[0], $selectedIDs)) {
                echo ("<OPTION SELECTED value='$row[0]'>$row[1]</OPTION>");
            } else {
                print ("<OPTION value='$row[0]'>$row[1]</OPTION>");


    German Rumm.

    Author Comment

    i would like to do this dynamically.
    LVL 6

    Accepted Solution


    Then you need to create $selectedIDs array dynamically.

    For example, you have a table tblUserGroups:

    UserID      GroupID

    The data in it looks like this:

    UserID         GroupID
    username    9
    username    10
    username    25

        $sql = 'SELECT GroupID FROM tblUserGroups WHERE UserID = "username"';
        $result = mysql_query($sql);
        $selectedIDs = array();

        while ($row = mysql_fetch_row($result)) {
            $selectedIDs[] = $row[0];

        //    here you go, dynamically populated from db $selectedIDs array.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    A colleague recently asked me about how to give his client a small part of the web site that could be completely under the client's control.  Since I have done this sort of thing before to add emergency banners to a web site, I decided I would creat…
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now