kfrico 发表于 2008-9-30 17:14:19

form_radio() 有辦法用變數預設嗎

echo '狀態: ';
echo form_radio('Status', 'a',FALSE);
echo 'a';
echo form_radio('Status', 'b',FALSE);
echo 'b';
echo form_radio('Status', 'c',FALSE);

有辦法在讀取資料庫回來之後半斷數據是a.b.c那一個
並在那一個預設打勾起來嗎?

Hex 发表于 2008-9-30 17:44:24

我发现你很喜欢用 form helper ,我不推荐这样做。
直接用 HTML + Javascript 实现。

itlong 发表于 2008-9-30 19:21:31

class My_Html{
      public function My_Html(){
      }
        /**
       *功能:输出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;
                }
        }
}
上面这个类保存在application/library里,
在controller里:
$this->load->library('html');
$this->html->makeFormRadio('name',array('one'=>'one', 'tow'=>'row'), 'tow');

kfrico 发表于 2008-9-30 21:21:38

form helper 不夠好嗎?
會喜歡用是因為用form helper 程式碼也會比較簡短一點
至於Javascript是真的不太懂所以才會想知道有沒有別的辦法!!

itlong這是妳自己寫的library嗎?

有沒更俱體的用法
在在controller
$this->load->library('html');
$this->html->makeFormRadio('name',array('one'=>'one', 'tow'=>'row'), 'tow');
那該怎麼傳進去view裡呢

為什麼我出現An Error Was Encountered
Unable to load the requested class: Html
不能裝載呢
是因為我的/system/libraries/下,不存在class CI_Html嗎?
我照你上面打的會有錯嗎?
$this->checkInArray
這函數是從那調用的?

[ 本帖最后由 kfrico 于 2008-9-30 23:48 编辑 ]

itlong 发表于 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 versionx.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>
* @copyright2008-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 动作,如:onclick="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;
                }
        }

}
?>
页: [1]
查看完整版本: form_radio() 有辦法用變數預設嗎