| 
 | 
 
 
发表于 2008-10-1 11:33:05
|
显示全部楼层
 
 
 
是的,这是一个人写的容易使用的html form组件,这完全可以自己写,看自己方便如何使用罢了。 
要在view中用到这个类,你要先明白view和controller的关系。 
在MVC的思想中,一般情况下,在VIEW 中不直接使用对象来完成工作,view只是一个显示html构适,是提供给controller来渲染填充数据然后输出,所以尽量不要在view中完成一些复杂的逻辑工作。如果确实要完成一些简单的函数输出,一般情况是用到助手,助手函手就是专门为view而做的。form helper就是一个好简单的函数,你完全可以模仿它,写自己需求的函数。 
 
而我下面的类,就是一个生成html form组件的简单类,思想是先在controller里调用,生成组件,然后再用来填充view. 
完代码如下: 
<?php 
 
/** 
 * HTML OBJECT 
 * 
 * make the html object ,such as checkbox,radio,select... 
 * 
 * PHP versions 4 and 5 
 * 
 * LICENSE: This source file is subject to version  x.x.x of the PHP license 
 * that is available through the world-wide-web at the following URI: 
 * http://www.itlong.com/x.x.x.txt.  If you did not receive a copy of 
 * the PHP License and are unable to obtain it through the web, please 
 * send a note to itlongtom@tom.com so we can mail you a copy immediately. 
 * 
 * @category   (CategoryName) 
 * @package    (PackageName) 
 * @author     Itlong <itlongtom@tom.com> 
 * @copyright  2008-2010 Itlong 
 * @license    http://www.itlong.com/x.x.x.txt PHP License x.x.x 
 * @version    CVS: $Id  
 * @link       http://pear.php.net/package/PackageName 
 * @see        no message. 
 * @since      File available since Release x.x.x 
 * @deprecated File deprecated in Release x.x.x 
 * @date       2008-4-3 17:51 
 */ 
class My_Html 
{ 
        public function __construct() 
        { 
                //TODO. 
        } 
        /** 
         *功能:检查一个变量是否在一数组里面 
         *参数 arr是数组 
         *参数 val是要检查的变量 
         *返回值:返回布尔值 
         */ 
        private function checkInArray($arr, $val) 
        { 
                /* 
                if(empty($arr) or empty($val)) 
                { 
                        return false; 
                } 
                */ 
                if(is_array($arr)) 
                { 
                        return in_array($val, $arr); 
                }else 
                { 
                        return $arr == $val; 
                } 
        } 
        /** 
         *功能:输出文本框 
         *参数 name 为文本框的名称 
         *参数 value 为文本框的值 
         *参数 size为文本框的长度 
         *参数 event为文本框的javascript 动作,如 nclick="function()" 
         *参数 type 为text或hidden 
         *参数 style为CSS 
         *返回值:返回字符串 
         */ 
        public function makeFormText($name, $value = '', $size = 10, $event = '', $type = 'text', $style = '') 
        { 
                if(!empty($name)) 
                { 
                        return sprintf("<input type=\"%s\" name=\"%s\" size=\"%d\" value=\"%s\" %s $style />", $type, $name, $size, $value, $event, $style); 
                } 
        } 
        public function makeFormTextArea($name, $value = '', $rows = 10, $cols = 40, $event = '', $style = '') 
        { 
                if(!empty($name)) 
                { 
                        return sprintf("<textarea name=\"%s\" rows=\"%d\" cols=\"%d\" %s %s>%s</textarea>", $name, $rows, $cols, $event, $style, $value); 
                } 
        } 
        /** 
         *功  能:这里下拉列表的OPTION输出函数 
         *参  数 value为下拉列表的value 
         *参  数:$text为下拉列表的文字部分 
         *参  数:$selected为判断是否选中的开关 
         *返回值:返回一个option项 
         */ 
        private function outOption($value, $text, $selected) 
        { 
                return sprintf("<option value=\"%s\" %s>%s</option>", $value, $selected?"selected":"", $text); 
        } 
        public function makeFormSelect($name, $array, $selected, $event = '', $style = '') 
        { 
                if(!empty($name)) 
                { 
                        $select = "<select size=\"1\" name=\"$name\" $event $style>"; 
                        foreach($array as $value=>$text) 
                        { 
                                $select .= $this->outOption($value, $text, $this->checkInArray($value, $selected)); 
                        } 
                        $select .= "</select>"; 
                } 
                return $select; 
        } 
        /** 
         *功能:输出radio选项 
         *参数:$text为项前说明,$name为radio名,$value为radio值,$checked为是否选中开关 
         *返回:返回radio字符串 
         */ 
        private function outRadio($text, $name, $value, $checked, $event = '', $style = '') 
        { 
                return sprintf("  %s<input type=\"radio\" name=\"%s\" value=\"%s\" %s %s %s/>",$text, $name, $value, $checked?"checked":"", $event, $style); 
        } 
        public function makeFormRadio($name, $array, $checked, $event = '', $style = '') 
        { 
                if(!empty($name)) 
                { 
                        $radio = ''; 
                        foreach($array as $value => $text) 
                        { 
                                $radio .=$this->outRadio($text, $name, $value, $this->checkInArray($value, $checked), $event, $style); 
                        } 
                        return $radio; 
                } 
        } 
        /** 
         *功能:输出checkbox控件 
         *参数:$name是checkbox名,$value为控制的值,$checkbox是否选中开关,$event是执行的javascript函数,$style是CSS 
         *返回值:字符串 
         */ 
        private function outCheckbox($name, $value, $text, $checked, $event = '', $style = '') 
        { 
                return sprintf(" %s<input type=\"checkbox\" name=\"%s\" value=\"%s\" %s %s $s />", $text, $name, $value, $check?"checked":"", $event, $style); 
        } 
        public function makeFormCheckbox($name, $array, $checked,$event = '', $style = '') 
        { 
                if(!empty($name)) 
                { 
                        $checkbox = ''; 
                        foreach($array as $value=>$text) 
                        { 
                                $checkbox .=$this->outCheckbox($name, $value, $text, $this->checkInArray($value, $checked), $evant, $style); 
                        } 
                        return $checkbox; 
                } 
        } 
 
} 
?> |   
 
 
 
 |