function cat_options($id, $parent_id = 0, $level = 0) { global $db, $prefix; $rows = $db->get_all("SELECT * FROM {$prefix}category WHERE parent_id = ? ORDER BY sort_order", array($parent_id)); foreach ($rows as $row) { $selected = $id == $row['id'] ? 'selected' : ''; echo "<option value=\"{$row[id]}\" {$selected} >" . str_repeat(' ', $level * 4) . "{$row[name]}</option>"; cat_options($id, $row['id'], $level + 1); } }
/** * 获得指定分类下的子分类的数组 * * @access public * @param int $cat_id 分类的ID * @param int $selected 当前选中分类的ID * @param boolean $re_type 返回的类型: 值为真时返回下拉列表,否则返回数组 * @param int $level 限定返回的级数。为0时返回所有级数 * @param int $is_show_all 如果为true显示所有分类,如果为false隐藏不可见分类。 * @return mix */ function cat_list($cat_id = 0, $selected = 0, $re_type = true, $level = 0, $is_show_all = true) { static $res = NULL; if ($res === NULL) { $data = read_static_cache('cat_pid_releate'); if ($data === false) { $sql = "SELECT c.cat_id, c.cat_name, c.measure_unit, c.parent_id, c.is_show, c.show_in_nav, c.grade, c.sort_order, COUNT(s.cat_id) AS has_children " . 'FROM ' . $GLOBALS['ecs']->table('category') . " AS c " . "LEFT JOIN " . $GLOBALS['ecs']->table('category') . " AS s ON s.parent_id=c.cat_id " . "GROUP BY c.cat_id " . 'ORDER BY c.parent_id, c.sort_order ASC'; $res = $GLOBALS['db']->getAll($sql); $sql = "SELECT cat_id, COUNT(*) AS goods_num " . " FROM " . $GLOBALS['ecs']->table('goods') . " WHERE is_delete = 0 AND is_on_sale = 1 " . " GROUP BY cat_id"; $res2 = $GLOBALS['db']->getAll($sql); $sql = "SELECT gc.cat_id, COUNT(*) AS goods_num " . " FROM " . $GLOBALS['ecs']->table('goods_cat') . " AS gc , " . $GLOBALS['ecs']->table('goods') . " AS g " . " WHERE g.goods_id = gc.goods_id AND g.is_delete = 0 AND g.is_on_sale = 1 " . " GROUP BY gc.cat_id"; $res3 = $GLOBALS['db']->getAll($sql); $newres = array(); foreach ($res2 as $k => $v) { $newres[$v['cat_id']] = $v['goods_num']; foreach ($res3 as $ks => $vs) { if ($v['cat_id'] == $vs['cat_id']) { $newres[$v['cat_id']] = $v['goods_num'] + $vs['goods_num']; } } } foreach ($res as $k => $v) { $res[$k]['goods_num'] = !empty($newres[$v['cat_id']]) ? $newres[$v['cat_id']] : 0; } //如果数组过大,不采用静态缓存方式 if (count($res) <= 1000) { write_static_cache('cat_pid_releate', $res); } } else { $res = $data; } } if (empty($res) == true) { return $re_type ? '' : array(); } $options = cat_options($cat_id, $res); // 获得指定分类下的子分类的数组 $children_level = 99999; //大于这个分类的将被删除 if ($is_show_all == false) { foreach ($options as $key => $val) { if ($val['level'] > $children_level) { unset($options[$key]); } else { if ($val['is_show'] == 0) { unset($options[$key]); if ($children_level > $val['level']) { $children_level = $val['level']; //标记一下,这样子分类也能删除 } } else { $children_level = 99999; //恢复初始值 } } } } /* 截取到指定的缩减级别 */ if ($level > 0) { if ($cat_id == 0) { $end_level = $level; } else { $first_item = reset($options); // 获取第一个元素 $end_level = $first_item['level'] + $level; } /* 保留level小于end_level的部分 */ foreach ($options as $key => $val) { if ($val['level'] >= $end_level) { unset($options[$key]); } } } if ($re_type == true) { $select = ''; foreach ($options as $var) { $select .= '<option value="' . $var['cat_id'] . '" '; $select .= $selected == $var['cat_id'] ? "selected='ture'" : ''; $select .= '>'; if ($var['level'] > 0) { $select .= str_repeat(' ', $var['level'] * 4); } $select .= htmlspecialchars(addslashes($var['cat_name']), ENT_QUOTES) . '</option>'; } return $select; } else { foreach ($options as $key => $value) { $options[$key]['url'] = build_uri('category', array('cid' => $value['cat_id']), $value['cat_name']); } return $options; } }
/** * 获得指定分类下的子分类的数组 * * @access public * @param int $cat_id 分类的ID * @param int $selected 当前选中分类的ID * @param boolean $re_type 返回的类型: 值为真时返回下拉列表,否则返回数组 * @param int $level 限定返回的级数。为0时返回所有级数 * @param int $is_show_all 如果为true显示所有分类,如果为false隐藏不可见分类。 * @return mix */ function cat_list($cat_id = 0, $selected = 0, $re_type = true, $level = 0, $is_show_all = true) { static $res = NULL; if ($res === NULL) { $sql = "SELECT c.*, COUNT(s.cat_id) AS has_children " . 'FROM ' . $GLOBALS['ecs']->table('category') . " AS c " . "LEFT JOIN " . $GLOBALS['ecs']->table('category') . " AS s ON s.parent_id=c.cat_id " . "GROUP BY c.cat_id " . 'ORDER BY parent_id, sort_order ASC'; $res = $GLOBALS['db']->getAllCached($sql); $sql = "SELECT c.cat_id as cat_id, COUNT(g.goods_id) AS goods_num " . 'FROM ' . $GLOBALS['ecs']->table('category') . " AS c " . "LEFT JOIN " . $GLOBALS['ecs']->table('goods') . " AS g ON g.cat_id=c.cat_id " . "GROUP BY c.cat_id "; $res2 = $GLOBALS['db']->getAllCached($sql); $newres = array(); foreach ($res2 as $k => $v) { $newres[$v['cat_id']] = $v['goods_num']; } foreach ($res as $k => $v) { $res[$k]['goods_num'] = $newres[$v['cat_id']]; } } if (empty($res) == true) { return $re_type ? '' : array(); } $options = cat_options($cat_id, $res); // 获得指定分类下的子分类的数组 $children_level = 99999; //大于这个分类的将被删除 if ($is_show_all == false) { foreach ($options as $key => $val) { if ($val['level'] > $children_level) { unset($options[$key]); } else { if ($val['is_show'] == 0) { unset($options[$key]); if ($children_level > $val['level']) { $children_level = $val['level']; //标记一下,这样子分类也能删除 } } else { $children_level = 99999; //恢复初始值 } } } } /* 截取到指定的缩减级别 */ if ($level > 0) { if ($cat_id == 0) { $end_level = $level; } else { $first_item = reset($options); // 获取第一个元素 $end_level = $first_item['level'] + $level; } /* 保留level小于end_level的部分 */ foreach ($options as $key => $val) { if ($val['level'] >= $end_level) { unset($options[$key]); } } } if ($re_type == true) { $select = ''; foreach ($options as $var) { $select .= '<option value="' . $var['cat_id'] . '" '; $select .= $selected == $var['cat_id'] ? "selected='ture'" : ''; $select .= '>'; if ($var['level'] > 0) { $select .= str_repeat(' ', $var['level'] * 4); } $select .= htmlspecialchars($var['cat_name'], ENT_QUOTES) . '</option>'; } return $select; } else { foreach ($options as $key => $value) { $options[$key]['url'] = build_uri('category', array('cid' => $value['cat_id']), $value['cat_name']); } return $options; } }
/** * 获得指定分类下的子分类的数组 * * @access public * @param int $cat_id 分类的ID * @param int $selected 当前选中分类的ID * @param boolean $re_type 返回的类型: 值为真时返回下拉列表,否则返回数组 * @param int $level 限定返回的级数。为0时返回所有级数 * @param int $is_show_all 如果为true显示所有分类,如果为false隐藏不可见分类。 * @return mix */ function cat_list($cat_id = 0, $selected = 0, $res, $res2, $re_type = true, $level = 0, $is_show_all = true) { //static $res = NULL; /* $db=D("cate"); if ($res === NULL) { $sql = "SELECT c.*, COUNT(s.cat_id) AS has_children FROM __TABLE__ AS c LEFT JOIN __TABLE__ AS s ON s.parent_id=c.cat_id GROUP BY c.cat_id ORDER BY parent_id, sort_order ASC "; $res =$db->query($sql); //dump($res); $sql = "SELECT c.cat_id as cat_id, COUNT(g.goods_id) AS goods_num FROM shuguang_cate AS c LEFT JOIN shuguang_goods AS g ON g.cat_id=c.cat_id GROUP BY c.cat_id "; $res2 = $db->query($sql); */ //dump($res2); //if ($res === NULL){ $newres = array(); foreach ($res2 as $k => $v) { $newres[$v['cat_id']] = $v['goods_num']; } foreach ($res as $k => $v) { $res[$k]['goods_num'] = $newres[$v['cat_id']]; } //} if (empty($res) == true) { return $re_type ? '' : array(); } $options = cat_options($cat_id, $res); // 获得指定分类下的子分类的数组 //dump($options); $children_level = 99999; //大于这个分类的将被删除 if ($is_show_all == false) { foreach ($options as $key => $val) { if ($val['level'] > $children_level) { unset($options[$key]); } else { if ($val['is_show'] == 0) { unset($options[$key]); if ($children_level > $val['level']) { $children_level = $val['level']; //标记一下,这样子分类也能删除 } } else { $children_level = 99999; //恢复初始值 } } } } /* 截取到指定的缩减级别 */ if ($level > 0) { if ($cat_id == 0) { $end_level = $level; } else { $first_item = reset($options); // 获取第一个元素 $end_level = $first_item['level'] + $level; } /* 保留level小于end_level的部分 */ foreach ($options as $key => $val) { if ($val['level'] >= $end_level) { unset($options[$key]); } } } if ($re_type == true) { $select = ''; foreach ($options as $var) { $select .= '<option value="' . $var['cat_id'] . '" '; $select .= $selected == $var['cat_id'] ? "selected='ture'" : ''; $select .= '>'; if ($var['level'] > 0) { $select .= str_repeat(' ', $var['level'] * 4); } $select .= htmlspecialchars($var['cat_name'], ENT_QUOTES) . '</option>'; } return $select; } else { // dump($options ); foreach ($options as $key => $value) { $options[$key]['url'] = build_uri('category', array('cid' => $value['cat_id']), $value['cat_name']); } return $options; } }
/** * 获得指定分类下的子分类的数组 * * @access public * @param int $cat_id 分类的ID * @param int $selected 当前选中分类的ID * @param boolean $re_type 返回的类型: 值为真时返回下拉列表,否则返回数组 * @param int $level 限定返回的级数。为0时返回所有级数 * @param int $is_show_all 如果为true显示所有分类,如果为false隐藏不可见分类。 * @return mix */ function cat_list($cat_id = 0, $selected = 0, $res, $res2, $re_type = true, $level = 0, $is_show_all = true) { $newres = array(); foreach ($res2 as $k => $v) { $newres[$v['cat_id']] = $v['goods_num']; } foreach ($res as $k => $v) { $res[$k]['goods_num'] = $newres[$v['cat_id']]; } //} if (empty($res) == true) { return $re_type ? '' : array(); } $options = cat_options($cat_id, $res); // 获得指定分类下的子分类的数组 //dump($options); $children_level = 99999; //大于这个分类的将被删除 if ($is_show_all == false) { foreach ($options as $key => $val) { if ($val['level'] > $children_level) { unset($options[$key]); } else { if ($val['is_show'] == 0) { unset($options[$key]); if ($children_level > $val['level']) { $children_level = $val['level']; //标记一下,这样子分类也能删除 } } else { $children_level = 99999; //恢复初始值 } } } } /* 截取到指定的缩减级别 */ if ($level > 0) { if ($cat_id == 0) { $end_level = $level; } else { $first_item = reset($options); // 获取第一个元素 $end_level = $first_item['level'] + $level; } /* 保留level小于end_level的部分 */ foreach ($options as $key => $val) { if ($val['level'] >= $end_level) { unset($options[$key]); } } } if ($re_type == true) { $select = ''; foreach ($options as $var) { $select .= '<option value="' . $var['cat_id'] . '" '; $select .= $selected == $var['cat_id'] ? "selected='ture'" : ''; $select .= '>'; if ($var['level'] > 0) { $select .= str_repeat(' ', $var['level'] * 4); } $select .= htmlspecialchars($var['cat_name'], ENT_QUOTES) . '</option>'; } return $select; } else { // dump($options ); foreach ($options as $key => $value) { $options[$key]['url'] = build_uri('category', array('cid' => $value['cat_id']), $value['cat_name']); } return $options; } }
/** * 获得指定分类下的子分类的数组 * * @access public * @param int $cat_id 分类的ID * @param int $selected 当前选中分类的ID * @param boolean $re_type 返回的类型: 值为真时返回下拉列表,否则返回数组 * @param int $level 限定返回的级数。为0时返回所有级数 * @param int $is_show_all 如果为true显示所有分类,如果为false隐藏不可见分类。 * @return mix */ function cat_list($cat_id = 0, $selected = 0, $re_type = true, $level = 0, $is_show_all = true) { static $res = NULL; if ($res === NULL) { $data = read_static_cache('cat_pid_releate'); if ($data === false) { $res = model('Base')->get_all_cat_list(); //如果数组过大,不采用静态缓存方式 if (count($res) <= 1000) { write_static_cache('cat_pid_releate', $res); } } else { $res = $data; } } if (empty($res) == true) { return $re_type ? '' : array(); } $options = cat_options($cat_id, $res); // 获得指定分类下的子分类的数组 $children_level = 99999; //大于这个分类的将被删除 if ($is_show_all == false) { foreach ($options as $key => $val) { if ($val['level'] > $children_level) { unset($options[$key]); } else { if ($val['is_show'] == 0) { unset($options[$key]); if ($children_level > $val['level']) { $children_level = $val['level']; //标记一下,这样子分类也能删除 } } else { $children_level = 99999; //恢复初始值 } } } } /* 截取到指定的缩减级别 */ if ($level > 0) { if ($cat_id == 0) { $end_level = $level; } else { $first_item = reset($options); // 获取第一个元素 $end_level = $first_item['level'] + $level; } /* 保留level小于end_level的部分 */ foreach ($options AS $key => $val) { if ($val['level'] >= $end_level) { unset($options[$key]); } } } if ($re_type == true) { $select = ''; foreach ($options AS $var) { $select .= '<option value="' . $var['cat_id'] . '" '; $select .= ($selected == $var['cat_id']) ? "selected='ture'" : ''; $select .= '>'; if ($var['level'] > 0) { $select .= str_repeat(' ', $var['level'] * 4); } $select .= htmlspecialchars(addslashes($var['cat_name']), ENT_QUOTES) . '</option>'; } return $select; } else { foreach ($options AS $key => $value) { $options[$key]['url'] = build_uri('category/index', array('id' => $value['cat_id'])); } return $options; } }
/** * 根据关键词搜索商品分类 * * @access public * * @return mix */ function search_cat() { if (empty($_POST['cat_name'])) { return; } $res = NULL; // 根据类别名称进行模糊查询 $sql = "SELECT c.cat_id, c.cat_name, c.measure_unit, c.parent_id, c.is_show, c.show_in_nav, c.grade, c.sort_order, COUNT(s.cat_id) AS has_children, 1 AS result " . 'FROM ' . $GLOBALS['ecs']->table('category') . " AS c " . "LEFT JOIN " . $GLOBALS['ecs']->table('category') . " AS s ON s.parent_id=c.cat_id " . "GROUP BY c.cat_id " . "HAVING c.cat_name LIKE '%" . $_POST['cat_name'] . "%' " . 'ORDER BY c.parent_id, c.sort_order ASC'; $res = $GLOBALS['db']->getAll($sql); // 查询所有类别 $sql = "SELECT c.cat_id, c.cat_name, c.measure_unit, c.parent_id, c.is_show, c.show_in_nav, c.grade, c.sort_order, COUNT(s.cat_id) AS has_children " . 'FROM ' . $GLOBALS['ecs']->table('category') . " AS c " . "LEFT JOIN " . $GLOBALS['ecs']->table('category') . " AS s ON s.parent_id=c.cat_id " . "GROUP BY c.cat_id " . 'ORDER BY c.parent_id, c.sort_order ASC'; $res1 = $GLOBALS['db']->getAll($sql); // 构建一个全分类的Map集合<cat_id, cat> $cat_map = array(); foreach ($res1 as $cat) { if (!empty($cat)) { $cat_id = $cat['cat_id']; $cat_map[$cat_id] = $cat; } } // 对商品类别进行排序 $res1 = cat_options(0, $res1); // 获取查询结果的上级所有父类别 $parents = array(); $cat_result = array(); foreach ($res as $cat) { $cat_result[$cat['cat_id']] = 1; array_push($parents, $cat); get_cat_parents($parents, $cat_map, $cat['cat_id']); } // 重构集合,只包含将来返回结果所包含的类别 $cat_map = array(); foreach ($parents as $cat) { $cat_map[$cat['cat_id']] = $cat; } // 移除与查询结果无关的类别 $res = array(); foreach ($res1 as $cat) { if (!empty($cat_map[$cat['cat_id']])) { // 标识出匹配查询条件的结果 if (empty($cat_result[$cat['cat_id']])) { $cat['is_result'] = 2; } else { $cat['is_result'] = 1; } array_push($res, $cat); } } file_put_contents("D:/php.debug", var_export($res, true)); return $res; }
/** * 获得指定分类下的子分类的数组 * * @access public * @param int $cat_id 分类的ID * @param int $selected 当前选中分类的ID * @param boolean $re_type 返回的类型: 值为真时返回下拉列表,否则返回数组 * @param int $level 限定返回的级数。为0时返回所有级数 * @param int $is_show_all 如果为true显示所有分类,如果为false隐藏不可见分类。 * @return mix */ function cat_list($cat_id = 0, $selected = 0, $re_type = true, $level = 0, $is_show_all = true) { static $res = NULL; if ($res === NULL) { $data = read_static_cache('cat_pid_releate'); if ($data === false) { $res = M('category')->alias('c')->field('c.cat_id, c.cat_name, c.measure_unit, c.parent_id, c.is_show, c.show_in_nav, c.grade, c.sort_order, COUNT(s.cat_id) AS has_children')->join(C('DB_PREFIX') . 'category s ON s.parent_id=c.cat_id')->group('c.cat_id')->order('c.parent_id, c.sort_order ASC')->select(); $res2 = M('goods')->field('cat_id, COUNT(*) AS goods_num')->where(array('is_delete' => 0, 'is_on_sale' => 1))->group('cat_id')->select(); $res3 = M('goods_cat')->alias('gc')->field('gc.cat_id, COUNT(*) AS goods_num')->join(C('DB_PREFIX') . 'goods g ON g.goods_id = gc.goods_id')->where(array('g.goods_id = gc.goods_id AND g.is_delete = 0 AND g.is_on_sale = 1'))->select(); $newres = array(); foreach ($res2 as $k => $v) { $newres[$v['cat_id']] = $v['goods_num']; foreach ($res3 as $ks => $vs) { if ($v['cat_id'] == $vs['cat_id']) { $newres[$v['cat_id']] = $v['goods_num'] + $vs['goods_num']; } } } foreach ($res as $k => $v) { $res[$k]['goods_num'] = !empty($newres[$v['cat_id']]) ? $newres[$v['cat_id']] : 0; } //如果数组过大,不采用静态缓存方式 if (count($res) <= 1000) { write_static_cache('cat_pid_releate', $res); } } else { $res = $data; } } if (empty($res) == true) { return $re_type ? '' : array(); } $options = cat_options($cat_id, $res); // 获得指定分类下的子分类的数组 $children_level = 99999; //大于这个分类的将被删除 if ($is_show_all == false) { foreach ($options as $key => $val) { if ($val['level'] > $children_level) { unset($options[$key]); } else { if ($val['is_show'] == 0) { unset($options[$key]); if ($children_level > $val['level']) { $children_level = $val['level']; //标记一下,这样子分类也能删除 } } else { $children_level = 99999; //恢复初始值 } } } } /* 截取到指定的缩减级别 */ if ($level > 0) { if ($cat_id == 0) { $end_level = $level; } else { $first_item = reset($options); // 获取第一个元素 $end_level = $first_item['level'] + $level; } /* 保留level小于end_level的部分 */ foreach ($options as $key => $val) { if ($val['level'] >= $end_level) { unset($options[$key]); } } } if ($re_type == true) { $select = ''; foreach ($options as $var) { $select .= '<option value="' . $var['cat_id'] . '" '; $select .= $selected == $var['cat_id'] ? "selected='ture'" : ''; $select .= '>'; if ($var['level'] > 0) { $select .= str_repeat(' ', $var['level'] * 4); } $select .= htmlspecialchars(addslashes($var['cat_name']), ENT_QUOTES) . '</option>'; } return $select; } else { foreach ($options as $key => $value) { $options[$key]['url'] = build_uri('category', array('cid' => $value['cat_id']), $value['cat_name']); } return $options; } }
<div class="main-div"> <form action="?act=<?php echo $form_act; ?> " method="post"> <input type="hidden" name="id" value="<?php echo $id; ?> " /> <table width="100%" id="general-table"> <tr> <td class="label">上级分类</td> <td><select name="parent_id" id="parent_id" tabindex="1"> <option value="0">顶级分类</option> <?php cat_options($parent_id); ?> </select></td> </tr> <tr> <td class="label">分类名称</td> <td><input type="text" name="name" id="name" tabindex="2" value="<?php echo $name; ?> " /></td> </tr> <tr> <td class="label">跳转链接</td> <td><input type="text" name="url" id="url" tabindex="3" value="<?php echo $url; ?>
<div class="main-div"> <form action="?act=<?php echo $form_act; ?> " method="post"> <input type="hidden" name="uid" value="<?php echo $uid; ?> " /> <table width="100%" id="general-table"> <tr> <td class="label">网站分类:</td> <td><select name="cat_id" id="cat_id" tabindex="1"> <option value="0">顶级分类</option> <?php echo cat_options($cat_id); ?> </select></td> </tr> <tr> <td class="label">网站名称:</td> <td><input type="text" name="title" id="title" tabindex="2" value="<?php echo $title; ?> " /></td> </tr> <tr> <td class="label">网站地址:</td> <td><input type="text" name="url" id="url" tabindex="3" value="<?php echo $url ? $url : 'http://'; ?>