API15

JHTMLSelect/options

From Joomla! Documentation

< API15:JHTMLSelect

The "API15" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Description[edit]

Generates just the option tags for an HTML select list

[<! removed edit link to red link >]

<! removed transcluded page call, red link never existed >

Syntax[edit]

options($arr, $key= 'value', $text= 'text', $selected=null, $translate=false)
Parameter Name Default Value Description
$arr An array of objects
$key 'value' The name of the object variable for the option value
$text 'text' The name of the object variable for the option text
$selected null The key that is selected (accepts an array or a string)
$translate false

Returns[edit]

string HTML for the select list

Defined in[edit]

libraries/joomla/html/html/select.php

Importing[edit]

jimport( 'joomla.html.html.select' );

Source Body[edit]

function options( $arr, $key = 'value', $text = 'text', $selected = null, $translate = false )
{
        $html = '';

        foreach ($arr as $i => $option)
        {
                $element =& $arr[$i]; // since current doesn't return a reference, need to do this

                $isArray = is_array( $element );
                $extra   = '';
                if ($isArray)
                {
                        $k              = $element[$key];
                        $t              = $element[$text];
                        $id     = ( isset( $element['id'] ) ? $element['id'] : null );
                        if(isset($element['disable']) && $element['disable']) {
                                $extra .= ' disabled="disabled"';
                        }
                }
                else
                {
                        $k              = $element->$key;
                        $t              = $element->$text;
                        $id     = ( isset( $element->id ) ? $element->id : null );
                        if(isset( $element->disable ) && $element->disable) {
                                $extra .= ' disabled="disabled"';
                        }
                }

                // This is real dirty, open to suggestions,
                // barring doing a propper object to handle it
                if ($k === '<OPTGROUP>') {
                        $html .= '<optgroup label="' . $t . '">';
                } else if ($k === '</OPTGROUP>') {
                        $html .= '</optgroup>';
                }
                else
                {
                        //if no string after hypen - take hypen out
                        $splitText = explode( ' - ', $t, 2 );
                        $t = $splitText[0];
                        if(isset($splitText[1])){ $t .= ' - '. $splitText[1]; }

                        //$extra = '';
                        //$extra .= $id ? ' id="' . $arr[$i]->id . '"' : '';
                        if (is_array( $selected ))
                        {
                                foreach ($selected as $val)
                                {
                                        $k2 = is_object( $val ) ? $val->$key : $val;
                                        if ($k == $k2)
                                        {
                                                $extra .= ' selected="selected"';
                                                break;
                                        }
                                }
                        } else {
                                $extra .= ( (string)$k == (string)$selected  ? ' selected="selected"' : '' );
                        }

                        //if flag translate text
                        if ($translate) {
                                $t = JText::_( $t );
                        }

                        // ensure ampersands are encoded
                        $k = JFilterOutput::ampReplace($k);
                        $t = JFilterOutput::ampReplace($t);

                        $html .= '<option value="'. $k .'" '. $extra .'>' . $t . '</option>';
                }
        }

        return $html;
}

[<! removed edit link to red link >] <! removed transcluded page call, red link never existed >

Examples[edit]

Code Examples[edit]