Actions

API15

JHTMLSelect/options

From Joomla! Documentation

< API15:JHTMLSelect
Revision as of 13:31, 12 May 2013 by JoomlaWikiBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Contents

Description

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

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

string HTML for the select list

Defined in

libraries/joomla/html/html/select.php

Importing

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

Source Body

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

<CodeExamplesForm />