<?php
/* v.0.1 latest:2013/2/13 designed by 不惑仔
*/
class uix{
#另增預先選項？與 selected 互斥？
	public static function createOptionTags($pdo,$table,$mode=array(),$selectID=array(),$field=array()){
		if(empty($field['order'])) $field['order']='id';
		$sql="SELECT * FROM $table ORDER BY {$field['order']}";
		$stmt=$pdo->prepare($sql);
		$stmt->execute();
		if(empty($mode['optionOnly'])){
			if(empty($selectID['id'])) $selectID['id']='';
			if(empty($selectID['name'])) $selectID['name']=$selectID['id'];
			if(empty($selectID['label'])) $selectID['label']='';
		}
		if(empty($mode['rearLabel'])){
			echo self::createLabel($selectID['id'],$selectID['label']);
		}
		echo '<select id="'.$selectID['id'].'" name="'.$selectID['name'].'">';
		if(empty($field['label'])) $field['label']=1;
		if(empty($field['value'])) $field['value']=0;
		if(empty($field['default'])) $field['default']=-1;
		while($row=$stmt->fetch(PDO::FETCH_LAZY)){
			printf('<option value="%s"%s>%s</option>',$row[$field['value']],$row[$field['value']]==$field['default']?' selected':'',$row[$field['label']]);
		}
		echo '</select>';
		if(!empty($mode['rearLabel'])){
			self::createLabel($selectID['id'],$selectID['label']);
		}
	}
	#sprintf() is better?
	public static function createLabel($forID,$context,$class='',$tabindex=0){
		if(empty($class))
		 return "<label for='$forID' tabindex='$tabindex'>$context</label>";
		else
		 return "<label for='$forID' tabindex='$tabindex' class='$class'>$context</label>";
	}
}
?>