commit | author | age
|
3e083b
|
1 |
<?php |
B |
2 |
|
|
3 |
/** |
|
4 |
* 选购中心 |
|
5 |
*/ |
|
6 |
|
|
7 |
define('IN_ECS', true); |
|
8 |
|
|
9 |
require(dirname(__FILE__) . '/includes/init.php'); |
|
10 |
|
|
11 |
$condition = array(); |
|
12 |
$picks = array(); |
|
13 |
$cat_id = !empty($_GET['cat_id']) ? intval($_GET['cat_id']) : 0; |
|
14 |
if (!empty($_GET['attr'])) |
|
15 |
{ |
|
16 |
foreach($_GET['attr'] as $key => $value) |
|
17 |
{ |
|
18 |
if (!is_numeric($key)) |
|
19 |
{ |
|
20 |
unset($_GET['attr'][$key]); |
|
21 |
continue; |
|
22 |
} |
|
23 |
$key = intval($key); |
|
24 |
$_GET['attr'][$key] = htmlspecialchars($value); |
|
25 |
} |
|
26 |
} |
|
27 |
|
|
28 |
if (empty($cat_id)) |
|
29 |
{ |
|
30 |
/* 获取所有符合条件的商品类型 */ |
|
31 |
$sql = "SELECT DISTINCT t.cat_id, t.cat_name " . |
|
32 |
"FROM " . $ecs->table('goods_type') . " AS t, " . $ecs->table('attribute') . " AS a, " . $ecs->table('goods_attr') . " AS g " . |
|
33 |
"WHERE t.cat_id = a.cat_id AND a.attr_id = g.attr_id AND t.enabled = 1"; |
|
34 |
$rs = $db->query($sql); |
|
35 |
|
|
36 |
$in_cat = array(); |
|
37 |
$cat_name = array(); |
|
38 |
$in_goods = ''; |
|
39 |
|
|
40 |
while ($row = $db->fetchRow($rs)) |
|
41 |
{ |
|
42 |
$condition[$row['cat_id']]['name'] = $row['cat_name']; |
|
43 |
$in_cat[] = $row['cat_id']; |
|
44 |
} |
|
45 |
|
|
46 |
$in_cat = "AND a.cat_id ".db_create_in($in_cat); |
|
47 |
|
|
48 |
/* 获取符合条件的属性 */ |
|
49 |
$sql = "SELECT DISTINCT a.attr_id ". |
|
50 |
"FROM ".$ecs->table('goods_attr')." AS g, ".$ecs->table('attribute') ." AS a ". |
|
51 |
"WHERE a.attr_id = g.attr_id " . $in_cat; |
|
52 |
|
|
53 |
$in_attr = $db->getCol($sql); //符合条件attr_id; |
|
54 |
$in_attr = 'AND g.attr_id '.db_create_in($in_attr); |
|
55 |
|
|
56 |
/* 获取所有属性值 */ |
|
57 |
$sql = "SELECT DISTINCT g.attr_id, a.attr_name, a.cat_id, g.attr_value". |
|
58 |
" FROM ".$ecs->table('goods_attr')." AS g, ". |
|
59 |
$ecs->table('attribute') ." AS a". |
|
60 |
" WHERE a.attr_id = g.attr_id ".$in_attr." ORDER BY cat_id"; |
|
61 |
$rs = $db->query($sql); |
|
62 |
|
|
63 |
while ($row = $db->fetchRow($rs)) |
|
64 |
{ |
|
65 |
if (empty($condition[$row['cat_id']]['cat'][$row['attr_id']]['cat_name'])) |
|
66 |
{ |
|
67 |
$condition[$row['cat_id']]['cat'][$row['attr_id']]['cat_name'] = $row['attr_name']; |
|
68 |
} |
|
69 |
|
|
70 |
$condition[$row['cat_id']]['cat'][$row['attr_id']]['list'][] = array('name'=>$row['attr_value'], 'url'=>'pick_out.php?cat_id='.$row['cat_id'].'&attr['.$row['attr_id'].']='.urlencode($row['attr_value'])); |
|
71 |
} |
|
72 |
|
|
73 |
/* 获取商品总数 */ |
|
74 |
$goods_count = $db->GetOne("SELECT COUNT(DISTINCT(goods_id)) FROM " . $ecs->table('goods_attr')); |
|
75 |
/* 获取符合条件的商品id */ |
|
76 |
//$sql = "SELECT DISTINCT goods_id FROM " .$ecs->table('goods_attr'). " LIMIT 100"; |
|
77 |
$sql = "SELECT DISTINCT goods_id FROM " .$ecs->table('goods_attr'); |
|
78 |
$in_goods = $db->GetCol($sql); |
|
79 |
$in_goods = 'AND g.goods_id ' . db_create_in(implode(',', $in_goods)); |
|
80 |
$url = "search.php?pickout=1"; |
|
81 |
} |
|
82 |
else |
|
83 |
{ |
|
84 |
/* 取得商品类型名称 */ |
|
85 |
$sql = "SELECT cat_name FROM ".$ecs->table('goods_type')." WHERE cat_id = '$cat_id'"; |
|
86 |
$cat_name = $db->GetOne($sql); |
|
87 |
$condition[0]['name'] = $cat_name; |
|
88 |
|
|
89 |
$picks[] = array('name'=>'<strong>'.$_LANG['goods_type'].':</strong><br />'.$cat_name, 'url'=>'pick_out.php'); |
|
90 |
|
|
91 |
$attr_picks = array(); //选择过的attr_id |
|
92 |
|
|
93 |
/* 处理属性,获取满足属性的goods_id */ |
|
94 |
if (!empty($_GET['attr'])) |
|
95 |
{ |
|
96 |
$attr_table = ''; |
|
97 |
$attr_where = ''; |
|
98 |
$attr_url = ''; |
|
99 |
$i = 0; |
|
100 |
$goods_result = ''; |
|
101 |
foreach ($_GET['attr'] AS $key => $value) |
|
102 |
{ |
|
103 |
$attr_url .= '&attr[' . $key . ']=' . $value; |
|
104 |
|
|
105 |
$attr_picks[] = $key; |
|
106 |
if ($i > 0) |
|
107 |
{ |
|
108 |
if (empty($goods_result)) |
|
109 |
{ |
|
110 |
break; |
|
111 |
} |
|
112 |
$goods_result = $db->getCol("SELECT goods_id FROM " . $ecs->table("goods_attr") . " WHERE goods_id IN (" . implode(',' , $goods_result) . ") AND attr_id='$key' AND attr_value='$value'"); |
|
113 |
} |
|
114 |
else |
|
115 |
{ |
|
116 |
$goods_result = $db->getCol("SELECT goods_id FROM " . $ecs->table("goods_attr") . " WHERE attr_id='$key' AND attr_value='$value'"); |
|
117 |
} |
|
118 |
$i++; |
|
119 |
} |
|
120 |
|
|
121 |
/* 获取指定attr_id的名字 */ |
|
122 |
$sql = "SELECT attr_id, attr_name FROM ".$ecs->table('attribute')." WHERE attr_id ".db_create_in(implode(',',$attr_picks)); |
|
123 |
$rs = $db->query($sql); |
|
124 |
while ($row = $db->fetchRow($rs)) |
|
125 |
{ |
|
126 |
$picks[] = array('name'=>'<strong>'.$row['attr_name'].':</strong><br />'.htmlspecialchars(urldecode($_GET['attr'][$row['attr_id']])), 'url'=>'pick_out.php?cat_id='.$cat_id.search_url($attr_picks, $row['attr_id'])); |
|
127 |
} |
|
128 |
|
|
129 |
/* 查出数量 */ |
|
130 |
$goods_count = count($goods_result); |
|
131 |
/* 获取符合条件的goods_id */ |
|
132 |
$in_goods = 'AND g.goods_id '.db_create_in(implode(',', $goods_result)); |
|
133 |
} |
|
134 |
else |
|
135 |
{ |
|
136 |
/* 仅选择了商品类型的情况 */ |
|
137 |
|
|
138 |
/* 查出数量 */ |
|
139 |
$goods_count = $db->GetOne("SELECT COUNT(distinct(g.goods_id)) FROM ".$ecs->table('goods_attr')." AS g, ".$ecs->table('attribute')." AS a WHERE g.attr_id = a.attr_id AND a.cat_id = '$cat_id' "); |
|
140 |
|
|
141 |
/* 防止结果过大,最多只查出前100个goods_id */ |
|
142 |
|
|
143 |
$sql = "SELECT DISTINCT g.goods_id FROM ".$ecs->table('goods_attr')." AS g, ".$ecs->table('attribute')." AS a WHERE g.attr_id = a.attr_id AND a.cat_id = '$cat_id' LIMIT 100"; |
|
144 |
$in_goods = $db->GetCol($sql); |
|
145 |
$in_goods = 'AND g.goods_id '.db_create_in(implode(',', $in_goods)); |
|
146 |
|
|
147 |
} |
|
148 |
|
|
149 |
/* 获取符合条件的属性 */ |
|
150 |
$sql = "SELECT DISTINCT a.attr_id FROM ".$ecs->table('goods_attr')." AS g, ".$ecs->table('attribute') ." AS a ". |
|
151 |
"WHERE a.attr_id = g.attr_id " . $in_goods; |
|
152 |
|
|
153 |
$in_attr = $db->GetCol($sql); // 符合条件attr_id; |
|
154 |
$in_attr = array_diff($in_attr, $attr_picks); // 除去已经选择过的attr_id |
|
155 |
$in_attr = 'AND g.attr_id '.db_create_in(implode(',', $in_attr)); |
|
156 |
|
|
157 |
/* 获取所有属性值 */ |
|
158 |
$sql = "SELECT DISTINCT g.attr_id, a.attr_name, g.attr_value FROM ".$ecs->table('goods_attr')." AS g, ".$ecs->table('attribute') ." AS a WHERE a.attr_id = g.attr_id ".$in_attr.$in_goods; |
|
159 |
$rs = $db->query($sql); |
|
160 |
|
|
161 |
while ($row = $db->fetchRow($rs)) |
|
162 |
{ |
|
163 |
if (empty($condition[0]['cat'][$row['attr_id']]['cat_name'])) |
|
164 |
{ |
|
165 |
$condition[0]['cat'][$row['attr_id']]['cat_name'] = $row['attr_name']; |
|
166 |
} |
|
167 |
$condition[0]['cat'][$row['attr_id']]['list'][] = array('name'=>$row['attr_value'], 'url'=>'pick_out.php?cat_id='.$cat_id.search_url($attr_picks).'&attr['.$row['attr_id'].']='.urlencode($row['attr_value'])); |
|
168 |
} |
|
169 |
|
|
170 |
/* 生成更多商品的url */ |
|
171 |
$url = "search.php?pickout=1&cat_id=".$cat_id.search_url($attr_picks); |
|
172 |
|
|
173 |
} |
|
174 |
|
|
175 |
/* 显示商品 */ |
|
176 |
$goods = array(); |
|
177 |
$sql = "SELECT g.goods_id, g.goods_name, g.market_price, g.shop_price AS org_price, ". |
|
178 |
"IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, ". |
|
179 |
"g.promote_price, promote_start_date, promote_end_date, g.goods_brief, g.goods_thumb ". |
|
180 |
"FROM " .$ecs->table('goods'). " AS g ". |
|
181 |
"LEFT JOIN " . $GLOBALS['ecs']->table('member_price') . " AS mp ". |
|
182 |
"ON mp.goods_id = g.goods_id AND mp.user_rank = '$_SESSION[user_rank]' ". |
|
183 |
"WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ".$in_goods. |
|
184 |
"ORDER BY g.sort_order, g.last_update DESC"; |
|
185 |
$res = $db->SelectLimit($sql, 4); |
|
186 |
|
|
187 |
/* 获取品牌 */ |
|
188 |
$sql = "SELECT b.brand_id, b.brand_name, b.brand_logo, COUNT(g.goods_id) AS goods_num ". |
|
189 |
" FROM " . $ecs->table('goods') . " AS g ". |
|
190 |
" LEFT JOIN " . $ecs->table('brand') . " AS b ON g.brand_id=b.brand_id ". |
|
191 |
" WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 AND b.brand_id > 0 " . $in_goods . |
|
192 |
" GROUP BY g.brand_id "; |
|
193 |
|
|
194 |
$brand_list = $db->getAll($sql); |
|
195 |
foreach ($brand_list as $key=>$val) |
|
196 |
{ |
|
197 |
$brand_list[$key]['url'] = $url . '&brand=' . $val['brand_id']; |
|
198 |
} |
|
199 |
|
|
200 |
/* 获取分类 */ |
|
201 |
$sql = "SELECT c.cat_id, c.cat_name, COUNT(g.goods_id) AS goods_num ". |
|
202 |
" FROM " . $ecs->table('goods') . " AS g ". |
|
203 |
" LEFT JOIN " . $ecs->table('category') . " AS c ON c.cat_id = g.cat_id ". |
|
204 |
" WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 " . $in_goods . |
|
205 |
" GROUP BY g.cat_id "; |
|
206 |
|
|
207 |
$cat_list = $db->getAll($sql); |
|
208 |
|
|
209 |
foreach ($cat_list as $key=>$val) |
|
210 |
{ |
|
211 |
$cat_list[$key]['url'] = $url . '&category=' . $val['cat_id']; |
|
212 |
} |
|
213 |
|
|
214 |
|
|
215 |
$idx = 0; |
|
216 |
while ($row = $db->fetchRow($res)) |
|
217 |
{ |
|
218 |
if ($row['promote_price'] > 0) |
|
219 |
{ |
|
220 |
$promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']); |
|
221 |
} |
|
222 |
else |
|
223 |
{ |
|
224 |
$promote_price = 0; |
|
225 |
} |
|
226 |
|
|
227 |
$goods[$idx]['id'] = $row['goods_id']; |
|
228 |
$goods[$idx]['name'] = $row['goods_name']; |
|
229 |
$goods[$idx]['short_name'] = $_CFG['goods_name_length'] > 0 ? sub_str($row['goods_name'], $_CFG['goods_name_length']) : $row['goods_name']; |
|
230 |
$goods[$idx]['market_price'] = $row['market_price']; |
|
231 |
$goods[$idx]['shop_price'] = price_format($row['shop_price']); |
|
232 |
$goods[$idx]['promote_price'] = $promote_price > 0 ? price_format($promote_price) : ''; |
|
233 |
$goods[$idx]['brief'] = $row['goods_brief']; |
|
234 |
$goods[$idx]['thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true); |
|
235 |
$goods[$idx]['url'] = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']); |
|
236 |
|
|
237 |
$idx++; |
|
238 |
} |
|
239 |
|
|
240 |
$picks[] = array('name'=>$_LANG['remove_all'], 'url'=>'pick_out.php'); |
|
241 |
|
|
242 |
assign_template(); |
|
243 |
$position = assign_ur_here(0, $_LANG['pick_out']); |
|
244 |
$smarty->assign('page_title', $position['title']); // 页面标题 |
|
245 |
$smarty->assign('ur_here', $position['ur_here']); // 当前位置 |
|
246 |
|
|
247 |
$smarty->assign('brand_list', $brand_list); //品牌 |
|
248 |
$smarty->assign('cat_list', $cat_list); //分类列表 |
|
249 |
|
|
250 |
$smarty->assign('categories', get_categories_tree()); // 分类树 |
|
251 |
$smarty->assign('helps', get_shop_help()); // 网店帮助 |
|
252 |
$smarty->assign('top_goods', get_top10()); // 销售排行 |
|
253 |
$smarty->assign('data_dir', DATA_DIR); // 数据目录 |
|
254 |
|
|
255 |
/* 调查 */ |
|
256 |
$vote = get_vote(); |
|
257 |
if (!empty($vote)) |
|
258 |
{ |
|
259 |
$smarty->assign('vote_id', $vote['id']); |
|
260 |
$smarty->assign('vote', $vote['content']); |
|
261 |
} |
|
262 |
|
|
263 |
assign_dynamic('pick_out'); |
|
264 |
|
|
265 |
$smarty->assign('url', $url); |
|
266 |
$smarty->assign('pickout_goods', $goods); |
|
267 |
$smarty->assign('count', $goods_count); |
|
268 |
$smarty->assign('picks', $picks); |
|
269 |
$smarty->assign('condition', $condition); |
|
270 |
$smarty->display('pick_out.dwt'); |
|
271 |
|
|
272 |
/** |
|
273 |
* 生成搜索的链接地址 |
|
274 |
* |
|
275 |
* @access public |
|
276 |
* @param int attr_id 要排除的attr_id |
|
277 |
* |
|
278 |
* @return string |
|
279 |
*/ |
|
280 |
function search_url(&$attr_picks, $attr_id = 0) |
|
281 |
{ |
|
282 |
$str = ''; |
|
283 |
foreach ($attr_picks AS $pick_id) |
|
284 |
{ |
|
285 |
if ($pick_id != $attr_id) |
|
286 |
{ |
|
287 |
$str .= '&attr['.$pick_id.']='.urlencode($_GET['attr'][$pick_id]); |
|
288 |
} |
|
289 |
} |
|
290 |
|
|
291 |
return $str; |
|
292 |
} |
|
293 |
|
|
294 |
?> |