public function add($id = '') { require APPPATH . 'libraries/tree.class.php'; if ($id) { $query = $this->db->where(array('catid' => $id))->get('category')->row_array(); } $this->data['item'] = $query; $categorys = $this->categorys; $tree = new tree(); $tree->icon = array(' │ ', ' ├─ ', ' └─ '); $tree->nbsp = ' '; $string = '<select name="parentid">'; $string .= "<option value='0'>≡ 作为一级栏目 ≡</option>"; $str = "<option value='\$catid' \$selected>\$spacer \$catname</option>;"; $str2 = "<optgroup label='\$spacer \$catname'></optgroup>"; $tree->init($categorys); $string .= $tree->get_tree_category(0, $str, $str2, $query['parentid']); $string .= '</select>'; $this->data['categorys'] = $string; $this->repair(false); $this->load->view('category/add', $this->data); }
/** * 栏目选择 * @param string $file 栏目缓存文件名 * @param intval/array $catid 别选中的ID,多选是可以是数组 * @param string $str 属性 * @param string $default_option 默认选项 * @param intval $modelid 按所属模型筛选 * @param intval $type 栏目类型 * @param intval $onlysub 只可选择子栏目 * @param intval $siteid 如果设置了siteid 那么则按照siteid取 */ public static function select_category($file = '', $catid = 0, $str = '', $default_option = '', $modelid = 0, $type = -1, $onlysub = 0, $siteid = 0, $is_push = 0) { require_once "Tree.class.php"; $tree = new tree(); if (!$siteid) { $siteid = get_siteid(); } if ($modelid) { $result = D('Category')->where('siteid = %d and modelid = %d', $siteid, $modelid)->select(); } else { $result = D('Category')->where('siteid = %d', $siteid)->select(); } $string = '<select ' . $str . '>'; if ($default_option) { $string .= "<option value='0'>{$default_option}</option>"; } if (is_array($result)) { foreach ($result as $r) { if ($siteid != $r['siteid'] || $type >= 0 && $r['type'] != $type) { continue; } $r['selected'] = ''; if (is_array($catid)) { $r['selected'] = in_array($r['id'], $catid) ? 'selected' : ''; } elseif (is_numeric($catid)) { $r['selected'] = $catid == $r['id'] ? 'selected' : ''; } $r['html_disabled'] = "0"; if (!empty($onlysub) && $r['child'] != 0) { $r['html_disabled'] = "1"; } $categorys[$r['id']] = $r; if ($modelid && $r['modelid'] != $modelid) { unset($categorys[$r['catid']]); } } } $str = "<option value='\$id' \$selected>\$spacer \$catname</option>;"; $str2 = "<optgroup label='\$spacer \$catname'></optgroup>"; $tree->init($categorys); $string .= $tree->get_tree_category(0, $str, $str2); $string .= '</select>'; return $string; }