把分类的select写在moden里做成一个组件 高洛峰

function selectform($name="pid", $pid=0) {
            $data = $this->field('id, concat(path, "-", id) as abspath, name')->order('abspath, id')->select();
            
            $html = '';
            
            $html .= '<select name="'.$name.'">';

            $html .= '<option value="0">--根分类--</option>';
            foreach($data as $row) {
                //数量
                $num = count(explode('-', $row['abspath']))-2;
                //有一个数量 "-" 变成8个空格
                $space =  str_repeat("|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;", $num);
                
                $selected = ($pid == $row['id']) ? "selected" : '';


                $html .='<option '.$selected.' value="'.$row['id'].'">'.$space.'|-'.$row['name'].'</option>';
            }

            $html .='</select>';

            return $html;
        }

 

php:

html调用输出:

 

posted @ 2015-11-09 11:41  天涯alone  阅读(180)  评论(0编辑  收藏  举报