Top  Branding  Banner 
blank.gif
blank.gif
triangle.gif Du er her: /  Forsiden  /  Kildekoden  /  Form  /  Options   Login nu   Login
blank.gif
««« Se kilde koden
blank.gif
tl.gif Base tr.gif tl.gif Basic tr.gif tl.gif Dto tr.gif tls.gif     Form  trs.gif tl.gif Language tr.gif tl.gif Layout tr.gif tl.gif Menu tr.gif tl.gif Mvc tr.gif tl.gif Netbank.eksperter.dk tr.gif tl.gif Tab tr.gif tl.gif Table tr.gif tl.gif Util tr.gif
blank.gif
blank.gif
arrow-headline.gif Index
MenuLink  MenuLeft  
Tilbage

Skjul: Navn

Options.php


Vis: Sample code, tutorial

Options, Sample code, tutorial

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();
    ?>

Skjul: Sådan vises komponenten

Options, Sådan vises komponenten

Sådan vises komponenten Options klassen


Vis: PHP source code

Options, PHP source code

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();
    }
}
?>

Vis: HTML source code

Options, HTML source code

Den fulde HTML kildekode for Options klassen

<?
<!-- DEBUGOptions -->
<!-- 
DEBUGOptgroup -->
 <
optgroup label="erhverv">
<!-- 
DEBUGOption -->
    <
option value="1">erhverv 1</option>

</
optgroup>

<!-- 
DEBUGOptgroup -->
 <
optgroup label="privat">
<!-- 
DEBUGOption -->
    <
option value="2" selected="selected">privat 2</option>

<!-- 
DEBUGOption -->
    <
option value="3">privat 3</option>

</
optgroup>



?>

Vis: Class methods

Options, Class methods

Her er 'klasse metoderne' for Options klassen:

  • __construct
  • getSelected
  • newOptions
  • getHtml
  • display
  • setObject
  • set
  • get
  • getAttribute
  • getTag
  • add
  • getSizeof
  • getElement
  • getElements
  • getToogle
  • getMaximize
  • getMinimize
  • newTriangle
  • getStartHtml
  • getEndHtml
  • showsource
  • getClassName
  • getMsg
  • addHtml
  • __toString
  • getCacheFileName
  • save
  • content

Vis: Object vars

Options, Object vars

Her er 'objekt variable' for Options klassen:

  • html =>
  • sql =>

MenuRight 
triangle.gif

Dansk

Deutch

English (UK)

France

Italy

Norsk

Svensk

English (USA)


 
blank.gif
MenuBottom 
triangle.gif Copyright @ 1999-2010 www.Finn-Rasmussen.com Powered by myPHP Version (5.3.3-7+squeeze3) 1.11
blank.gif