Okay just trying to keep with the same thread.  Has anyone used this
helper?  Is this something included now in the Cake core?


---------- Forwarded message ----------
From: "zis" <[EMAIL PROTECTED]>
Date: Apr 11, 4:38 am
Subject: Select tag with optgroup
To: Cake PHP


Hi i would like to share with you a helper function to print a select
tag with optgroups.

Actually it's just a small mod the the existing selecttag function.

To have optgroups, make the $optionElements parameter a two dimensional
array. With the key for the sub array the name of the subgroup.. for
example

array(
'Fruits' => array(
'banana' => 'Bananas',
'apl' => 'Apples'
),
'Animals' => array(
'horse' => 'Horse',
'donley' => 'Donkey'
),
'car'=>'Car'
)

Note that car in the example will be outputed as a regular option
element..

Here's the helper.

class MorehtmlHelper extends Helper {

        var $helpers = array('Html');

        /**
 * Returns a SELECT element,
 *
 * @param string $fieldName Name attribute of the SELECT
 * @param array $optionElements Array of the OPTION elements (as
'value'=>'Text' pairs) to be used in the SELECT element. Can be a
2dimensional array.
 * @param boolean $show_empty Show/hide the empty select option
 * @param array $selectAttr Array of HTML options for the opening
SELECT element
 * @param array $optionAttr Array of HTML options for the enclosed
OPTION elements
 * @return string Formatted SELECT element
 */
    function selectOptTag($fieldName, $optionElements, $selected=null,
$selectAttr=null, $optionAttr=null, $showEmpty=true)
    {
        $this->Html->setFormTag($fieldName);
        if ($this->Html->tagIsInvalid($this->Html->model,
$this->Html->field))
        {
            if (isset($selectAttr['class']) &&
trim($selectAttr['class']) != "")
            {
                $selectAttr['class'] .= ' form_error';
            }
            else
            {
                $selectAttr['class'] = 'form_error';
            }
        }
        // do not display the select tag if no option elements are
avaible
        if (!is_array($optionElements) || count($optionElements) == 0)
        {
            return null;
        }

        if(!isset($selected))
        {
            $selected = $this->Html->tagValue($fieldName);
        }

        if( isset($selectAttr) && array_key_exists( "multiple",
$selectAttr) )
        {
            $select[] =
sprintf($this->Html->tags['selectmultiplestart'], $this->Html->model,
$this->Html->field, $this->Html->parseHtmlOptions($selectAttr));
        }
        else
        {
            $select[] = sprintf($this->Html->tags['selectstart'],
$this->Html->model, $this->Html->field,
$this->Html->parseHtmlOptions($selectAttr));
        }

        if($showEmpty == true)
        {
            $select[] = sprintf($this->Html->tags['selectempty'],
$this->Html->parseHtmlOptions($optionAttr));
        }

        foreach ($optionElements as $name => $title)
        {
                if (is_array($title)) {
                        $select[] = '<optgrouplabel="'.$name.'">';
                        foreach ($title as $subname => $subtitle)
                        {
                                $optionsHere = $optionAttr;
                            if (($selected !== null) && ($selected ==
$subname))
                            {
                                $optionsHere['selected'] = 'selected';
                            } else if ( is_array($selected) &&
array_key_exists($subname, $selected) )
                            {
                                $optionsHere['selected'] = 'selected';
                            }
                            $select[] =
sprintf($this->Html->tags['selectoption'],
$subname, $this->Html->parseHtmlOptions($optionsHere), $subtitle);

                        }
                        $select[] = '</optgroup>';

                }
                else {
                    $optionsHere = $optionAttr;
                    if (($selected !== null) && ($selected == $name))
                    {
                        $optionsHere['selected'] = 'selected';
                    } else if ( is_array($selected) &&
array_key_exists($name,
$selected) )
                    {
                        $optionsHere['selected'] = 'selected';
                    }
                    $select[] =
sprintf($this->Html->tags['selectoption'],
$name, $this->Html->parseHtmlOptions($optionsHere), $title);
                }
        }

        $select[] = sprintf($this->Html->tags['selectend']);

        return implode("\n", $select);
    }

}


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---

Reply via email to