首页 > 技术文章 > 把分类的select写在moden里做成一个组件 高洛峰

hnbiao 2015-11-09 11:41 原文

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调用输出:

 

推荐阅读