Sådan benyttes komponenten Options klassen
Først skal du inkludere den fil der beskriver komponenten, som en klasse fil
<? require_once(HTML_PACKAGE_PATH.'/Options.php'); ?>
Dernæst kan du enten benytte komponenten som et taglib (statiske metoder):
<? Options::display($param1, $param2, $param3, ...); ?>
eller du kan lave en instance af komponenten og benytte metoderne direkte:
<? $object = new Options($param1, $param2, $param3, ...); print $object->getHtml(); ?>
Sådan vises komponenten Options klassen
Den fulde PHP kildekode for Options klassen
<?php/** * @package form * @filesource * @see HTML_FORM_COMPONENT_PATH.'/Options.php' * @copyright (c) http://Finn-Rasmussen.com * @license http://Finn-Rasmussen.com/license/ myPHP License conditions * @author http://Finn-Rasmussen.com * @version 1.11 * @since 27-nov-2009 *//** * The required files */require_once(HTML_BASE_COMMON_PATH.'/Html.php');require_once(HTML_FORM_COMPONENT_PATH.'/Option.php');require_once(HTML_FORM_COMPONENT_PATH.'/Optgroup.php');require_once(HTML_UTIL_COMPONENT_PATH.'/Request.php');/** * Generates an array of OPTION form element * <code> * Usage: * $options = array( * array('name'=>'theName','label'=>'','text'=>'option 1','value'=>'1','title'=>'','selected'=>'',), * array('name'=>'theName','label'=>'','text'=>'option 2','value'=>'2','title'=>'','selected'=>'selected',), * array('name'=>'theName','label'=>'','text'=>'option 3','value'=>'3','title'=>'','selected'=>'',), * ); * $elements = new Options($options); * print $elements->getHtml(); * Or * $select = new Select(); * $select->add($elements); * print $select->getHtml(); * Or * Options::display($options); * Or * Select::start($name); * Options::display($options); * Select::end(); * </code> * @package form */class Options extends Html { /** * @var String $name The name of the select element */ protected $name = ''; /** * @var array $options The array of option elements */ protected $options = array(); /** * Constructor * @param array $options The array of options */ function __construct($options) { parent::__construct(); $this->options = $options; $this->add($this->newOptions()); // Must be called here, because the label is used in the ViewSelect } /** * Returns the selected attribute, which is the default supplied * or if any request detected, use the value from the form post or get * @see Select.php * @param String $default The default value of the selected attribute * @param String $value The value of the selected attribute from the form post/get request * @return String the selected value 'selected' or empty '' */ function getSelected($default, $value) { $selected = $default; /** * Overule the default selected value, if any request is detected * Otherwise, use the the selected value from the options array */ $isSelected = $this->name !== '' ? Request::get($this->name) : ''; if ($isSelected !== '') { if ($isSelected == $value) { $selected = 'selected'; } else { $selected = ''; } } /** * Very crude data type conversion */ $selected = $selected !== '' ? 'selected' : ''; if ($selected != '' && $selected != 'selected') { die('File: '.__FILE__."<br />\r\nLine: ".__LINE__."<br />\r\n"."Unknown option attribute ($default, $value, $isSelected) found for selected=$selected accepted values are '' or selected"); } return $selected; } /** * Get the new array of options */ function newOptions() { $object = new Raw(); $optgroup = ''; $oldlabel = '-1'; foreach($this->options as $no=>$option) { if (!is_array($option)) { die('File: '.__FILE__."<br />\r\nLine: ".__LINE__."<br />\r\n".'Expected an array of key,value for options found option='.$option); } $name = ''; $text = ''; $value = ''; $selected = ''; $title = ''; $label = ''; foreach($option as $key=>$aValue) { switch($key) { case 'name': case 'label': case 'value': case 'text': case 'selected': $$key = $aValue; break; default: //die('File: '.__FILE__."<br />\r\nLine: ".__LINE__."<br />\r\n".'Unknown option attribute found key='.$key); break; } } /** * Create a new Optgroup, if not exists */ if ($oldlabel === '-1' && $optgroup === '') { if ($label === '') { $optgroup = new Raw(); } else { $optgroup = new Optgroup($label); } } /** * If a new label is detected, then save the previosly optgropt * and create a new one with the new label */ if ($oldlabel !== '-1' && $oldlabel !== $label) { $object->add($optgroup); $optgroup = new Optgroup($label); } /** * The name may be used from outside this class */ if ($this->name === '') { $this->name = $name; } /** * Create the Option element to use, set the selected attribut, if selected */ $theSelected = $this->getSelected($selected, $value); $element = new Option($text, $value, $theSelected, $title); $optgroup->add($element); $oldlabel = $label; } $object->add($optgroup); return $object; } /** * Returns the html for the array of option controls * @return String the complete html */ function getHtml() { $html = $this->html; $html .= $this->getElements(); return $html; } /** * Display html * <code> * Usage: * $options = array( * array('name'=>'theName','text'=>'option 1','value'=>'1','selected'=>'','title'=>'','label'=>''), * array('name'=>'theName','text'=>'option 2','value'=>'2','selected'=>'selected','title'=>'','label'=>''), * array('name'=>'theName','text'=>'option 3','value'=>'3','selected'=>'','title'=>'','label'=>''), * ); * Options::display($options); * </code> * @param array $options The array of options */ public static function display($options) { $html = new Options($options); $html->addHtml(); }}?>
Den fulde HTML kildekode for Options klassen
<? <!-- DEBUG: Options --> <!-- DEBUG: Optgroup --> <optgroup label="erhverv"> <!-- DEBUG: Option --> <option value="1">erhverv 1</option> </optgroup> <!-- DEBUG: Optgroup --> <optgroup label="privat"> <!-- DEBUG: Option --> <option value="2" selected="selected">privat 2</option> <!-- DEBUG: Option --> <option value="3">privat 3</option> </optgroup> ?>
Her er 'klasse metoderne' for Options klassen:
Her er 'objekt variable' for Options klassen: