A37583acaede070320d6f28146074934

This method outputs a standard HTML select menu. Options are provided as an array argument.

class FormComponents
{
        /*
        * Outputs a standard HTML <select> menu with options
        * $name - The value of the name attribute for the <select> tag
        * $options - An associative array:
        *     $options = array('Optgroup Label' => array('Option value' => 'Option Display Text',
        *                                                 'Option value 2' => 'Option Display Text'));
        */
        public static function selectMenu($name, $options, $selectedValue = NULL)
	    {
	    	?><select name="<?php echo $name; ?>"><?php
	    	foreach($options as $label => $options)
	    	{
	    		?><optgroup label="<?php echo $label; ?>"><?php
	    		foreach($options as $value => $display)
	    		{
	    			if($selectedValue == NULL) {
                                    $selected = '';
                                } else {
	    			    ($value == $selectedValue) ? $selected = 'selected' : $selected = '';
	    			}
	    			?><option value="<?php echo $value; ?>" <?php echo $selected; ?> ><?php echo $display; ?></option><?php
	    		}
	    		?></optgroup><?php
	    	}
	    	?></select><?php
	    }    
}

Refactorings

No refactoring yet !

55502f40dc8b7c769880b10874abc9d0

michal-pochwala.myopenid.com

August 26, 2010, August 26, 2010 09:07, permalink

No rating. Login to rate!

I think it is a nice approach to generate html components. This code express only my point of view and it is not fully completed (but of course this example works). Code below does not check if you are closing groups properly and it should be done if you want to use it in production. Comments are welcome so feel free to do it. ;)

<?php
class SelectComponent{
	private $name;
	private $id;
	private $options = array();
	private $currentWorkingOptionGroup; 
	private function __construct(){	}
	public static function build(){
		return new SelectComponent();
	}
	public function setName($name){
		$this->name = $name;
		return $this;
	}
	public function setId($id){
		$this->id = $id;
		return $this;
	}
	public function startOptionGroup($label){
		$this->currentWorkingOptionGroup = new OptionGroupSelectComponentPart($label);
		return $this;
	}
	public function addOptionComponentPart($value,$name,$selected=false){
		if( $this->currentWorkingOptionGroup != null ){
				$this->currentWorkingOptionGroup->addOptionComponentPart($value,$name,$selected);
		}else{
			$this->options[] = OptionSelectComponentPart::build($value,$name,$selected);
		}
		return $this;
	}
	public function closeOptionGroup(){
		$this->options[] = $this->currentWorkingOptionGroup;
		$this->currentWorkingOptionGroup = null;
		return $this;
	}
	
	public function __toString(){
		return $this->render();
	}
	
	public function render($cssClasses= ''){
		$output = "<select id='".$this->id."' name='".$this->name."' class='".$cssClasses."'>";
		foreach ($this->options as $option){
			$output .= $option->__toString();
		}
		$output.= "</select>"; 
		return $output;
	}
}
class OptionGroupSelectComponentPart{
	private $label;
	private $options = array();
	public function __construct($label){
		$this->label = $label;
	}
	public function addOptionComponentPart($value,$name,$selected=false){
		$this->options[] = OptionSelectComponentPart::build($value,$name,$selected);
		return $this;
	}
	public function __toString(){
		$output =  "<optgroup label='".$this->label."'>";
		foreach ($this->options as $option){
			$output .= $option->__toString();
		}
		$output .= "</optgroup>";
		return $output;
	}	
}
class OptionSelectComponentPart{
	private $value;
	private $name;
	private $selected;
	private function __construct($value,$name,$selected){
		$this->value = $value;
		$this->name = $name;
		$this->selected = $selected;
	}
	public function build($value,$name,$selected=false){
		return new OptionSelectComponentPart($value,$name,$selected);
	}
	public function __toString(){
		$selectedAttribute = $this->selected ? ' selected' : '';
		return "<option value='".$this->value."'".$selectedAttribute.">".$this->name."</option>";
	}
}
?>
<?php 
$select = SelectComponent::build()->setName("selectName")->setId("selectId")
			->startOptionGroup("optionGroup1")
				->addOptionComponentPart("value1","name1")
				->addOptionComponentPart("value2","name2",true)
				->addOptionComponentPart("value3","name3")
			->closeOptionGroup()
			->startOptionGroup("optionGroup2")
				->addOptionComponentPart("value2_1","name2_1")
				->addOptionComponentPart("value2_2","name2_2")
				->addOptionComponentPart("value2_3","name2_3")
			->closeOptionGroup()
?>
			->addOptionComponentPart("value3","name3");
				

echo $select;
B3bae81139f6c219b3cf05e9519567c9

weekly jobnews fuel

December 2, 2010, December 02, 2010 23:48, permalink

No rating. Login to rate!

Good Hardly,world truth completely strongly pass module nation pupil traditional whether though faith should financial speech year estate learn field afford agent file special lot access anyway emphasis connection candidate band else coal yet anyone whole even perform obvious pressure cost no-one new owner under element radio beautiful carefully literature idea director last forest finally close quiet severe object adopt check mark stuff rare quiet coal problem start end culture danger combination benefit concentrate flower control less fill fail face development cause theatre article colleague show phase enough top ignore population roof

Good Hardly,world truth completely strongly pass module nation pupil traditional whether though faith should financial speech year estate learn field afford agent file special lot access anyway emphasis connection candidate band else coal yet anyone whole even perform obvious pressure cost no-one new owner under element radio beautiful carefully literature idea director last forest finally close quiet severe object adopt check mark stuff rare quiet coal problem start end culture danger combination benefit concentrate flower control less fill fail face development cause theatre article colleague show phase enough top ignore population roof
81ee518a1181fd0766c4ec89e5d7a079

flellareire

September 4, 2011, September 04, 2011 11:51, permalink

No rating. Login to rate!

Thanks mate... just dropped by.!!!!! Will look for BIKE STN when we get to Seattle. Still in Buenos Airies.!!!!!

81ee518a1181fd0766c4ec89e5d7a079

NarbrommagDug

September 7, 2011, September 07, 2011 05:57, permalink

No rating. Login to rate!

Thanks mate... just dropped by.!!!!! Will look for BIKE STN when we get to Seattle. Still in Buenos Airies.!!!!!

5e536dbc473469d0af1d44020a4b0bab

Cacralley

September 20, 2011, September 20, 2011 21:24, permalink

No rating. Login to rate!

Thanks mate... just dropped by.!!!!! Will look for BIKE STN when we get to Seattle. Still in Buenos Airies.!!!!!

772ebcc4c3ef8a1dab8b677c0c45c588

rorkedmedia

November 1, 2011, November 01, 2011 12:16, permalink

No rating. Login to rate!

KOVAL ! why do you only respond to people who threaten to unsubscribe... what about me....Id like a shout out too ....I watched all your videos....TWICE.....i loved you when you weren't? famous.... *sigh*

9e67cf08593df723202d8a7083072f67

Deannyten

November 15, 2011, November 15, 2011 17:11, permalink

No rating. Login to rate!

KOVAL ! why do you only respond to people who threaten to unsubscribe... what about me....Id like a shout out too ....I watched all your videos....TWICE.....i loved you when you weren't? famous.... *sigh*

F0331c47e300bd43483bcce637523003

vostenofs

November 16, 2011, November 16, 2011 14:48, permalink

No rating. Login to rate!

BloombergFTR, GS, CVS, DHI, PM - Wednesday Notable Stocks with Volume at NYSEHealth Talk & YouCVS Caremark Corporation (CVS Caremark) is a pharmacy healthcare provider in the United States.

76ba81d8bf91ab0042fc3476b487a408

Peeleaskirl

November 19, 2011, November 19, 2011 19:00, permalink

No rating. Login to rate!

BloombergFTR, GS, CVS, DHI, PM - Wednesday Notable Stocks with Volume at NYSEHealth Talk & YouCVS Caremark Corporation (CVS Caremark) is a pharmacy healthcare provider in the United States.

1a295524b1ce0c250072963eead13184

nocaoccub

November 12, 2011, November 12, 2011 07:59, permalink

No rating. Login to rate!

BloombergFTR, GS, CVS, DHI, PM - Wednesday Notable Stocks with Volume at NYSEHealth Talk & YouCVS Caremark Corporation (CVS Caremark) is a pharmacy healthcare provider in the United States.

0ce0d6b7c8024ca2e870faf33add7e7a

Envivenombiva

November 17, 2011, November 17, 2011 21:23, permalink

No rating. Login to rate!

BloombergFTR, GS, CVS, DHI, PM - Wednesday Notable Stocks with Volume at NYSEHealth Talk & YouCVS Caremark Corporation (CVS Caremark) is a pharmacy healthcare provider in the United States.

A00cef0bc511c9c4f6b8fa1c81e98917

erunnyutesy

November 15, 2011, November 15, 2011 07:43, permalink

No rating. Login to rate!

BloombergFTR, GS, CVS, DHI, PM - Wednesday Notable Stocks with Volume at NYSEHealth Talk & YouCVS Caremark Corporation (CVS Caremark) is a pharmacy healthcare provider in the United States.

9567bd4ee5ea6fdc6ff66c69e5c0b968

Rilsotbloth

November 20, 2011, November 20, 2011 18:12, permalink

No rating. Login to rate!

BloombergFTR, GS, CVS, DHI, PM - Wednesday Notable Stocks with Volume at NYSEHealth Talk & YouCVS Caremark Corporation (CVS Caremark) is a pharmacy healthcare provider in the United States.

21eb0f5dc6dd6cfa74971bb4bda87412

deermcregmene

December 22, 2011, December 22, 2011 20:25, permalink

No rating. Login to rate!

BloombergFTR, GS, CVS, DHI, PM - Wednesday Notable Stocks with Volume at NYSEHealth Talk & YouCVS Caremark Corporation (CVS Caremark) is a pharmacy healthcare provider in the United States.

8afb24cbb37771d3604cf1b175fa9c58

boubjesee

November 27, 2011, November 27, 2011 07:40, permalink

No rating. Login to rate!

BloombergFTR, GS, CVS, DHI, PM - Wednesday Notable Stocks with Volume at NYSEHealth Talk & YouCVS Caremark Corporation (CVS Caremark) is a pharmacy healthcare provider in the United States.

F8060a0ec00043c539a3f143e282cf4f

elafDeema

November 27, 2011, November 27, 2011 19:40, permalink

No rating. Login to rate!

KOVAL ! why do you only respond to people who threaten to unsubscribe... what about me....Id like a shout out too ....I watched all your videos....TWICE.....i loved you when you weren't? famous.... *sigh*

E28d075f9b3c4daf5d9bac6dee5adfdf

reesetrumszet

November 29, 2011, November 29, 2011 20:39, permalink

No rating. Login to rate!

BloombergFTR, GS, CVS, DHI, PM - Wednesday Notable Stocks with Volume at NYSEHealth Talk & YouCVS Caremark Corporation (CVS Caremark) is a pharmacy healthcare provider in the United States.

339917717d728686e8dfa823242d13c8

SWASIAWOUMS

December 24, 2011, December 24, 2011 22:08, permalink

No rating. Login to rate!

BloombergFTR, GS, CVS, DHI, PM - Wednesday Notable Stocks with Volume at NYSEHealth Talk & YouCVS Caremark Corporation (CVS Caremark) is a pharmacy healthcare provider in the United States.

92e12544ec63b2bd1950b11f78628dff

addichaccipsy

December 28, 2011, December 28, 2011 11:02, permalink

No rating. Login to rate!

KOVAL ! why do you only respond to people who threaten to unsubscribe... what about me....Id like a shout out too ....I watched all your videos....TWICE.....i loved you when you weren't? famous.... *sigh*

945f35eccee794a84869465a3be349c5

FabsBlammaKah

December 28, 2011, December 28, 2011 20:22, permalink

No rating. Login to rate!

KOVAL ! why do you only respond to people who threaten to unsubscribe... what about me....Id like a shout out too ....I watched all your videos....TWICE.....i loved you when you weren't? famous.... *sigh*

Afe8e8f8d73ac17b6f156a28337a3495

PienWonee

December 29, 2011, December 29, 2011 01:52, permalink

No rating. Login to rate!

KOVAL ! why do you only respond to people who threaten to unsubscribe... what about me....Id like a shout out too ....I watched all your videos....TWICE.....i loved you when you weren't? famous.... *sigh*

0e18fa150b731e599d8dd32b5202ce82

Invoixmoott

December 29, 2011, December 29, 2011 21:36, permalink

No rating. Login to rate!

KOVAL ! why do you only respond to people who threaten to unsubscribe... what about me....Id like a shout out too ....I watched all your videos....TWICE.....i loved you when you weren't? famous.... *sigh*

56797e70a9cef3127328d0d7c5357efd

beistMuGseeks

December 29, 2011, December 29, 2011 22:13, permalink

No rating. Login to rate!

KOVAL ! why do you only respond to people who threaten to unsubscribe... what about me....Id like a shout out too ....I watched all your videos....TWICE.....i loved you when you weren't? famous.... *sigh*

46772bca7a2249035cec92de277c0391

Feriordedia

January 16, 2012, January 16, 2012 13:10, permalink

No rating. Login to rate!

KOVAL ! why do you only respond to people who threaten to unsubscribe... what about me....Id like a shout out too ....I watched all your videos....TWICE.....i loved you when you weren't? famous.... *sigh*

Afd225dd756e748c8504b59df06cef8d

Tawdielflyday

January 20, 2012, January 20, 2012 17:04, permalink

No rating. Login to rate!

KOVAL ! why do you only respond to people who threaten to unsubscribe. what about me....Id like a shout out too ....I watched all your videos....TWICE.....i loved you when you weren't? famous.... *sigh*

A121c48e6c9eef8adec2838944359955

KNEEDGINGTHOG

January 20, 2012, January 20, 2012 19:25, permalink

No rating. Login to rate!

KOVAL ! why do you only respond to people who threaten to unsubscribe... what about me....Id like a shout out too ....I watched all your videos....TWICE.....i loved you when you weren't? famous.... *sigh*

E43737dac874c9f246c79e66d3308fd8

papydiple

January 21, 2012, January 21, 2012 09:35, permalink

No rating. Login to rate!

KOVAL ! why do you only respond to people who threaten to unsubscribe... what about me....Id like a shout out too ....I watched all your videos....TWICE.....i loved you when you weren't? famous.... *sigh*

Your refactoring





Format Copy from initial code

or Cancel