发布一个自己写的form类

生成各种表单格式和样式,以div+label+css进行布局。

< ?php class Form extends Base{ /*------------------------------------------- * 表单类 form.class * Author: corrie * 生成表单布局 --------------------------------------------*/ var $_formdata; var $_forminfo; var $_formarray; var $_index = 1; var $_signer = 1; function Form(){ $this->_set(); } function _set(){ $_formname = ‘field’; $_formprename = ‘prefield’; } function debug(){ $this->_debugshow($this->_sprint($this->_formarray)); } function _reset(){ $this->_formdata = “”; $this->_forminfo = array(); $this->_formarray = array(); $this->_index = 1; $this->_signer = 1; } function signer($index){ $this->_signer = $index; } function createelment($type, $name, $add=”, $value=”, $list=”, $signer=”){ if(!$signer || $signer==”)$signer=$this->_signer; if($name){ if(strexists($name, ‘[]’)){ $name = str_replace(‘[]’, ”, $name); $strname = “field[{$signer}][{$name}][]”; $prename = “prefield[{$signer}][{$name}][]”; $attrid = “”; }else{ $strname = “field[{$signer}][{$name}]”; $prename = “prefield[{$signer}][{$name}]”; $attrid = “id=’{$name}’”; //$attrid = “”; } $attrname = ‘name=”’.$strname.’”’; $preattrname = ‘name=”’.$prename.’”’; }else{ $attrname = ”; } $value && $preelement = ””; !$value && $preelement = ”; switch($type){ case “text”: $element = “{$list}”; break; case “password”: $element = “{$list}”; break; case “textarea”: $element = ””; break; case “file”: $preelement = ”; $element = ””; break; case “submit”: $preelement = ”; $element = ””; break; case “reset”: $preelement = ”; $element = ””; break; case “button”: $preelement = ”; $element = ””; break; case “checkbox”: $checked = ”; if(!$list){ $key = ‘1’; }else{ list($key, $item) = explode(‘,’, $list); if($key==$value){ $checked = “checked=’true’”; } } $element = ”{$item}”; break; case “radio”: $checked = ”; if(!$list){ $key = ‘1’; }else{ list($key, $item) = explode(‘,’, $list); if($key==$value){ $checked = “checked=’true’”; } } $element = ”{$item}”; break; case “select”: if(!is_array($list)){ if(!$list){ $key = ‘0’; $item = “No Items!”; }else{ list($key, $item) = explode(‘,’, $list); } $element = ””; }else{ $element = ””; } break; } return $preelement.$element; } function createone($type, $name, $add=”, $value=”, $list=”, $signer=”){ if(is_array($list)){ $one = ”; switch($type){ case “checkbox”: foreach($list as $k=>$v){ $one.= ””.$this->createelment($type, $name, $add, $value, “{$k},{$v}”, $signer).””; } break; case “radio”: foreach($list as $k=>$v){ $one.= ””.$this->createelment($type, $name, $add, $value, “{$k},{$v}”, $signer).””; } break; case “select”: $one = ””.$this->createelment($type, $name, $add, $value, $list, $signer).””; break; case “text”: foreach($list as $k=>$v){ if(is_array($value)){ $value = $value[$k]; } $one.= ””.$this->createelment($type, $name, $add, $value, “{$v}”, $signer).” “; } break; default: $one = ””.$this->createelment($type, $name, $add, $value, $list, $signer).””; } }else{ $one = ””.$this->createelment($type, $name, $add, $value, $list, $signer).””; } return $one; } function createline($arr){ $line = ”; //print_r($arr); foreach($arr as $k=>$v){ $line .= “$v”; } return $line; } function addtoline($arr, $title=”, $list=”, $add=”){ $line = ”; if(!$list){ $line.= ”
\n”; }else{ $line.= ”
\n”; } return $line; } function addtoplat($arr, $title=”, $add=”){ $plat = ”
”; //$plat.= join(“\n”, $arr); foreach($arr as $k=>$v){ $plat.= ”{$v}”; } if($title){ $plat.= ”
{$title}”; } $plat.= ”
”; return $plat; } function addtogroup($arr, $title=”, $add=”){ if($title==”) $group = ”
”; else $group = ”
{$title}
\n”; $group.= join(“\n”, $arr); $group.= ”
”; return $group; } function addtoform($arr, $title=”, $name=”, $action=’?’, $method=’post’, $add=”){ $form = ”
\n”; if($title!=”)$form.= ”
{$title}
\n”; $form.= join(“\n”, $arr); //$form.= $arr; $form.= ”
\n”; return $form; } function addline($array, $title=”, $intro=”){ $len = count($this->_formarray); $this->_formarray[$len][‘model’] = ‘line’; $this->_formarray[$len][‘elements’] = $array; $this->_formarray[$len][‘title’] = $title; $this->_formarray[$len][‘intro’] = $intro; return $len; } function addgroup(){ $arr = $args = func_get_args(); unset($arr[0], $arr[1]); $len = count($this->_formarray); $this->_formarray[$len][‘model’] = ‘group’; $this->_formarray[$len][‘title’] = $args[0]; $this->_formarray[$len][‘add’] = $args[1]; for($i=2; $i< =count($arr)+1; $i++){ $this->_formarray[$len][‘elements’][$i-2][‘model’] = ‘line’; $this->_formarray[$len][‘elements’][$i-2][‘elements’] = $arr[$i][0]; $this->_formarray[$len][‘elements’][$i-2][‘title’] = $arr[$i][1]; $this->_formarray[$len][‘elements’][$i-2][‘intro’] = $arr[$i][2]; } return $len; } function addplat($array, $intro=”, $add=”){ $len = count($this->_formarray); $this->_formarray[$len][‘model’] = ‘plat’; $this->_formarray[$len][‘elements’] = $array; $this->_formarray[$len][‘intro’] = $intro; $this->_formarray[$len][‘add’] = $add; return $len; } function addform($title=”, $name=”, $action=’?’, $method=’post’, $add=”){ //$this->_formdata = $this->addtoform($this->_formdata, $name, $title, $action, $add, $method); $this->_forminfo[‘name’] = $name; $this->_forminfo[‘title’] = $title; $this->_forminfo[‘action’] = $action; $this->_forminfo[‘add’] = $add; $this->_forminfo[‘method’] = $method; } function arraytoline($array, $title=”, $intro=”, $index=”){ $add = ”; if($index%2==0)$add = ‘odd’; foreach($array as $k=>$v){ $elems[] = $this->createone($v[0], $v[1], isset($v[2])?$v[2]:”, isset($v[3])?$v[3]:”, isset($v[4])?$v[4]:”, isset($v[5])?$v[5]:”); } $line = $this->addtoline($elems, $title, $intro, $add); //$this->_formdata.= $line; return $line; } function arraytoplat($array, $intro=”){ foreach($array as $k=>$v){ $elems[] = $this->createone($v[0], $v[1], isset($v[2])?$v[2]:”, isset($v[3])?$v[3]:”, isset($v[4])?$v[4]:”, isset($v[5])?$v[5]:”); } $line = $this->addtoplat($elems, $intro); return $line; //$this->_formdata.= $line; } function buildform($array=array(), $isgroup=false){ if(empty($array))$array = $this->_formarray; $lines = ”; foreach($array as $k=>$val){ if($val[‘model’]==’line’){ $line = $this->arraytoline($val[‘elements’], $val[‘title’], $val[‘intro’], $this->_index); $lines.= $line; $this->_index += 1; if($isgroup==true){ if(count($array)==$k+1){ return $lines; } }else{ $this->_formdata .= $line; } //$from.= $this->addtoline($array(‘type’)); }elseif($val[‘model’]==’plat’){ $this->_formdata .= $this->arraytoplat($val[‘elements’], $val[‘intro’], $val[‘add’]); }elseif($val[‘model’]==’group’){ $this->_formdata .= $this->addtogroup(array($this->buildform($val[‘elements’], true)), $val[‘title’], $val[‘add’]); //echo $this->addtogroup(array($this->buildform($val)), $val[‘title’]);; } } if(!empty($this->_forminfo)){ $info = $this->_forminfo; $this->_formdata = $this->addtoform(array($this->_formdata), $info[‘title’], $info[‘name’], $info[‘action’], $info[‘method’], $info[‘add’]); } return $this->_formdata; } } ?>

Comments