paruby Posted June 10, 2009 Share Posted June 10, 2009 Hello - I found the following code to create combo and list boxes, and it works great :: function createComboBox($identifier, $pairs, $helptext="", $multiple="", $key="", $numitems="") { if ($numitems == "") { $numitems = "1"; } $listbox = "<select name=\"$identifier\" $multiple size=\"$numitems\">\n"; if ($helptext != "") { $listbox .= " <option value=\"99999\">$helptext</option>\n"; } foreach($pairs AS $value => $name) { if ($value == $key) { $listbox .= " <option value=\"$value\" SELECTED>$name</option>\n"; } else $listbox .= " <option value=\"$value\">$name</option>\n"; } $listbox .= "</select>\n"; return $listbox; } If it makes sense, it only allows for one item to be selected. I am using the following "code" to to send to the function :: select * from table; while rows { $value = $GetRow['id']; $name = $GetRow['name']; $pairs["$value"] = $name; } echo createComboBox("cboathlete[]", $pairs, "", "MULTIPLE", "", "5"); I am not versed enough to figure out how to change the function to just send (for instance) a comma delimited list of items to be selected. i would like to send a list of id's that should be selected in the listbox. I am hoping it is pretty straight forward, and only need to change around the "if ($value == $key) " piece of the fuction. Thanx! Pete Quote Link to comment https://forums.phpfreaks.com/topic/161622-dynamic-listbox-multiple-selected/ Share on other sites More sharing options...
paruby Posted June 10, 2009 Author Share Posted June 10, 2009 OK. I think I got it... Posting in case anyone else is interested... First, I had to get the values that were to be "SELECTED" - this is in addition to the existing query to get the full list :: select * from Table where ...; while rows { $thisAthID = $GetRow['id']; $thisraID = $GetRow['name']; $athIDPairs["$thisraID"] = $thisAthID; } Then I had to change the function as follows :: function createListBox($identifier, $pairs, $helptext="", $multiple="", $key="", $numitems="") { if ($numitems == "") { $numitems = "1"; } $listbox = "<select name=\"$identifier\" $multiple size=\"$numitems\">\n"; if ($helptext != "") { $listbox .= " <option value=\"99999\">$helptext</option>\n"; } foreach($pairs AS $value => $name) { $selectOpt = ""; if (key != "") { foreach($key as $thisraID => $thisAthID) { if ($value == $thisAthID) { $selectOpt = " SELECTED"; continue; } } } $listbox .= " <option value=\"$value\"$selectOpt>$name</option>\n"; } $listbox .= "</select>\n"; return $listbox; } It basically checks to see if the ID of the list item matches any of the Selected item ID's. If it does, add the "selected' keyword to the code... Quote Link to comment https://forums.phpfreaks.com/topic/161622-dynamic-listbox-multiple-selected/#findComment-852876 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.