Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/161622-dynamic-listbox-multiple-selected/
Share on other sites

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...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.