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 !
michal-pochwala.myopenid.com
August 26, 2010, August 26, 2010 09:07, permalink
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;
weekly jobnews fuel
December 2, 2010, December 02, 2010 23:48, permalink
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
flellareire
September 4, 2011, September 04, 2011 11:51, permalink
Thanks mate... just dropped by.!!!!! Will look for BIKE STN when we get to Seattle. Still in Buenos Airies.!!!!!
NarbrommagDug
September 7, 2011, September 07, 2011 05:57, permalink
Thanks mate... just dropped by.!!!!! Will look for BIKE STN when we get to Seattle. Still in Buenos Airies.!!!!!
Cacralley
September 20, 2011, September 20, 2011 21:24, permalink
Thanks mate... just dropped by.!!!!! Will look for BIKE STN when we get to Seattle. Still in Buenos Airies.!!!!!
rorkedmedia
November 1, 2011, November 01, 2011 12:16, permalink
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*
Deannyten
November 15, 2011, November 15, 2011 17:11, permalink
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*
vostenofs
November 16, 2011, November 16, 2011 14:48, permalink
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.
Peeleaskirl
November 19, 2011, November 19, 2011 19:00, permalink
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.
nocaoccub
November 12, 2011, November 12, 2011 07:59, permalink
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.
Envivenombiva
November 17, 2011, November 17, 2011 21:23, permalink
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.
erunnyutesy
November 15, 2011, November 15, 2011 07:43, permalink
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.
Rilsotbloth
November 20, 2011, November 20, 2011 18:12, permalink
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.
deermcregmene
December 22, 2011, December 22, 2011 20:25, permalink
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.
boubjesee
November 27, 2011, November 27, 2011 07:40, permalink
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.
elafDeema
November 27, 2011, November 27, 2011 19:40, permalink
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*
reesetrumszet
November 29, 2011, November 29, 2011 20:39, permalink
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.
SWASIAWOUMS
December 24, 2011, December 24, 2011 22:08, permalink
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.
addichaccipsy
December 28, 2011, December 28, 2011 11:02, permalink
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*
FabsBlammaKah
December 28, 2011, December 28, 2011 20:22, permalink
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*
PienWonee
December 29, 2011, December 29, 2011 01:52, permalink
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*
Invoixmoott
December 29, 2011, December 29, 2011 21:36, permalink
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*
beistMuGseeks
December 29, 2011, December 29, 2011 22:13, permalink
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*
Feriordedia
January 16, 2012, January 16, 2012 13:10, permalink
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*
Tawdielflyday
January 20, 2012, January 20, 2012 17:04, permalink
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*
KNEEDGINGTHOG
January 20, 2012, January 20, 2012 19:25, permalink
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*
This method outputs a standard HTML select menu. Options are provided as an array argument.