2011年5月17日 星期二

cweb_select

class cweb_select {
    //put your code here

    var $name;
    var $option;
    var $selected;

    function cweb_select($name = "")
    {
        $this->option = array();
        $this->selected = null;
        $this->name = $name;
    }

    function insert($row /*array or object*/ )
    {
        foreach ($row as $key => $value )
        {
            $this->insertItem($key, $value);
        }
    }

    function insert_only_value($row /*array or object*/ )
    {
        foreach ($row as $value )
        {
            $this->insertItem($value, $value);
        }
    }

    function insertItem($value , $text)
    {
        $this->option[$value] = $text;
    }

    function selected($value)
    {
        $this->selected = $value;
    }

    function getText($value)
    {
        return $this->option[$value];
    }

    function outHtml()
    {
        $result = "";


        foreach( $this->option as $value => $text )
        {
            $selected = "";
            if( $this->selected == $value)
                $selected = " selected";

            $result .= "<option value='{$value}'{$selected}>{$text}</option>";
        }

        $result = "<select name='{$this->name}' >{$result}</select>";

        return $result;
    }
}


沒有留言:

張貼留言