Created
March 28, 2014 06:37
-
-
Save netkiller/9826647 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Form{ | |
private $forms = array(); | |
private $element = array(); | |
private $attributes = array(); | |
public $id,$name,$method,$action; | |
public function Form($id = '', $name = '', $method = 'post', $action = ''){ | |
$this->id = $id; | |
$this->name = $name; | |
$this->method = $method; | |
$this->action = $action; | |
//<fieldset><legend>jjjj</legend></fieldset> | |
} | |
public function addField($label, $type, $name, $value){ | |
$this->forms[] = array('label' => $label,'type' => $type, 'name' => $name, 'value' => $value); | |
} | |
public function createElement(){ | |
} | |
public function addElement(){ | |
} | |
public function addHidden($name, $value){ | |
$this->forms[] = array('type' => 'hidden', 'name' => $name, 'value' => $value); | |
} | |
public function addTextArea($label,$name, $value){ | |
$this->forms[] = array('label' => $label,'type' => 'textarea', 'name' => $name, 'value' => $value); | |
} | |
public function addCheckBox($label,$name, $value, $checked = false){ | |
$this->forms[] = array('label' => $label,'type' => 'checkbox', 'name' => $name, 'value' => $value, 'checked' => $checked); | |
} | |
public function addRadio($label,$name, $value, $checked = false){ | |
$this->forms[] = array('label' => $label,'type' => 'radio', 'name' => $name, 'value' => $value, 'checked' => $checked); | |
} | |
public function addSelect($label,$name, $options){ | |
$this->forms[] = array('label' => $label,'type' => 'select', 'name' => $name, 'options' => $options); | |
} | |
public function addImage($label,$name, $src, $alt = '', $align = 'left'){ | |
$this->forms[] = array('label' => $label,'type' => 'image', 'name' => $name, 'src' => $src, 'alt' => $alt); | |
} | |
public function addFile($label,$name, $src = ''){ | |
$this->forms[] = array('label' => $label,'type' => 'file', 'name' => $name, 'src' => $src, 'alt' => $alt); | |
} | |
public function addButton($type = 'button', $name, $value){ | |
$this->forms[] = array('type' => $type, 'name' => $name, 'value' => $value); | |
} | |
public function render(){ | |
$fields = array(); | |
$fields[] = '<form id="'.$this->id.'" name="'.$this->name.'" method="'.$this->method.'" action="'.$this->action.'">'; | |
foreach($this->forms as $type => $element){ | |
$field = ''; | |
if($element['label']){ | |
$field = '<label for="'.$element['name'].'">'.$element['label'].'</label>'; | |
} | |
switch($element['type']){ | |
case 'hidden': | |
$field = '<input type="'.$element['type'].'" name="'.$element['name'].'" value="'.$element['value'].'"/>'; | |
break; | |
case 'textarea': | |
$field .= '<textarea name="'.$element['name'].'">'.$element['value'].'</textarea>'; | |
break; | |
case 'radio': | |
case 'checkbox': | |
if($element['checked']){ | |
$checked = 'checked="checked"'; | |
}else{ | |
$checked = ''; | |
} | |
$field .= '<input name="'.$element['name'].'" type="'.$element['type'].'" id="'.$element['name'].'" value="'.$element['value'].'" '.$checked.' />'; | |
break; | |
case 'select': | |
$field .= '<select name="'.$element['name'].'" id="'.$element['name'].'">'; | |
foreach ($element['options'] as $option){ | |
$field .= '<option value="'.$option[1].'" '.($option[2]?'selected="selected"':'').'>'.$option[0].'</option>'; | |
} | |
$field .= '</select>'; | |
break; | |
case 'image': | |
$field .= '<input name="'.$element['name'].'" type="'.$element['type'].'" id="'.$element['name'].'" src="'.$element['src'].'" alt="'.$element['alt'].'" align="'.$element['align'].'" />'; | |
break; | |
default: | |
$field .= '<input type="'.$element['type'].'" name="'.$element['name'].'" value="'.$element['value'].'"/>'; | |
} | |
$fields[] = $field; | |
} | |
$fields[] = '</form>'; | |
return implode("\r\n",$fields); | |
} | |
public function display(){ | |
print($this->render()); | |
} | |
}; | |
final class SmartForm{ | |
private $forms = array(); | |
private $elements = array(); | |
private $attributes = array(); | |
function __construct($id, $name = null, $methed = null) { | |
$this->elements = array('form' => array()); | |
$this->attributes = array('id' => $id, 'name' => $name?$name:$id); | |
} | |
function __destruct() { | |
} | |
public function attr($key, $value){ | |
} | |
public function select($id, $name = null){ | |
$this->elements = array('select' => array()); | |
$this->attributes = array('id' => $id, 'name' => $name?$name:$id); | |
return $this; | |
} | |
public function render(){ | |
$tag = key($this->elements); | |
foreach ($this->attributes as $key=>$value){ | |
$attr .= $key . '=' . $value . ' '; | |
} | |
$html = '<select ' . $attr . '>'; | |
foreach($this->elements['select'] as $k=>$v){ | |
$html .= '<option value='.$v.'>'.$k.'</option>'; | |
} | |
$html .='</select>'; | |
echo($html); | |
} | |
public function option($key, $value){ | |
$this->elements['select'][] = array($key, $value); | |
return $this; | |
} | |
} | |
$form = new Form('test','test','get'); | |
//$form->createElement(); | |
//$form->addElement('Label','text', 'chen', 'chen'); | |
$form->addField('Label','text', 'chen', 'chen'); | |
$form->addTextArea('Notes','text', 'sfasfasfasfafchen'); | |
$form->addCheckBox('Linux','os','T'); | |
$form->addCheckBox('Freebsd','os','F',true); | |
$form->addRadio('Boy','sex','T'); | |
$form->addRadio('Girl','sex','F',true); | |
$form->addSelect('Language','lang', array(array('CN','cn',false),array('EN','cn',true),array('HK','cn',false))); | |
$form->addImage('chen','ok','chen.jpg','OK'); | |
$form->addFile('file','file'); | |
$form->addButton('reset','ss','Reset'); | |
$form->addButton('submit','ss','Submit'); | |
$form->addButton('button','ok','Hello world'); | |
$form->addHidden('user_id', '10'); | |
//$form->display(); | |
//$form->render(); | |
$form = new SmartForm(); | |
$form->select('city')->option('url','neo.com')->render(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment