Welcome, guest | Sign In | My Account | Store | Cart

Just a simple class i wrote to save a bit of time when dealing with dynamic selection boxes in HTML.

PHP, 47 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?
/*
** SelectionBox class
*/

class SelectionBox
{
	function SetName($name)
	{
		$this->myname = $name;
	}
	
	function SetOption($optionvalue,$optionname)
	{
		$this->options[$optionname] = $optionvalue;
	}

	function CreateBox($selected)
	{
		if($this->options)
		{
			print "<SELECT name='".$this->myname."'>\n";
	
			// Create Options
			foreach($this->options as $optionname=>$optionvalue)
			{
				print "<OPTION value='$optionvalue'";
				
				// Check if option should be selected
				if($selected == $optionvalue)
				{
					print " SELECTED";
				}
			
				print ">$optionname</OPTION>\n";
			}
			
			print "</SELECT>";
		}
		else
		{
			print "Error: No options specified";
		}
	}
}

?>

Heres how to use it:

// This is the name of the selection box $mybox->SetName("MySelectionBox");

// This is how to set an option and its value $mybox->SetOption("EU","Europe");

// Finally, we output the box and specify which option value we // want selected $mybox->CreateBox("EU");

Short and sweet, but i've used it loads.

Created by Stephen Bartholomew on Sun, 28 Apr 2002 (MIT)
PHP recipes (51)
Stephen Bartholomew's recipes (2)

Required Modules

  • (none specified)

Other Information and Tasks