3 * Object-oriented HTML selectbox with validation
5 * @author Anthony Parsons (xmpp:ant@specialops.ath.cx)
6 * @license http://specialops.ath.cx/repos/so2/trunk/COPYING
15 private $values = array();
17 function __construct($name, $indent = 0, $default = null, $attributes = array())
19 $this->indent
= $indent;
20 $this->default = $default;
21 $this->attributes
= $attributes;
22 $this->attributes
['name'] = $name;
25 function add_item($value, $name = '')
30 $this->values
[$name] = $value;
34 * Fill selectbox with rows of name:value pairs from a MySQLi result set
36 function db_fill(mysqli_result
$query)
38 while ( $row = $query->fetch_row() ) {
39 $this->add_item($row[0], $row[1]);
43 function set_default($value)
45 $this->default = $value;
48 function check_value(&$value)
50 if ( !isset($value) ||
!in_array($value, $this->values
) ) {
51 throw new OutOfBoundsException('Bad value given for selectbox "'.$this->name
.'": '.$value);
57 $t = str_repeat("\t", $this->indent
);
59 // Concat attributes into a string
61 foreach ( $this->attributes
as $name => $value ) {
62 $attribs .= sprintf(' %s="%s"', $name, $value);
65 // Concat options into another string (how exciting!)
67 foreach ( $this->values
as $name => $value ) {
68 $opts .= sprintf('%s <option value="%s"%s>%s</option>'."\n",
71 ( $value == $this->default ?
' selected="selected"' : '' ),
75 return '<select'.$attribs.">\n".$opts.$t.'</select>';