Made the registration process and TOS more appropriate.
[specialops2.git] / lib / class.HTML_Select.php
blob4681f8b503f7cc9e78e7c6931768ab3f7c572fa1
1 <?php
2 /**
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
7 * @version $Id$
8 */
9 class HTML_Select
11 public $name;
12 public $indent;
13 public $default;
14 public $attributes;
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 = '')
27 if ( !$name ) {
28 $name = $value;
30 $this->values[$name] = $value;
33 /**
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);
55 function display()
57 $t = str_repeat("\t", $this->indent);
59 // Concat attributes into a string
60 $attribs = '';
61 foreach ( $this->attributes as $name => $value ) {
62 $attribs .= sprintf(' %s="%s"', $name, $value);
65 // Concat options into another string (how exciting!)
66 $opts = '';
67 foreach ( $this->values as $name => $value ) {
68 $opts .= sprintf('%s <option value="%s"%s>%s</option>'."\n",
69 $t,
70 $value,
71 ( $value == $this->default ? ' selected="selected"' : '' ),
72 $name );
75 return '<select'.$attribs.">\n".$opts.$t.'</select>';