<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -379,32 +379,56 @@ function radio_button_tag($name, $value, $current_value = null, $options = array
     return empty_tag('input', $options);
 }
 
-function select_tag($name, $values, $selected = null, $options = array()) {
-    $option_string = '';
-    foreach ($values as $v) {
-        $v = h($v);
-        $s = ($selected !== null &amp;&amp; $selected == $v) ? ' selected=&quot;selected&quot;' : '';
-        $option_string .= &quot;&lt;option{$sel}&gt;{$v}&lt;/option&gt;\n&quot;;
-    }
+function text_area_tag($name, $value, $options = array()) {
     $options['name'] = $name;
-    return tag('select', $option_string, $options);
+    return tag('textarea', $value, $options + array('rows' =&gt; 6, 'cols' =&gt; 50));
 }
 
-function key_select_tag($name, $values, $selected = null, $options = array()) {
-    $option_string = '';
-    foreach ($values as $k =&gt; $v) {
-        $s = ($selected !== null &amp;&amp; $selected == $k) ? ' selected=&quot;selected&quot;' : '';
-        $k = h($k);
-        $v = h($v);
-        $option_string .= &quot;&lt;option value='$k'{$sel}&gt;{$v}&lt;/option&gt;\n&quot;;
-    }
+function select_box($name, $choices, $selected = null, $options = array()) {
+    
+    $options += array('keys' =&gt; true, 'multiple' =&gt; false, 'groups' =&gt; false);
     $options['name'] = $name;
-    return tag('select', $option_string, $options);
+    
+    $ofs_opts = array('groups' =&gt; $options['groups'], 'keys' =&gt; $options['keys']);
+    unset($options['groups']);
+    unset($options['keys']);
+    
+    if ($options['multiple']) $selected = (array) $selected;
+    
+    return tag('select', options_for_select($choices, $selected, $ofs_opts), $options);
+    
+}
+
+function options_for_select($choices, $selected = null, $options = array()) {
+    $options += array('groups' =&gt; false, 'keys' =&gt; true);
+    $html = '';
+    if ($options['groups']) {
+        foreach ($choices as $group_label =&gt; $group_options) {
+            $html .= '&lt;optgroup label=&quot;' . h($group_label) . '&quot;&gt;';
+            $html .= option_group($group_options, $selected, $options['keys']);
+            $html .= '&lt;/optgroup&gt;';
+        }
+    } else {
+        $html .= option_group($choices, $selected, $options['keys']);
+    }
+    return $html;
 }
 
-function text_area_tag($name, $value, $options = array()) {
-    $options['name'] = $name;
-    return tag('textarea', $value, $options + array('rows' =&gt; 6, 'cols' =&gt; 50));
+function option_group($choices, $selected, $use_keys) {
+    $html = '';
+    foreach ($choices as $k =&gt; $v) {
+        $c = $use_keys ? $k : $v;
+        $s = is_array($selected) ? in_array($c, $selected) : ($selected == $c);
+        $s = $s ? ' selected=&quot;selected&quot;' : '';
+        $v = htmlentities($v);
+        if ($use_keys) {
+            $k = htmlentities($k);
+            $html .= &quot;&lt;option value=\&quot;$k\&quot;{$s}&gt;{$v}&lt;/option&gt;&quot;;
+        } else {
+            $html .= &quot;&lt;option{$s}&gt;{$v}&lt;/option&gt;&quot;;
+        }
+    }
+    return $html;
 }
 
 //</diff>
      <filename>helpers-5.3.php</filename>
    </modified>
    <modified>
      <diff>@@ -379,32 +379,56 @@ function radio_button_tag($name, $value, $current_value = null, $options = array
     return empty_tag('input', $options);
 }
 
-function select_tag($name, $values, $selected = null, $options = array()) {
-    $option_string = '';
-    foreach ($values as $v) {
-        $v = h($v);
-        $s = ($selected !== null &amp;&amp; $selected == $v) ? ' selected=&quot;selected&quot;' : '';
-        $option_string .= &quot;&lt;option{$sel}&gt;{$v}&lt;/option&gt;\n&quot;;
-    }
+function text_area_tag($name, $value, $options = array()) {
     $options['name'] = $name;
-    return tag('select', $option_string, $options);
+    return tag('textarea', $value, $options + array('rows' =&gt; 6, 'cols' =&gt; 50));
 }
 
-function key_select_tag($name, $values, $selected = null, $options = array()) {
-    $option_string = '';
-    foreach ($values as $k =&gt; $v) {
-        $s = ($selected !== null &amp;&amp; $selected == $k) ? ' selected=&quot;selected&quot;' : '';
-        $k = h($k);
-        $v = h($v);
-        $option_string .= &quot;&lt;option value='$k'{$sel}&gt;{$v}&lt;/option&gt;\n&quot;;
-    }
+function select_box($name, $choices, $selected = null, $options = array()) {
+    
+    $options += array('keys' =&gt; true, 'multiple' =&gt; false, 'groups' =&gt; false);
     $options['name'] = $name;
-    return tag('select', $option_string, $options);
+    
+    $ofs_opts = array('groups' =&gt; $options['groups'], 'keys' =&gt; $options['keys']);
+    unset($options['groups']);
+    unset($options['keys']);
+    
+    if ($options['multiple']) $selected = (array) $selected;
+    
+    return tag('select', options_for_select($choices, $selected, $ofs_opts), $options);
+    
+}
+
+function options_for_select($choices, $selected = null, $options = array()) {
+    $options += array('groups' =&gt; false, 'keys' =&gt; true);
+    $html = '';
+    if ($options['groups']) {
+        foreach ($choices as $group_label =&gt; $group_options) {
+            $html .= '&lt;optgroup label=&quot;' . h($group_label) . '&quot;&gt;';
+            $html .= option_group($group_options, $selected, $options['keys']);
+            $html .= '&lt;/optgroup&gt;';
+        }
+    } else {
+        $html .= option_group($choices, $selected, $options['keys']);
+    }
+    return $html;
 }
 
-function text_area_tag($name, $value, $options = array()) {
-    $options['name'] = $name;
-    return tag('textarea', $value, $options + array('rows' =&gt; 6, 'cols' =&gt; 50));
+function option_group($choices, $selected, $use_keys) {
+    $html = '';
+    foreach ($choices as $k =&gt; $v) {
+        $c = $use_keys ? $k : $v;
+        $s = is_array($selected) ? in_array($c, $selected) : ($selected == $c);
+        $s = $s ? ' selected=&quot;selected&quot;' : '';
+        $v = htmlentities($v);
+        if ($use_keys) {
+            $k = htmlentities($k);
+            $html .= &quot;&lt;option value=\&quot;$k\&quot;{$s}&gt;{$v}&lt;/option&gt;&quot;;
+        } else {
+            $html .= &quot;&lt;option{$s}&gt;{$v}&lt;/option&gt;&quot;;
+        }
+    }
+    return $html;
 }
 
 ?&gt;</diff>
      <filename>helpers.php</filename>
    </modified>
    <modified>
      <diff>@@ -62,32 +62,56 @@ function radio_button_tag($name, $value, $current_value = null, $options = array
     return empty_tag('input', $options);
 }
 
-function select_tag($name, $values, $selected = null, $options = array()) {
-    $option_string = '';
-    foreach ($values as $v) {
-        $v = h($v);
-        $s = ($selected !== null &amp;&amp; $selected == $v) ? ' selected=&quot;selected&quot;' : '';
-        $option_string .= &quot;&lt;option{$sel}&gt;{$v}&lt;/option&gt;\n&quot;;
-    }
+function text_area_tag($name, $value, $options = array()) {
     $options['name'] = $name;
-    return tag('select', $option_string, $options);
+    return tag('textarea', $value, $options + array('rows' =&gt; 6, 'cols' =&gt; 50));
 }
 
-function key_select_tag($name, $values, $selected = null, $options = array()) {
-    $option_string = '';
-    foreach ($values as $k =&gt; $v) {
-        $s = ($selected !== null &amp;&amp; $selected == $k) ? ' selected=&quot;selected&quot;' : '';
-        $k = h($k);
-        $v = h($v);
-        $option_string .= &quot;&lt;option value='$k'{$sel}&gt;{$v}&lt;/option&gt;\n&quot;;
-    }
+function select_box($name, $choices, $selected = null, $options = array()) {
+    
+    $options += array('keys' =&gt; true, 'multiple' =&gt; false, 'groups' =&gt; false);
     $options['name'] = $name;
-    return tag('select', $option_string, $options);
+    
+    $ofs_opts = array('groups' =&gt; $options['groups'], 'keys' =&gt; $options['keys']);
+    unset($options['groups']);
+    unset($options['keys']);
+    
+    if ($options['multiple']) $selected = (array) $selected;
+    
+    return tag('select', options_for_select($choices, $selected, $ofs_opts), $options);
+    
 }
 
-function text_area_tag($name, $value, $options = array()) {
-    $options['name'] = $name;
-    return tag('textarea', $value, $options + array('rows' =&gt; 6, 'cols' =&gt; 50));
+function options_for_select($choices, $selected = null, $options = array()) {
+    $options += array('groups' =&gt; false, 'keys' =&gt; true);
+    $html = '';
+    if ($options['groups']) {
+        foreach ($choices as $group_label =&gt; $group_options) {
+            $html .= '&lt;optgroup label=&quot;' . h($group_label) . '&quot;&gt;';
+            $html .= option_group($group_options, $selected, $options['keys']);
+            $html .= '&lt;/optgroup&gt;';
+        }
+    } else {
+        $html .= option_group($choices, $selected, $options['keys']);
+    }
+    return $html;
+}
+
+function option_group($choices, $selected, $use_keys) {
+    $html = '';
+    foreach ($choices as $k =&gt; $v) {
+        $c = $use_keys ? $k : $v;
+        $s = is_array($selected) ? in_array($c, $selected) : ($selected == $c);
+        $s = $s ? ' selected=&quot;selected&quot;' : '';
+        $v = htmlentities($v);
+        if ($use_keys) {
+            $k = htmlentities($k);
+            $html .= &quot;&lt;option value=\&quot;$k\&quot;{$s}&gt;{$v}&lt;/option&gt;&quot;;
+        } else {
+            $html .= &quot;&lt;option{$s}&gt;{$v}&lt;/option&gt;&quot;;
+        }
+    }
+    return $html;
 }
 
 ?&gt;
\ No newline at end of file</diff>
      <filename>src/form.php</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>debd4e74e4f5e3cb840cee0a67e3b982868c3940</id>
    </parent>
  </parents>
  <author>
    <name>Jason Frame</name>
    <email>jason@magiclamp.co.uk</email>
  </author>
  <url>http://github.com/jaz303/php-helpers/commit/798ead881e17fae15bc47b20ab9b18105fd9a028</url>
  <id>798ead881e17fae15bc47b20ab9b18105fd9a028</id>
  <committed-date>2009-05-19T13:44:07-07:00</committed-date>
  <authored-date>2009-05-19T13:44:07-07:00</authored-date>
  <message>select box helpers</message>
  <tree>45f85b402f7ddb4545be65d906c3590019d9775b</tree>
  <committer>
    <name>Jason Frame</name>
    <email>jason@magiclamp.co.uk</email>
  </committer>
</commit>
