wangtengyu
2018-12-07 f459412e0dac4ed94106da043b4c6f8576bfe496
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
12 if ((DEBUG_MODE & 2) != 2)
13 {
14     $smarty->caching = true;
15 }
16
17
18 /*------------------------------------------------------ */
19 //-- INPUT
20 /*------------------------------------------------------ */
21
22 // START  safety 
23 if (isset($_REQUEST['brand']))
24 {
25     $brands = explode('_',$_REQUEST['brand']);
26     foreach ($brands as $brand_info)
27     {
28         if (!is_numeric($brand_info))
29         {
30             /* 如果分类ID为0,则返回首页 */
31             ecs_header("Location: ./\n");
32             exit;
33         }
34     }
35 }
36 // END  safety 
37
38
39 /* 获得请求的分类 ID */
40 if (isset($_REQUEST['id']))
41 {
42     $cat_id = intval($_REQUEST['id']);
43 }
44 elseif (isset($_REQUEST['category']))
45 {
46     $cat_id = intval($_REQUEST['category']);
47 }
48 else
49 {
50     /* 如果分类ID为0,则返回首页 */
51     ecs_header("Location: ./\n");
52
53     exit;
54 }
55
56 // 读取频道页模版
57 $index_file_sql = "select * from " . $ecs->table('category') . " where cat_id = " . $cat_id;
58 $index_file = $db->getRow($index_file_sql);
59 $is_index = $index_file['category_index'];
60 $is_index_dwt = $index_file['category_index_dwt'];
61 $index_dwt_file = $index_file['index_dwt_file'];
62 if ($is_index == 0)
63 {
64     $index_dwt_files = 'category.dwt';
65 }
66 else
67 {
68     $index_dwt_files = (($is_index_dwt == 0 || $index_dwt_file == '') ? 'category_index.dwt' : $index_dwt_file);
69 }
70     /* 初始化分页信息 */
71     $page = isset($_REQUEST['page'])   && intval($_REQUEST['page'])  > 0 ? intval($_REQUEST['page'])  : 1;
72     $size = isset($_CFG['page_size'])  && intval($_CFG['page_size']) > 0 ? intval($_CFG['page_size']) : 10;
73     $brand = isset($_REQUEST['brand']) && $_REQUEST['brand'] > 0 ? $_REQUEST['brand'] : 0;
74     $price_max = isset($_REQUEST['price_max']) && intval($_REQUEST['price_max']) > 0 ? intval($_REQUEST['price_max']) : 0;
75     $price_min = isset($_REQUEST['price_min']) && intval($_REQUEST['price_min']) > 0 ? intval($_REQUEST['price_min']) : 0;
76     $filter = (isset($_REQUEST['filter'])) ? intval($_REQUEST['filter']) : 0;
77     $filter_attr_str = isset($_REQUEST['filter_attr']) ? htmlspecialchars(trim($_REQUEST['filter_attr'])) : '0';
78
79     $filter_attr_str = trim(urldecode($filter_attr_str));
80     $filter_attr_str = preg_match('/^[\d\._]+$/',$filter_attr_str) ? $filter_attr_str : '';  //
81     $filter_attr = empty($filter_attr_str) ? '' : explode('.', $filter_attr_str);
82     
83     if(!empty($brand) || !empty($price_max) || !empty($price_min) || !empty($filter_attr)){
84         //就算有顶级页面,也不调用
85         $index_dwt_files = 'category.dwt';
86     }
87
88     /* 排序、显示方式以及类型 */
89     $default_display_type = $_CFG['show_order_type'] == '0' ? 'list' : ($_CFG['show_order_type'] == '1' ? 'grid' : 'text');
90     $default_sort_order_method = $_CFG['sort_order_method'] == '0' ? 'DESC' : 'ASC';
91     $default_sort_order_type   = $_CFG['sort_order_type'] == '0' ? 'goods_id' : ($_CFG['sort_order_type'] == '1' ? 'shop_price' : 'last_update');
92     $default_sort_order_type   = $_CFG['sort_order_type'] == '0' ? 'goods_id' : ($_CFG['sort_order_type'] == '1' ? 'shop_price' : 'last_update');
93
94     $sort = (isset($_REQUEST['sort']) && in_array(trim(strtolower($_REQUEST['sort'])), array('goods_id', 'shop_price', 'last_update','click_count','goods_number', 'salenum'))) ? trim($_REQUEST['sort'])  : $default_sort_order_type;  
95
96     $order = (isset($_REQUEST['order']) && in_array(trim(strtoupper($_REQUEST['order'])), array('ASC', 'DESC')))                              ? trim($_REQUEST['order']) : $default_sort_order_method;
97     /* 代码修改Start */
98     $is_stock = isset($_REQUEST['is_stock']) ? $_REQUEST['is_stock'] : 0;
99     if(!empty($is_stock))
100     {
101         $smarty->assign('is_stock', $is_stock);
102     }
103     /* 代码修改 End */
104 $display  = (isset($_REQUEST['display']) && in_array(trim(strtolower($_REQUEST['display'])), array('list', 'grid', 'text'))) ? trim($_REQUEST['display'])  : (isset($_COOKIE['ECS']['display']) ? $_COOKIE['ECS']['display'] : $default_display_type);
105     $display  = in_array($display, array('list', 'grid', 'text')) ? $display : 'text';
106     setcookie('ECS[display]', $display, gmtime() + 86400 * 7);
107     /*------------------------------------------------------ */
108     //-- PROCESSOR
109     /*------------------------------------------------------ */
110
111     /* 页面的缓存ID */
112     $cache_id = sprintf('%X', crc32($cat_id . '-' . $display . '-' . $sort  .'-' . $order  .'-' . $page . '-' . $size . '-' . $_SESSION['user_rank'] . '-' .
113         $_CFG['lang'] .'-'. $brand. '-' . $price_max . '-' .$price_min . '-' . $filter_attr_str . '-' . $filter . '-' . $is_stock));
114
115     if (!$smarty->is_cached($index_dwt_files, $cache_id))
116     {
117         /* 如果页面没有被缓存则重新获取页面的内容 */
118
119         $children = get_children($cat_id);
120
121         $cat = get_cat_info($cat_id);   // 获得分类的相关信息
122
123         if (!empty($cat))
124         {
125             $cat['style'] = empty($cat['style']) ? "category.css" : $cat['style'];
126             $smarty->assign('keywords',    htmlspecialchars($cat['keywords']));
127             $smarty->assign('description', htmlspecialchars($cat['cat_desc']));
128             
129             $smarty->assign('parent_id',   htmlspecialchars($cat['parent_id']));
130             $smarty->assign('cat_id', $cat_id);
131         }
132         else
133         {
134             /* 如果分类不存在则返回首页 */
135             ecs_header("Location: ./\n");
136
137             exit;
138         }
139
140         /* 赋值固定内容 */
141         if ($brand > 0)
142         {
143             if (strstr($brand,'_'))
144             {
145                 $brand_name="";
146                 $bbbb=0;
147                 $brand_sql =str_replace("_", ",", $brand);
148                 $sql = "SELECT brand_name FROM " .$GLOBALS['ecs']->table('brand'). " WHERE brand_id in ($brand_sql) ";
149                 $brand_res = $db->query($sql);
150                 while ($brand_row=$db->fetchRow($brand_res))
151                 {
152                     $brand_name .= ($bbbb ? "," : ""). $brand_row['brand_name'];
153                     $bbbb++;
154                 }
155             }
156             else
157             {
158                 $sql = "SELECT brand_name FROM " .$GLOBALS['ecs']->table('brand'). " WHERE brand_id = '$brand'";
159                 $brand_name = $db->getOne($sql);
160             }
161         }
162         else
163         {
164             $brand_name = '';
165         }
166
167         /* 获取价格分级 */
168         if ($cat['grade'] == 0  && $cat['parent_id'] != 0)
169         {
170             $cat['grade'] = get_parent_grade($cat_id); //如果当前分类级别为空,取最近的上级分类
171         }
172
173         if ($cat['grade'] > 1)
174         {
175             /* 需要价格分级 */
176
177             /*
178                 算法思路:
179                     1、当分级大于1时,进行价格分级
180                     2、取出该类下商品价格的最大值、最小值
181                     3、根据商品价格的最大值来计算商品价格的分级数量级:
182                             价格范围(不含最大值)    分级数量级
183                             0-0.1                   0.001
184                             0.1-1                   0.01
185                             1-10                    0.1
186                             10-100                  1
187                             100-1000                10
188                             1000-10000              100
189                     4、计算价格跨度:
190                             取整((最大值-最小值) / (价格分级数) / 数量级) * 数量级
191                     5、根据价格跨度计算价格范围区间
192                     6、查询数据库
193
194                 可能存在问题:
195                     1、
196                     由于价格跨度是由最大值、最小值计算出来的
197                     然后再通过价格跨度来确定显示时的价格范围区间
198                     所以可能会存在价格分级数量不正确的问题
199                     该问题没有证明
200                     2、
201                     当价格=最大值时,分级会多出来,已被证明存在
202             */
203
204             $sql = "SELECT min(g.shop_price) AS min, max(g.shop_price) as max ".
205                    " FROM " . $ecs->table('goods'). " AS g ".
206                    " WHERE ($children OR " . get_extension_goods($children) . ') AND g.is_delete = 0 AND g.is_on_sale = 1 AND g.is_alone_sale = 1  ';
207                    //获得当前分类下商品价格的最大值、最小值
208
209             $row = $db->getRow($sql);
210
211             // 取得价格分级最小单位级数,比如,千元商品最小以100为级数
212             $price_grade = 0.0001;
213             for($i=-2; $i<= log10($row['max']); $i++)
214             {
215                 $price_grade *= 10;
216             }
217
218             //跨度
219             $dx = ceil(($row['max'] - $row['min']) / ($cat['grade']) / $price_grade) * $price_grade;
220             if($dx == 0)
221             {
222                 $dx = $price_grade;
223             }
224
225             for($i = 1; $row['min'] > $dx * $i; $i ++);
226
227             for($j = 1; $row['min'] > $dx * ($i-1) + $price_grade * $j; $j++);
228             $row['min'] = $dx * ($i-1) + $price_grade * ($j - 1);
229
230             for(; $row['max'] >= $dx * $i; $i ++);
231             $row['max'] = $dx * ($i) + $price_grade * ($j - 1);
232
233             $sql = "SELECT (FLOOR((g.shop_price - $row[min]) / $dx)) AS sn, COUNT(*) AS goods_num  ".
234                    " FROM " . $ecs->table('goods') . " AS g ".
235                    " WHERE ($children OR " . get_extension_goods($children) . ') AND g.is_delete = 0 AND g.is_on_sale = 1 AND g.is_alone_sale = 1 '.
236                    " GROUP BY sn ";
237
238             $price_grade = $db->getAll($sql);
239             
240             foreach ($price_grade as $key=>$val)
241             {
242                 $temp_key = $key + 1;
243                 $price_grade[$temp_key]['goods_num'] = $val['goods_num'];
244                 $price_grade[$temp_key]['start'] = $row['min'] + round($dx * $val['sn']);
245                 $price_grade[$temp_key]['end'] = $row['min'] + round($dx * ($val['sn'] + 1));
246                 $price_grade[$temp_key]['price_range'] = $price_grade[$temp_key]['start'] . '&nbsp;-&nbsp;' . $price_grade[$temp_key]['end'];
247                 $price_grade[$temp_key]['formated_start'] = price_format($price_grade[$temp_key]['start']);
248                 $price_grade[$temp_key]['formated_end'] = price_format($price_grade[$temp_key]['end']);
249                 $price_grade[$temp_key]['url'] = build_uri('category', array('cid'=>$cat_id, 'bid'=>$brand, 'price_min'=>$price_grade[$temp_key]['start'], 'price_max'=> $price_grade[$temp_key]['end'], 'filter_attr'=>$filter_attr_str, 'filter'=>$filter), $cat['cat_name']);
250
251                 /* 判断价格区间是否被选中 */
252                 if (isset($_REQUEST['price_min']) && $price_grade[$temp_key]['start'] == $price_min && $price_grade[$temp_key]['end'] == $price_max)
253                 {
254                     $price_grade[$temp_key]['selected'] = 1;                
255                 }
256                 else
257                 {
258                     $price_grade[$temp_key]['selected'] = 0;
259                 }
260             }
261
262             $price_grade[0]['start'] = 0;
263             $price_grade[0]['end'] = 0;
264             $price_grade[0]['price_range'] = $_LANG['all_attribute'];
265             $price_grade[0]['url'] = build_uri('category', array('cid'=>$cat_id, 'bid'=>$brand, 'price_min'=>0, 'price_max'=> 0, 'filter_attr'=>$filter_attr_str, 'filter'=>$filter), $cat['cat_name']);
266             $price_grade[0]['selected'] = empty($price_max) ? 1 : 0;
267
268             $smarty->assign('price_grade',     $price_grade);
269
270         }
271
272
273         
274
275
276         /* 品牌筛选 */
277
278         $sql = "SELECT b.brand_id, b.brand_name, b.brand_logo, COUNT(*) AS goods_num ".
279                 "FROM " . $GLOBALS['ecs']->table('brand') . "AS b, ".
280                     $GLOBALS['ecs']->table('goods') . " AS g LEFT JOIN ". $GLOBALS['ecs']->table('goods_cat') . " AS gc ON g.goods_id = gc.goods_id " .
281                 "WHERE g.brand_id = b.brand_id AND ($children OR " . 'gc.cat_id ' . db_create_in(array_unique(array_merge(array($cat_id), array_keys(cat_list($cat_id, 0, false))))) . ") AND b.is_show = 1 " .
282                 " AND g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 ".
283                 "GROUP BY b.brand_id HAVING goods_num > 0 ORDER BY b.sort_order, b.brand_id ASC"; //此SQL语句增加字段 b.brand_logo,  
284
285         $brands = $GLOBALS['db']->getAll($sql);
286
287         //商品来源过滤
288         
289         $attr_url_value = array('cid'=>$cat_id, 'bid'=>$brand, 'price_min'=>$price_min, 'price_max'=>$price_max,'filter_attr'=>$filter_attr_str);
290
291         $brand_have_logo = 0;  //代码增加
292
293         foreach ($brands AS $key => $val)
294         {
295             $temp_key = $key + 1;
296             $brands[$temp_key]['brand_name'] = $val['brand_name'];
297             $brands[$temp_key]['url'] = build_uri('category', array('cid' => $cat_id, 'bid' => $val['brand_id'], 'price_min'=>$price_min, 'price_max'=> $price_max, 'filter_attr'=>$filter_attr_str, 'filter'=>$filter), $cat['cat_name']);
298             
299             $brands[$temp_key]['brand_id_68ecshop'] = $val['brand_id'];
300             $brands[$temp_key]['brand_logo'] = $val['brand_logo'];
301             if ($val['brand_logo']){$brand_have_logo=1;}
302
303             /* 判断品牌是否被选中 */
304             if ($brand == $brands[$key]['brand_id'])
305             {
306                 $brands[$temp_key]['selected'] = 1;
307             }
308             else
309             {
310                 $brands[$temp_key]['selected'] = 0;
311             }
312         }
313
314         $condition = array();
315         $brand_zimu=array();
316         foreach($brands AS $bkey=>$bval)
317         {
318             $brands[$bkey]['pinyin'] = GetPinyin($bval['brand_name']);
319             $brands[$bkey]['shouzimu'] =substr($brands[$bkey]['pinyin'],0,1);
320             if (preg_match("/[a-zA-Z]/i", $brands[$bkey]['shouzimu']))
321             {        
322                 $brands[$bkey]['shouzimu']=strtoupper($brands[$bkey]['shouzimu']);
323                 $brand_zimu[$brands[$bkey]['shouzimu']]=$brands[$bkey]['shouzimu'];            
324             }
325             else
326             {
327                 $brand_zimu['其他']='其它'; 
328                 $brands[$bkey]['shouzimu']='其它';
329             }        
330         }
331         ksort($brand_zimu);
332     
333         if ($brand)
334         {
335                 $condition[] = array(
336                     'cond_type' => "品牌" ,
337                     'cond_name' => $brand_name ,
338                     'cond_url' => build_uri('category', array('cid' => $cat_id, 'bid' => 0, 'price_min'=>$price_min, 'price_max'=> $price_max, 'filter_attr'=>$filter_attr_str, 'filter'=>$filter), $cat['cat_name']) ,
339                 );
340         }
341         
342         if ($price_min || $price_max)
343         {
344                 $condition[] =array(
345                         'cond_type'=> '价格',
346                         'cond_name'=> $price_min."-".$price_max,
347                         'cond_url' => build_uri('category', array('cid'=>$cat_id, 'bid'=>$brand, 'price_min'=>0, 'price_max'=> 0, 'filter_attr'=>$filter_attr_str, 'filter'=>$filter), $cat['cat_name'])
348                     ); 
349         }
350         $smarty->assign('brand_zimu_68ecshop', $brand_zimu);
351         $smarty->assign('url_no_price', build_uri('category', array('cid'=>$cat_id, 'bid'=>$brand, 'price_min'=>$price_min, 'price_max'=>$price_max, 'filter_attr'=>$filter_attr_str, 'filter'=>$filter), $cat['cat_name']));
352         //$smarty->assign('url_no_price',);
353
354         $brands[0]['brand_name'] = $_LANG['all_attribute'];
355         $brands[0]['url'] = build_uri('category', array('cid' => $cat_id, 'bid' => 0, 'price_min'=>$price_min, 'price_max'=> $price_max, 'filter_attr'=>$filter_attr_str, 'filter'=>$filter), $cat['cat_name']);
356         $brands[0]['selected'] = empty($brand) ? 1 : 0;
357
358         $smarty->assign('brands', $brands);
359
360         if($brands and $cat['brand_qq'])
361         {
362         $brands_wwwecshop68Com=array();
363         $brands_wwwecshop68Com[0] = $brands[0];
364         $brands_qq=explode(',' , $cat['brand_qq']);
365         foreach($brands_qq as $key_qq => $brand_qq)
366         {
367             foreach($brands as $key_wwwecshop68com=>$brand_ecshop68com)
368             {
369                 if($brand_ecshop68com['brand_name']==$brand_qq)
370                 {
371                     $brands_wwwecshop68Com[]=$brand_ecshop68com;
372                     break;
373                 }
374             }
375         }
376         $brands=$brands_wwwecshop68Com;
377         $smarty->assign('brands', $brands);
378         }
379         /* 代码增加_end */
380
381         /* 属性筛选 */
382         $ext = ''; //商品查询条件扩展
383         if ($cat['filter_attr'] > 0)
384         {
385             $cat_filter_attr = explode(',', $cat['filter_attr']);       //提取出此分类的筛选属性
386             $smarty->assign('filter_attr_count_num',      count($cat_filter_attr));
387             $all_attr_list = array();
388
389             foreach ($cat_filter_attr AS $key => $value)
390             {
391                 $sql = "SELECT a.attr_name FROM " . $ecs->table('attribute') . " AS a, " . $ecs->table('goods_attr') . " AS ga, " . $ecs->table('goods') . " AS g WHERE ($children OR " . get_extension_goods($children) . ") AND a.attr_id = ga.attr_id AND g.goods_id = ga.goods_id AND g.is_delete = 0 AND g.is_on_sale = 1 AND g.is_alone_sale = 1 AND a.attr_id='$value'";
392                 if($temp_name = $db->getOne($sql))
393                 {
394                     $all_attr_list[$key]['filter_attr_name'] = $temp_name;
395
396                     $sql = "SELECT a.attr_id, MIN(a.goods_attr_id ) AS goods_id, a.attr_value AS attr_value FROM " . $ecs->table('goods_attr') . " AS a, " . $ecs->table('goods') .
397                            " AS g" .
398                            " WHERE ($children OR " . get_extension_goods($children) . ') AND g.goods_id = a.goods_id AND g.is_delete = 0 AND g.is_on_sale = 1 AND g.is_alone_sale = 1 '.
399                            " AND a.attr_id='$value' ".
400                            " GROUP BY a.attr_value";
401
402                     $attr_list = $db->getAll($sql);
403
404                     $temp_arrt_url_arr = array();
405
406                     for ($i = 0; $i < count($cat_filter_attr); $i++)        //获取当前url中已选择属性的值,并保留在数组中
407                     {
408                         $temp_arrt_url_arr[$i] = !empty($filter_attr[$i]) ? $filter_attr[$i] : 0;
409                     }
410
411                     $temp_arrt_url_arr[$key] = 0;                           //“全部”的信息生成
412                     $temp_arrt_url = implode('.', $temp_arrt_url_arr);
413                     $all_attr_list[$key]['attr_list'][0]['attr_value'] = $_LANG['all_attribute'];
414                     $all_attr_list[$key]['attr_list'][0]['url'] = build_uri('category', array('cid'=>$cat_id, 'bid'=>$brand, 'price_min'=>$price_min, 'price_max'=>$price_max, 'filter_attr'=>$temp_arrt_url, 'filter'=>$filter), $cat['cat_name']);
415                     $all_attr_list[$key]['attr_list'][0]['selected'] = empty($filter_attr[$key]) ? 1 : 0;
416                     
417
418                     foreach ($attr_list as $k => $v)
419                     {
420                         $temp_key = $k + 1;
421                         $temp_arrt_url_arr[$key] = $v['goods_id'];       //为url中代表当前筛选属性的位置变量赋值,并生成以‘.’分隔的筛选属性字符串
422                         $temp_arrt_url = implode('.', $temp_arrt_url_arr);
423                         
424
425                         $all_attr_list[$key]['attr_list'][$temp_key]['attr_value'] = $v['attr_value'];
426                         $all_attr_list[$key]['attr_list'][$temp_key]['url'] = build_uri('category', array('cid'=>$cat_id, 'bid'=>$brand, 'price_min'=>$price_min, 'price_max'=>$price_max, 'filter_attr'=>$temp_arrt_url, 'filter'=>$filter), $cat['cat_name']);
427
428                         $all_attr_list[$key]['attr_list'][$temp_key]['goods_id'] = $v['goods_id'];
429                         if($temp_name=='颜色')
430                         {
431                             $all_attr_list[$key]['attr_list'][$temp_key]['color_code']=$db->getOne("select color_code from ". $ecs->table('attribute_color') ." where color_name='$v[attr_value]' and attr_id='$v[attr_id]' ");
432                         }
433                         $filter_attr_name[$key][$v['goods_id']]=$v['attr_value'];
434
435                         if (!empty($filter_attr[$key]) AND $filter_attr[$key] == $v['goods_id'])
436                         {
437                             $all_attr_list[$key]['attr_list'][$temp_key]['selected'] = 1;
438                         }
439                         else
440                         {
441                             $all_attr_list[$key]['attr_list'][$temp_key]['selected'] = 0;
442                         }
443                     }
444                 }
445
446             }
447
448             foreach ($filter_attr AS $fkey=>$fval)
449             {
450                 if ($fval)
451                 {
452                     $cond_name = "";
453                     $filter_attr_temp = $filter_attr;
454                     $filter_attr_temp[$fkey]='0';
455                     $temp_arrt_url_68ecshop =implode(".", $filter_attr_temp);
456                     if (strstr($fval, "_"))
457                     {
458                         $fval_array = explode("_", $fval);
459                         foreach ($fval_array AS $fval_key=> $fval_68ecshop)
460                         {
461                             $cond_name .= ($fval_key ? "," : "") . $filter_attr_name[$fkey][$fval_68ecshop];
462                         }
463                     }
464                     else
465                     {
466                         $cond_name = $filter_attr_name[$fkey][$fval];
467                     }
468                     $condition[]=array(
469                             'cond_type' =>$all_attr_list[$fkey]['filter_attr_name'],
470                             'cond_name' =>$cond_name,
471                             'cond_url' =>build_uri('category', array('cid'=>$cat_id, 'bid'=>$brand, 'price_min'=>$price_min, 'price_max'=>$price_max, 'filter_attr'=>$temp_arrt_url_68ecshop, 'filter'=>$filter), $cat['cat_name'])
472                         );
473                 }
474             }
475             $attr_group_more_txt = "";
476             $attr_group_more_id =0;
477             foreach ($all_attr_list AS $k_68ecshop=>$k_value)
478             {
479                 if ($k_68ecshop >1)
480                 {
481                     if ($attr_group_more_id <3)
482                     {
483                         $attr_group_more_txt .= ($attr_group_more_id ? "," :"") . $k_value['filter_attr_name'];
484                     }
485                     $attr_group_more_id++;
486                 }
487             }
488             $smarty->assign('attr_group_more_count', count($all_attr_list));
489             $smarty->assign('attr_group_more_txt', $attr_group_more_txt);
490
491             $smarty->assign('filter_attr_list',  $all_attr_list);
492
493             /* 扩展商品查询条件 */
494             if (!empty($filter_attr))
495             {
496                 $ext_sql = "SELECT DISTINCT(b.goods_id) FROM " . $ecs->table('goods_attr') . " AS a, " . $ecs->table('goods_attr') . " AS b " .  "WHERE ";
497                 $ext_group_goods = array();
498
499                 foreach ($filter_attr AS $k => $v)                      // 查出符合所有筛选属性条件的商品id */
500                 {
501                     if (strstr($v, '_') && $v !=0 && isset($cat_filter_attr[$k]) )
502                     {
503                         $attr_sql = str_replace("_", ",", $v);
504                         $sql = $ext_sql . "b.attr_value = a.attr_value AND b.attr_id = " . $cat_filter_attr[$k] ." AND a.goods_attr_id  in ($attr_sql) ";
505                         $ext_group_goods = $db->getColCached($sql);
506                         $ext .= ' AND ' . db_create_in($ext_group_goods, 'g.goods_id');
507                     }
508                     elseif (is_numeric($v) && $v !=0 &&isset($cat_filter_attr[$k]))
509                     {
510                         $sql = $ext_sql . "b.attr_value = a.attr_value AND b.attr_id = " . $cat_filter_attr[$k] ." AND a.goods_attr_id = " . $v;
511                         $ext_group_goods = $db->getColCached($sql);
512                         $ext .= ' AND ' . db_create_in($ext_group_goods, 'g.goods_id');
513                     }
514                 }
515             }
516         }
517          if($cat['attr_wwwecshop68com'])
518          {
519              $attr_wwwecshop68com=explode("\r\n", $cat['attr_wwwecshop68com']);
520              $attr_def_wwwecshop68com=array();
521              foreach($attr_wwwecshop68com as $attr_ecshop68com)
522              {
523                 if ($attr_ecshop68com)
524                  {
525                         $attr_qq = explode(":", $attr_ecshop68com);
526                         $attr_def_wwwecshop68com[$attr_qq[0]]=explode(",", $attr_qq[1]);
527                 }
528              }
529
530             foreach ($all_attr_list as $key_attr_qq => $attr_old_ecshop68com)
531             {
532                 foreach($attr_def_wwwecshop68com as $key_def_ecshop68com => $attr_def_ecshop68com)
533                 {
534                     if ( $attr_old_ecshop68com['filter_attr_name'] == $key_def_ecshop68com)
535                     {
536                          $attr_list_qq = array();
537                          $attr_list_qq[0]= $attr_old_ecshop68com['attr_list'][0];
538                          foreach($attr_def_ecshop68com as  $attr_new_ecshop68com)
539                          {
540                                 foreach($attr_old_ecshop68com['attr_list'] as $attr_temp_qq)
541                                 {
542                                     if($attr_temp_qq['attr_value'] == $attr_new_ecshop68com )
543                                     {
544                                         $attr_list_qq[]=$attr_temp_qq;
545                                         break;
546                                     }
547                                 }
548                          }
549                         $all_attr_list[$key_attr_qq]['attr_list']=$attr_list_qq;
550                         break;
551                     }
552                 }
553             }
554
555
556             
557              $smarty->assign('filter_attr_list',  $all_attr_list);
558          }
559          
560          $filter_info = array(
561             0=>array('id'=>0,'name'=>'全部','selected'=>0,'url'=>build_uri('category', array_merge($attr_url_value,array('filter'=>0)),'全部')),
562             1=>array('id'=>1,'name'=>'网站自营','selected'=>0,'url'=>build_uri('category', array_merge($attr_url_value,array('filter'=>1)),'网站自营')),
563             2=>array('id'=>2,'name'=>'入驻商店铺','selected'=>0,'url'=>build_uri('category', array_merge($attr_url_value,array('filter'=>2)),'入驻商店铺'))
564         );
565         $filter_info[$filter]['selected'] = 1;
566         
567          $smarty->assign('filterinfo', $filter_info);
568
569         assign_template('c', array($cat_id));
570
571         $position = assign_ur_here($cat_id, $brand_name);
572         $smarty->assign('page_title',       $position['title']);    // 页面标题
573         $smarty->assign('ur_here',          $position['ur_here']);  // 当前位置
574         $smarty->assign('current_cat_id', $cat_id); //取得当前的id
575         /*取得顶级ID*/
576         $catlist = array();
577         foreach(get_parent_cats($cat_id) as $k=>$v)
578         {
579             $catlist[] = $v['cat_id'];
580         }
581         $smarty->assign('current_cat_pr_id',$catlist[count($catlist)-1]);/*取得顶级ID*/
582     $smarty->assign('current_cat_pr2_id',$catlist[count($catlist)-2]);/*取得二级分类ID*/
583         $smarty->assign('categories',       get_categories_tree($cat_id)); // 分类树
584         $smarty->assign('helps',            get_shop_help());              // 网店帮助
585         $smarty->assign('top_goods',        get_top10());                  // 销售排行
586         $smarty->assign('show_marketprice', $_CFG['show_marketprice']);
587         $smarty->assign('category',         $cat_id);
588         $smarty->assign('brand_id',         $brand);
589         $smarty->assign('price_max',        $price_max);
590         $smarty->assign('price_min',        $price_min);
591         $smarty->assign('filterid',         $filter);
592         $smarty->assign('filter_attr',      $filter_attr_str);
593         $smarty->assign('filter_attr_value',      $filter_attr_str);
594         $smarty->assign('feed_url',         ($_CFG['rewrite'] == 1) ? "feed-c$cat_id.xml" : 'feed.php?cat=' . $cat_id); // RSS URL
595         $smarty->assign('cainixihuan',       get_cainixihuan()); //猜你喜欢
596         $smarty->assign('compare_btn',        true);        //对比按钮
597
598         if ($brand > 0)
599         {
600             $arr['all'] = array('brand_id'  => 0,
601                             'brand_name'    => $GLOBALS['_LANG']['all_goods'],
602                             'brand_logo'    => '',
603                             'goods_num'     => '',
604                             'url'           => build_uri('category', array('cid'=>$cat_id), $cat['cat_name'])
605                         );
606         }
607         else
608         {
609             $arr = array();
610         }
611
612         $brand_list = array_merge($arr, get_brands($cat_id, 'category'));
613
614         $smarty->assign('data_dir',    DATA_DIR);
615         $smarty->assign('brand_list',      $brand_list);
616         $smarty->assign('promotion_info', get_promotion_info('',0));
617
618
619         /* 调查 */
620         $vote = get_vote();
621         if (!empty($vote))
622         {
623             $smarty->assign('vote_id',     $vote['id']);
624             $smarty->assign('vote',        $vote['content']);
625         }
626
627         $smarty->assign('best_goods',      get_category_recommend_goods('best', $children, $brand, $price_min, $price_max, $ext));
628         $smarty->assign('promotion_goods', get_category_recommend_goods('promote', $children, $brand, $price_min, $price_max, $ext));
629         $smarty->assign('hot_goods',       get_category_recommend_goods('hot', $children, $brand, $price_min, $price_max, $ext));
630         $smarty->assign('new_goods',       get_category_recommend_goods('new', $children, $brand, $price_min, $price_max, $ext));
631
632         $count = get_cagtegory_goods_count($children, $brand, $price_min, $price_max, $ext);
633         $max_page = ($count> 0) ? ceil($count / $size) : 1;
634         if ($page > $max_page)
635         {
636             $page = $max_page;
637         }
638         $goodslist = category_get_goods($children, $brand, $price_min, $price_max, $ext, $size, $page, $sort, $order, $is_stock);
639         if($display == 'grid')
640         {
641             if(count($goodslist) % 2 != 0)
642             {
643                 $goodslist[] = array();
644             }
645         }
646         //file_put_contents('./33$goodslist$goodslist$goodslist.txt',var_export($goodslist,true));
647         $smarty->assign('goods_list',       $goodslist);
648         $smarty->assign('category',         $cat_id);
649         $smarty->assign('script_name', 'category');
650         
651         $smarty->assign('cat_name_curr', $cat['cat_name']); 
652         $smarty->assign('condition', $condition);
653         $smarty->assign('brand_have_logo', $brand_have_logo);
654         $smarty->assign('filter_attr_count',      count($all_attr_list));
655         $filter = (isset($_REQUEST['filter'])) ? intval($_REQUEST['filter']) : 0;
656
657         assign_pager('category',            $cat_id, $count, $size, $sort, $order, $page, '', $brand, $price_min, $price_max, $display, $filter_attr_str,'','',$filter); // 分页
658         assign_dynamic('category'); // 动态内容
659     }
660
661 function get_categories($cat_id = 0)
662 {
663     if ($cat_id > 0)
664     {
665         $parent_id = $cat_id;
666     }
667     else
668     {
669         $parent_id = 0;
670     }
671
672     /*
673      判断当前分类中全是是否是底级分类,
674      如果是取出底级分类上级分类,
675      如果不是取当前分类及其下的子分类
676      */
677     $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('category') . " WHERE parent_id = '$cat_id' AND is_show = 1 ";
678     if ($GLOBALS['db']->getOne($sql) || $parent_id == 0)
679     {
680         /* 获取当前分类及其子分类 */
681         $sql = 'SELECT a.cat_id, a.cat_name, a.sort_order AS parent_order, a.cat_id, a.is_show,' .
682             'b.cat_id AS child_id, b.cat_name AS child_name, b.sort_order AS child_order ' .
683             'FROM ' . $GLOBALS['ecs']->table('category') . ' AS a ' .
684             'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS b ON b.parent_id = a.cat_id AND b.is_show = 1 ' .
685             "WHERE a.parent_id = '$parent_id' ORDER BY parent_order ASC, a.cat_id ASC, child_order ASC";
686     }
687     else
688     {
689         /* 获取当前分类及其父分类 */
690         $sql = 'SELECT a.cat_id, a.cat_name, b.cat_id AS child_id, b.cat_name AS child_name, b.sort_order, b.is_show ' .
691             'FROM ' . $GLOBALS['ecs']->table('category') . ' AS a ' .
692             'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS b ON b.parent_id = a.cat_id AND b.is_show = 1 ' .
693             "WHERE b.parent_id = '$parent_id' ORDER BY sort_order ASC";
694     }
695     $res = $GLOBALS['db']->getAll($sql);
696
697     $cat_arr = array();
698     foreach ($res AS $row)
699     {
700         if ($row['is_show'])
701         {
702             $cat_arr[$row['cat_id']]['id']   = $row['cat_id'];
703             $cat_arr[$row['cat_id']]['name'] = $row['cat_name'];
704             $cat_arr[$row['cat_id']]['url']  = build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);
705
706             if ($row['child_id'] != NULL)
707             {
708                 $cat_arr[$row['cat_id']]['children'][$row['child_id']]['id']   = $row['child_id'];
709                 $cat_arr[$row['cat_id']]['children'][$row['child_id']]['name'] = $row['child_name'];
710                 $cat_arr[$row['cat_id']]['children'][$row['child_id']]['url']  = build_uri('category', array('cid' => $row['child_id']), $row['child_name']);
711             }
712         }
713     }
714
715     return $cat_arr;
716 }
717 function get_cat_name_add($id)
718 {
719     $sql = 'SELECT cat_name ' . 'FROM ' . $GLOBALS['ecs']->table('category')  . "WHERE cat_id =$id " ;
720     $cat_name = $GLOBALS['db']->getOne($sql);
721     return $cat_name;
722 }
723 function get_parent($value,$id='')
724 {
725
726     if($value!=0)
727     {
728         $sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$value'";
729         $res = $GLOBALS['db']->getOne($sql);
730         return get_parent($res,$value);
731     }
732     else
733     {
734         return $id;
735     }
736 }
737 include_once("includes/lib_goods.php");
738 $smarty->assign('categories1'     ,    get_categories(get_parent($cat_id)));
739 $smarty->assign('cat_name'     ,       get_cat_name_add(get_parent($cat_id)));
740
741 if ( $is_index == '1' and !$_REQUEST['price_min'] and !$_REQUEST['price_max'] and !$_REQUEST['brand'] and !$_REQUEST['filter_attr'])
742 {
743     require_once ("themes/". $GLOBALS['_CFG']['template'] ."/library/lib_category_index.php" );
744
745     $smarty->assign('cat_style',   htmlspecialchars($cat['style']));
746 }else{
747     $smarty->assign('cat_style',   'category.css');
748 }
749 $smarty->assign('actname','category.php');
750
751 //线上红包
752 $sql = "SELECT * FROM " .$GLOBALS['ecs']->table('bonus_type') .
753     " WHERE send_type = '4'";
754 $row = $GLOBALS['db']->GetAll($sql);
755 $time=time();
756 date_default_timezone_set('PRC');
757 $smarty->assign('time',$time);
758 $smarty->assign('online_bonus',$row);
759
760
761 //判断 弹框登陆 验证码是否显示
762 $captcha = intval($_CFG['captcha']);
763 if(($captcha & CAPTCHA_LOGIN) && (! ($captcha & CAPTCHA_LOGIN_FAIL) || (($captcha & CAPTCHA_LOGIN_FAIL) && $_SESSION['login_fail'] > 2)) && gd_version() > 0)
764 {
765     $GLOBALS['smarty']->assign('enabled_captcha', 1);
766     $GLOBALS['smarty']->assign('rand', mt_rand());
767 }
768
769 $smarty->display($index_dwt_files, $cache_id);
770
771
772 /*------------------------------------------------------ */
773 //-- PRIVATE FUNCTION
774 /*------------------------------------------------------ */
775
776 /**
777  * 获得分类的信息
778  *
779  * @param   integer $cat_id
780  *
781  * @return  void
782  */
783 function get_cat_info($cat_id)
784 {
785     return $GLOBALS['db']->getRow('SELECT cat_name, keywords, cat_desc, style, grade, filter_attr, parent_id FROM ' . $GLOBALS['ecs']->table('category') .
786         " WHERE cat_id = '$cat_id'");
787 }
788
789 /**
790  * 获得分类下的商品
791  *
792  * @access  public
793  * @param   string  $children
794  * @return  array
795  */
796 function category_get_goods($children, $brand, $min, $max, $ext, $size, $page, $sort, $order, $is_stock = 0)
797 {
798     $filter = (isset($_REQUEST['filter'])) ? intval($_REQUEST['filter']) : 0;
799     
800     $display = $GLOBALS['display'];
801     $where = "g.is_on_sale = 1 AND g.is_alone_sale = 1 AND ".
802             "g.is_delete = 0 AND ($children OR " . get_extension_goods($children) . ')';
803     
804     if($filter==1){
805         
806         $where .= ' AND g.supplier_id=0 ';
807         
808     }elseif($filter==2){
809         
810         $where .= ' AND g.supplier_id>0 ';
811         
812     }else{}
813
814     if ($brand > 0)
815     {
816         /* 代码修改_start */
817         if (strstr($brand, '_'))
818         {
819             $brand_sql =str_replace("_", ",", $brand);
820             $where .=  "AND g.brand_id in ($brand_sql) ";
821         }
822         else
823         {
824             $where .=  "AND g.brand_id=$brand ";
825         }
826         /* 代码修改_end */
827     }
828
829     if ($min > 0)
830     {
831         $where .= " AND g.shop_price >= $min ";
832     }
833
834     if ($max > 0)
835     {
836         $where .= " AND g.shop_price <= $max ";
837     }
838
839     if($sort ==goods_number)
840     {
841         $where .= " AND g.goods_number != 0 ";
842     }
843     /* 代码增加Start */
844     if(!empty($is_stock))
845     {
846         $where .= " AND g.goods_number > 0 ";
847     }
848     /* 代码增加End */
849
850     /* 获得商品列表 */
851     $sort = ($sort == 'shop_price' ? 'shop_p' : $sort);
852     
853     $sql = "SELECT g.goods_id, g.goods_name, g.goods_name_style, g.click_count, g.goods_number, g.market_price, " .
854            " g.is_new, g.is_best, g.is_hot, g.shop_price AS org_price, " .
855            " IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, g.promote_price, " .
856            " IF(g.promote_price != '' " .
857                " AND g.promote_start_date < " . gmtime() .
858                " AND g.promote_end_date > " . gmtime() . ", g.promote_price, shop_price) " .
859            " AS shop_p, g.goods_type, " .
860            " g.promote_start_date, g.promote_end_date, g.goods_brief, g.goods_thumb, g.goods_img " .
861            " FROM " . $GLOBALS['ecs']->table('goods') .
862            " AS g " .
863            " LEFT JOIN " . $GLOBALS['ecs']->table('member_price') .
864            " AS mp " .
865            " ON mp.goods_id = g.goods_id " .
866            " AND mp.user_rank = '$_SESSION[user_rank]' " .
867            " WHERE $where $ext " .
868            " ORDER BY $sort $order";
869     if ($sort=='salenum')
870     {
871         $sql = "SELECT IFNULL(o.num,0) AS salenum, g.goods_id, g.goods_name, g.click_count, g.goods_number, g.goods_name_style, " .
872                " g.market_price, g.is_new, g.is_best, g.is_hot, g.shop_price AS org_price, " .
873                " IFNULL(mp.user_price, g.shop_price * '$_SESSION[discount]') AS shop_price, g.promote_price, g.goods_type, " .
874                " g.promote_start_date, g.promote_end_date, g.goods_brief, g.goods_thumb , g.goods_img " .
875                " FROM " . $GLOBALS['ecs']->table('goods') .
876                " AS g " .
877                " LEFT JOIN " . $GLOBALS['ecs']->table('member_price') .
878                " AS mp " .
879                " ON mp.goods_id = g.goods_id " .
880                " AND mp.user_rank = '$_SESSION[user_rank]' " .
881                " LEFT JOIN " .
882                " (SELECT " .
883                    " SUM(og.`goods_number`) " .
884                    " AS num,og.goods_id " .
885                    " FROM " .
886                    " ecs_order_goods AS og, " .
887                    " ecs_order_info AS oi " .
888                    " WHERE oi.pay_status = 2 " .
889                    " AND oi.order_status >= 1 " .
890                    " AND oi.order_id = og.order_id " .
891                    " GROUP BY og.goods_id) " .
892                " AS o " .
893                " ON o.goods_id = g.goods_id " .
894                " WHERE $where $ext " .
895                " ORDER BY $sort $order";
896                 
897     }
898
899
900     $res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);
901
902     $arr = array();
903     while ($row = $GLOBALS['db']->fetchRow($res))
904     {
905         if ($row['promote_price'] > 0)
906         {
907             $promote_price = bargain_price($row['promote_price'], $row['promote_start_date'], $row['promote_end_date']);
908         }
909         else
910         {
911             $promote_price = 0;
912         }
913
914         /* 处理商品水印图片 */
915         $watermark_img = '';
916
917         if ($promote_price != 0)
918         {
919             $watermark_img = "watermark_promote_small";
920         }
921         elseif ($row['is_new'] != 0)
922         {
923             $watermark_img = "watermark_new_small";
924         }
925         elseif ($row['is_best'] != 0)
926         {
927             $watermark_img = "watermark_best_small";
928         }
929         elseif ($row['is_hot'] != 0)
930         {
931             $watermark_img = 'watermark_hot_small';
932         }
933
934         if ($watermark_img != '')
935         {
936             $arr[$row['goods_id']]['watermark_img'] =  $watermark_img;
937         }
938
939         $arr[$row['goods_id']]['goods_id']         = $row['goods_id'];
940         if($display == 'grid')
941         {
942             $arr[$row['goods_id']]['goods_name']       = $GLOBALS['_CFG']['goods_name_length'] > 0 ? sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];
943         }
944         else
945         {
946             $arr[$row['goods_id']]['goods_name']       = $row['goods_name'];
947         }
948         $arr[$row['goods_id']]['goods_number']     = $row['goods_number'];
949         $arr[$row['goods_id']]['name']             = $row['goods_name'];
950         $arr[$row['goods_id']]['is_promote']             = $row['is_promote'];
951          $arr[$row['goods_id']]['is_new']             = $row['is_new'];
952           $arr[$row['goods_id']]['is_hot']             = $row['is_hot'];
953            $arr[$row['goods_id']]['is_best']             = $row['is_best'];
954         $arr[$row['goods_id']]['goods_brief']      = $row['goods_brief'];
955         $arr[$row['goods_id']]['goods_style_name'] = add_style($row['goods_name'],$row['goods_name_style']);
956         $arr[$row['goods_id']]['market_price']     = price_format($row['market_price']);
957         $arr[$row['goods_id']]['shop_price']       = price_format($row['shop_price']);
958         $arr[$row['goods_id']]['type']             = $row['goods_type'];
959         $arr[$row['goods_id']]['promote_price']    = ($promote_price > 0) ? price_format($promote_price) : '';
960         $arr[$row['goods_id']]['goods_thumb']      = get_image_path($row['goods_id'], $row['goods_thumb'], true);
961         $arr[$row['goods_id']]['goods_img']        = get_image_path($row['goods_id'], $row['goods_img']);
962         $arr[$row['goods_id']]['url']              = build_uri('goods', array('gid'=>$row['goods_id']), $row['goods_name']);
963         $arr[$row['goods_id']]['comment_count']    = get_comment_count($row['goods_id']);
964         $arr[$row['goods_id']]['count']            = selled_count($row['goods_id']);
965         $arr[$row['goods_id']]['click_count']      = $row['click_count'];
966     
967         /* 检查是否已经存在于用户的收藏夹 */
968         $sql = "SELECT COUNT(*) FROM " .$GLOBALS['ecs']->table('collect_goods') .
969             " WHERE user_id='$_SESSION[user_id]' AND goods_id = " . $row['goods_id'];
970         if ($GLOBALS['db']->GetOne($sql) > 0)
971         {
972             $arr[$row['goods_id']]['is_collet'] = 1;
973         }
974         else
975         {
976             $arr[$row['goods_id']]['is_collet'] = 0;
977         }
978     }
979     //按销量排序解决排序不对问题
980     if($sort=='salenum'){
981         foreach ($arr as $key => $value) {
982             $count[$key] = $value['count'];
983
984         }
985         if($order=="DESC"){
986             array_multisort($count,SORT_DESC, $arr);
987          }else{
988             array_multisort($count,SORT_ASC, $arr);
989         }
990     }
991     return $arr;
992 }
993
994 /**
995  * 获得分类下的商品总数
996  *
997  * @access  public
998  * @param   string     $cat_id
999  * @return  integer
1000  */
1001 function get_cagtegory_goods_count($children, $brand = 0, $min = 0, $max = 0, $ext='')
1002 {
1003     $filter = (isset($_REQUEST['filter'])) ? intval($_REQUEST['filter']) : 0;
1004     $where  = "g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 AND ($children OR " . get_extension_goods($children) . ')';
1005     
1006     if($filter==1){
1007         
1008         $where .= ' AND g.supplier_id=0 ';
1009         
1010     }elseif($filter==2){
1011         
1012         $where .= ' AND g.supplier_id>0 ';
1013         
1014     }else{}
1015     
1016     if ($brand > 0)
1017     {
1018         /* 代码增加_start  */
1019         if (strstr($brand, '_'))
1020         {
1021             $brand_sql =str_replace("_", ",", $brand);
1022             $where .=  "AND g.brand_id in ($brand_sql) ";
1023         }
1024         else
1025         {
1026             $where .=  "AND g.brand_id=$brand ";
1027         }
1028         /* 代码增加_end */
1029     }
1030
1031     if ($min > 0)
1032     {
1033         $where .= " AND g.shop_price >= $min ";
1034     }
1035
1036     if ($max > 0)
1037     {
1038         $where .= " AND g.shop_price <= $max ";
1039     }
1040     // 代码增加  Start
1041     $in_stock = (isset($_REQUEST['in_stock'])) ? intval($_REQUEST['in_stock']) : 0;
1042     if ($in_stock > 0)
1043     {
1044         $where .= " AND g.goods_number > 0";
1045     }
1046     // 代码增加 End
1047     /* 返回商品总数 */
1048     return $GLOBALS['db']->getOne('SELECT COUNT(*) FROM ' . $GLOBALS['ecs']->table('goods') . " AS g WHERE $where $ext");
1049 }
1050
1051 /**
1052  * 取得最近的上级分类的grade值
1053  *
1054  * @access  public
1055  * @param   int     $cat_id    //当前的cat_id
1056  *
1057  * @return int
1058  */
1059 function get_parent_grade($cat_id)
1060 {
1061     static $res = NULL;
1062
1063     if ($res === NULL)
1064     {
1065         $data = read_static_cache('cat_parent_grade');
1066         if ($data === false)
1067         {
1068             $sql = "SELECT parent_id, cat_id, grade ".
1069                    " FROM " . $GLOBALS['ecs']->table('category');
1070             $res = $GLOBALS['db']->getAll($sql);
1071             write_static_cache('cat_parent_grade', $res);
1072         }
1073         else
1074         {
1075             $res = $data;
1076         }
1077     }
1078
1079     if (!$res)
1080     {
1081         return 0;
1082     }
1083
1084     $parent_arr = array();
1085     $grade_arr = array();
1086
1087     foreach ($res as $val)
1088     {
1089         $parent_arr[$val['cat_id']] = $val['parent_id'];
1090         $grade_arr[$val['cat_id']] = $val['grade'];
1091     }
1092
1093     while ($parent_arr[$cat_id] >0 && $grade_arr[$cat_id] == 0)
1094     {
1095         $cat_id = $parent_arr[$cat_id];
1096     }
1097
1098     return $grade_arr[$cat_id];
1099
1100 }
1101  
1102 ?>