|
 |
13³â Àü |
php5¿ëÀÔ´Ï´Ù
<?php
class SelectBox{
private $name;
private $selectedIndex;
private $selectedFromValue;
private $attribute;
private $option = array();
public function __construct($name, $attribute=""){
$this->name = $name;
$this->attribute = $attribute;
}
public function __get($name){
try{
$this->checkProperty($name);
return $this->$name;
}catch(Exception $e){
echo $e->getMessage();
}
}
public function __set($name, $value){
try{
$this->checkProperty($name);
$this->$name = $value;
}catch(Exception $e){
echo $e->getMessage();
}
}
public function add($value, $text){
$this->option[$value] = $text;
}
public function getTag(){
$i=0;
$str = "<select name='".$this->name."' ".$this->attribute.">\n";
foreach($this->option as $key=>$value){
if($this->selectedIndex == $i) $selected = " selected='selected'";
else if($this->selectedFromValue == $key) $selected = " selected='selected'";
else $selected = "";
$str .= "<option value='".$key."'".$selected.">".$value."</option>\n";
$i++;
}
$str .= "</select>";
return $str;
}
private function checkProperty($name){
if(!property_exists(__CLASS__, $name)){
throw new Exception('Á¸ÀçÇÏÁö ¾Ê´Â Property['.$name.']¸¦ È£ÃâÇÏ¿´½À´Ï´Ù<br />');
}
}
}
?>
<»ç¿ë¹ý>
$select = new SelectBox('superUser', 'style="color:#ff00ff"');
$select->selectedIndex = 1; //¼±ÅÃµÈ »óÅÂÀÇ option À妽º
$select->add('admin', '°ü¸®ÀÚ');
$select->add('master', 'ºÎ°ü¸®ÀÚ');
$select->add('guest', '¼Õ´Ô');
echo $select->getTag();
|
|
̵̧ : 641 |
̵̧
¸ñ·Ï
|
|