Scripts

dropdownlist class

Het is een kleine simpele class om dropdownlists te maken. Alles kan in 1 regel (chainability) en dmv __toString() hoef je geen aparte toHtml() oid functie aan te roepen. Het gebruikt een functie om te starten voor makkelijkheid. Het voorbeeld is met __autoload() en een zinloze class instance, maar je kan natuurlijk ook gewoon het bestand includen. Veel plezier ermee

dropdownlist-class
inc.cls.dropdownlist.php
----
[code]<?php

function dropdownlist( $szName, $szExtras = "" )
{
	return new dropdownlist( $szName, $szExtras );
}

class dropdownlist
{

	private $items			= array();
	private $name			= "";
	private $extras			= "";
	private $size			= 1;


	public function __construct( $f_szName, $f_szExtras = "" )
	{
		$this->name		= (string)$f_szName;
		$this->extras	= trim($f_szExtras);
	}

	public function item( $f_szName, $f_szValue = null, $f_bSelected = false, $f_szExtras = "" )
	{
		if ( is_null($f_szValue) ) $f_szValue = $f_szName;

		$this->items[] = array($f_szName, $f_szValue, $f_bSelected, $f_szExtras);

		return $this;
	}

	public function size( $f_iSize = null )
	{
		if ( null === $f_iSize )	$this->size = count($this->items);
		else						$this->size = $f_iSize;

		return $this;
	}

	public function select( $f_iItemKey = 0 )
	{
		return $this->update_item($f_iItemKey, 2, true);
	}

	public function unselect( $f_iItemKey = 0 )
	{
		return $this->update_item($f_iItemKey, 2, false);
	}

	public function extras( $f_mixExtras )
	{
		$this->extras = !is_scalar($f_mixExtras) ? trim($this->parse_array($f_mixExtras)) : (string)$f_mixExtras;
		return $this;
	}


	public function __tostring()
	{
		$szHtml = "";
		$szHtml.= '<select name="'.$this->name.'" id="'.$this->name.'"'.( 0 < strlen($this->extras) ? ' '.$this->extras : "" ).( 1 < $this->size ? ' size="'.$this->size.'"' : "" ).'>';
		foreach ( $this->items AS $arrItem )
		{
			$szHtml.= '<option'.( $arrItem[1] ? ' value=""' : "" ).( $arrItem[3] ? ''.$arrItem[3] : "" ).( $arrItem[2] ? ' selected="selected"' : "" ).'>'.$arrItem[0].'</option>';
		}
		$szHtml.= '</select>';

		return $szHtml;
	}


	public function update_item( $f_iItem, $f_iProp, $f_mixValue )
	{
		if ( isset($this->items[$f_iItem][$f_iProp]) )
		{
			$this->items[$f_iItem][$f_iProp] = $f_mixValue;
		}

		return $this;
	}

	public function parse_array( $f_arrSource )
	{
		$szOut = "";
		foreach ( (array)$f_arrSource AS $key => $val )
		{
			if ( trim($val) )
			{
				if ( is_int($key) )	$szOut .= ' '.$val;
				else				$szOut .= ' '.$key.'="'.$val.'"';
			}
		}
		return $szOut;
	}

}

?>[/code]


example.php
----
[code]<?php

function __autoload( $szClass )
{
	require_once("inc.cls.".strtolower($szClass).".php");
}


echo '<body onload="document.forms[0].reset();">';
echo '<form method="post" action="">';

new dropdownlist(null,null);

echo $objDDL = dropdownlist("myList2", 'style="font-size:24px" class="some select"')->item("Red","Color1")->item("Blue", "Color2", true)->item("Yellow", "Color3")->item("Green", "Color4")->size(0);

echo "\n<br /><br />\n";

echo $objDDL->extras('class="some select"')->update_item(0,3,'style="background-color:green;"')->size(2);

?>[/code]

Reacties

0
Nog geen reacties.