-
Notifications
You must be signed in to change notification settings - Fork 0
/
selecthelper.class.php
executable file
·158 lines (142 loc) · 3.64 KB
/
selecthelper.class.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* SelectHelper
*
* @package default
* @author Ryan Abbott
*/
class SelectHelper extends FormHelper
{
/**
* __construct
*
* @param string $data
* @author Ryan Abbott
*/
public function __construct($data = null) {
parent::__construct($data);
if($data != null) {
if(isset($data['options'])) {
$this->addOptions($data['options']);
}
}
}
/**
* addOption
*
* @param string $option
* @return void
* @author Ryan Abbott
*/
public function addOption($value, $text, $selected = "") {
$this->options[] = new Option($value, $text, $selected);
}
/**
* addOptions
*
* @param string $options
* @return void
* @author Ryan Abbott
*/
public function addOptions($options) {
if(!is_array($options)) {
return false;
}
foreach($options as $value => $text) {
$this->addOption($value, $text);
}
}
/**
* addDatabaseOptions
*
* @param string $table
* @param string $keyColumn
* @param string $valueColumn
* @param string $conditions
* @return void
* @author Ryan Abbott
*/
public function addDatabaseOptions($table, $keyColumn, $valueColumn = null, $conditions = null, $order = null) {
$sql = "SELECT $keyColumn" . (($valueColumn != null) ? ", $valueColumn" : "") . " FROM $table" . (($conditions != null) ? " WHERE $conditions" : "") . (($order != null) ? " ORDER BY $order" : "");
$this->db->query($sql);
if($this->db->numRows() > 0) {
while($row = mysql_fetch_array($this->db->result)) {
$this->options[] = new Option($row[$keyColumn], $row[(($valueColumn != null) ? $valueColumn : $keyColumn)]);
}
}
}
public function getDatabaseOptions($table, $keyColumn, $valueColumn = null, $conditions = null, $order = null) {
$options = array();
$sql = "SELECT $keyColumn" . (($valueColumn != null) ? ", $valueColumn" : "") . " FROM $table" . (($conditions != null) ? " WHERE $conditions" : "") . (($order != null) ? " ORDER BY $order" : "");
$this->db->query($sql);
if($this->db->numRows() > 0) {
while($row = mysql_fetch_array($this->db->result)) {
$options[$row[$keyColumn]] = $row[(($valueColumn != null) ? $valueColumn : $keyColumn)];
}
}
return $options;
}
/**
* toString
*
* @param string $selected
* @param string $version
* @return void
* @author Ryan Abbott
*/
public function toString($selected = "none", $version = TOSTRING_FULL) {
// determine which version of the object to dislay
if($version == TOSTRING_FULL) {
$html = "<select".
(($this->name != "") ? " name=\"$this->name\"" : "") .
(($this->id != "") ? " id=\"$this->id\"" : "") .
(($this->class != "") ? " class=\"$this->class\"" : "") .
(($this->disabled != "") ? " disabled=\"disabled\"" : "") .">\n";
foreach($this->options as $option) {
$html .= $option->toString() ."\n";
}
$html .= "</select>\n";
return $html;
}
}
}
/**
* Option
*
* @package default
* @author Ryan Abbott
*/
class Option extends BaseClass
{
/**
* __construct
*
* @param string $key
* @param string $value
* @author Ryan Abbott
*/
public function __construct($key, $value, $selected = "") {
$this->key = $key;
$this->value = $value;
if(isset($selected) && $selected === true) {
$this->selected = true;
}
else {
$this->selected = false;
}
}
/**
* toString
*
* @param string $version
* @return void
* @author Ryan Abbott
*/
public function toString($version = TOSTRING_FULL) {
// determine which version of the object to dislay
if($version == TOSTRING_FULL) {
return "<option value=\"$this->key\"".(($this->selected) ? " selected=\"selected\"" : "").">$this->value</option>";
}
}
}
?>