dropdownlist-class
inc.cls.dropdownlist.php
----
Code (php)
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
function dropdownlist( $szName, $szExtras = "" )
{
return new dropdownlist( $szName, $szExtras );
}
class dropdownlist
{
private $items = array();
private $name = "";
private $extras = "";
private $size = 1;
public function __construct( $f_szName, $f_szExtras = "" )
{
$this->name = (string)$f_szName;
$this->extras = trim($f_szExtras);
}
public function item( $f_szName, $f_szValue = null, $f_bSelected = false, $f_szExtras = "" )
{
if ( is_null($f_szValue) ) $f_szValue = $f_szName;
$this->items[] = array($f_szName, $f_szValue, $f_bSelected, $f_szExtras);
return $this;
}
public function size( $f_iSize = null )
{
if ( null === $f_iSize ) $this->size = count($this->items);
else $this->size = $f_iSize;
return $this;
}
public function select( $f_iItemKey = 0 )
{
return $this->update_item($f_iItemKey, 2, true);
}
public function unselect( $f_iItemKey = 0 )
{
return $this->update_item($f_iItemKey, 2, false);
}
public function extras( $f_mixExtras )
{
$this->extras = !is_scalar($f_mixExtras) ? trim($this->parse_array($f_mixExtras)) : (string)$f_mixExtras;
return $this;
}
public function __tostring()
{
$szHtml = "";
$szHtml.= '<select name="'.$this->name.'" id="'.$this->name.'"'.( 0 < strlen($this->extras) ? ' '.$this->extras : "" ).( 1 < $this->size ? ' size="'.$this->size.'"' : "" ).'>';
foreach ( $this->items AS $arrItem )
{
$szHtml.= '<option'.( $arrItem[1] ? ' value=""' : "" ).( $arrItem[3] ? ''.$arrItem[3] : "" ).( $arrItem[2] ? ' selected="selected"' : "" ).'>'.$arrItem[0].'</option>';
}
$szHtml.= '</select>';
return $szHtml;
}
public function update_item( $f_iItem, $f_iProp, $f_mixValue )
{
if ( isset($this->items[$f_iItem][$f_iProp]) )
{
$this->items[$f_iItem][$f_iProp] = $f_mixValue;
}
return $this;
}
public function parse_array( $f_arrSource )
{
$szOut = "";
foreach ( (array)$f_arrSource AS $key => $val )
{
if ( trim($val) )
{
if ( is_int($key) ) $szOut .= ' '.$val;
else $szOut .= ' '.$key.'="'.$val.'"';
}
}
return $szOut;
}
}
?>
function dropdownlist( $szName, $szExtras = "" )
{
return new dropdownlist( $szName, $szExtras );
}
class dropdownlist
{
private $items = array();
private $name = "";
private $extras = "";
private $size = 1;
public function __construct( $f_szName, $f_szExtras = "" )
{
$this->name = (string)$f_szName;
$this->extras = trim($f_szExtras);
}
public function item( $f_szName, $f_szValue = null, $f_bSelected = false, $f_szExtras = "" )
{
if ( is_null($f_szValue) ) $f_szValue = $f_szName;
$this->items[] = array($f_szName, $f_szValue, $f_bSelected, $f_szExtras);
return $this;
}
public function size( $f_iSize = null )
{
if ( null === $f_iSize ) $this->size = count($this->items);
else $this->size = $f_iSize;
return $this;
}
public function select( $f_iItemKey = 0 )
{
return $this->update_item($f_iItemKey, 2, true);
}
public function unselect( $f_iItemKey = 0 )
{
return $this->update_item($f_iItemKey, 2, false);
}
public function extras( $f_mixExtras )
{
$this->extras = !is_scalar($f_mixExtras) ? trim($this->parse_array($f_mixExtras)) : (string)$f_mixExtras;
return $this;
}
public function __tostring()
{
$szHtml = "";
$szHtml.= '<select name="'.$this->name.'" id="'.$this->name.'"'.( 0 < strlen($this->extras) ? ' '.$this->extras : "" ).( 1 < $this->size ? ' size="'.$this->size.'"' : "" ).'>';
foreach ( $this->items AS $arrItem )
{
$szHtml.= '<option'.( $arrItem[1] ? ' value=""' : "" ).( $arrItem[3] ? ''.$arrItem[3] : "" ).( $arrItem[2] ? ' selected="selected"' : "" ).'>'.$arrItem[0].'</option>';
}
$szHtml.= '</select>';
return $szHtml;
}
public function update_item( $f_iItem, $f_iProp, $f_mixValue )
{
if ( isset($this->items[$f_iItem][$f_iProp]) )
{
$this->items[$f_iItem][$f_iProp] = $f_mixValue;
}
return $this;
}
public function parse_array( $f_arrSource )
{
$szOut = "";
foreach ( (array)$f_arrSource AS $key => $val )
{
if ( trim($val) )
{
if ( is_int($key) ) $szOut .= ' '.$val;
else $szOut .= ' '.$key.'="'.$val.'"';
}
}
return $szOut;
}
}
?>
example.php
----
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
function __autoload( $szClass )
{
require_once("inc.cls.".strtolower($szClass).".php");
}
echo '<body onload="document.forms[0].reset();">';
echo '<form method="post" action="">';
new dropdownlist(null,null);
echo $objDDL = dropdownlist("myList2", 'style="font-size:24px" class="some select"')->item("Red","Color1")->item("Blue", "Color2", true)->item("Yellow", "Color3")->item("Green", "Color4")->size(0);
echo "\n<br /><br />\n";
echo $objDDL->extras('class="some select"')->update_item(0,3,'style="background-color:green;"')->size(2);
?>
function __autoload( $szClass )
{
require_once("inc.cls.".strtolower($szClass).".php");
}
echo '<body onload="document.forms[0].reset();">';
echo '<form method="post" action="">';
new dropdownlist(null,null);
echo $objDDL = dropdownlist("myList2", 'style="font-size:24px" class="some select"')->item("Red","Color1")->item("Blue", "Color2", true)->item("Yellow", "Color3")->item("Green", "Color4")->size(0);
echo "\n<br /><br />\n";
echo $objDDL->extras('class="some select"')->update_item(0,3,'style="background-color:green;"')->size(2);
?>