当前位置:首页 > CMS教程 > ecshop > 列表

ecshop 当前分类及其子类的HACK

发布:smiling 来源: PHP粉丝网  添加日期:2014-07-21 14:46:16 浏览: 评论:0 

应该说Ecshop的分类做得还是比较好的,考虑到了大部分人的应用,能把所有的分类列表都显示出来,但还是有一些漏洞,有些网友也已经发现了.

当我们点击有子分类的某个分类时,Ecshop将没必要显示的分类也一起读出来了,相当于你想查你爸爸所有的孩子、孙子时,它把你爸爸所有的兄弟姐妹都一起显示出来了,这对一部分用户来说确实没必要.

我的修改只是在原有功能上添加一些功能,所以不影响原有的功能,而且也结合了模板技术,应该说定制起来还比较方便的,与大家分享:

第一步:修改/include/lib_goods.php,在第24行加入以下代码:

  1. function get_children_tree($cat_id) 
  2. { 
  3.     if ($cat_id >0 ) 
  4. { 
  5.        $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('category') . " WHERE parent_id = '$cat_id'"; 
  6.        //$cot = $GLOBALS['db']->getOne($sql);        
  7.        if ($GLOBALS['db']->getOne($sql)) 
  8.        { 
  9.          // 获取当前分类名及其子类 
  10.          $sql = 'SELECT a.cat_id, a.cat_name, a.sort_order AS parent_order, a.cat_id, ' . 
  11.                    'b.cat_id AS child_id, b.cat_name AS child_name, b.sort_order AS child_order ' . 
  12.             'FROM ' . $GLOBALS['ecs']->table('category') . ' AS a ' . 
  13.             'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS b ON b.parent_id = a.cat_id ' . 
  14.             "WHERE a.cat_id = '$cat_id' ORDER BY parent_order ASC, a.cat_id ASC, child_order ASC"; 
  15.        }        
  16.        else 
  17.        { 
  18.          $sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$cat_id'"; 
  19.          $parent_id = $GLOBALS['db']->getOne($sql); 
  20.          if ($parent_id > 0) 
  21.          { 
  22.             //获取当前分类、兄弟及其父类 
  23.             $sql = 'SELECT a.cat_id, a.cat_name, b.cat_id AS child_id, b.cat_name AS child_name, b.sort_order ' . 
  24.                    'FROM ' . $GLOBALS['ecs']->table('category') . ' AS a ' . 
  25.                    'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS b ON b.parent_id = a.cat_id ' . 
  26.                    "WHERE b.parent_id = '$parent_id' ORDER BY sort_order ASC"; 
  27.          } 
  28.          else 
  29.          { 
  30.             //获取当前分类 
  31.             $sql = 'SELECT a.cat_id, a.cat_name FROM ' 
  32.                      . $GLOBALS['ecs']->table('category') . ' AS a ' . 
  33.                      "WHERE a.cat_id = '$cat_id'"; 
  34.          } 
  35.        } 
  36.        
  37.        
  38.        $res = $GLOBALS['db']->getAll($sql); 
  39. $cat_arr = array(); 
  40. foreach ($res AS $row) 
  41. { 
  42.        $cat_arr[$row['cat_id']]['id'] = $row['cat_id']; 
  43.        $cat_arr[$row['cat_id']]['name'] = $row['cat_name']; 
  44.        $cat_arr[$row['cat_id']]['url']   = build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']); 
  45.        if ($row['child_id'] != NULL) 
  46.        { 
  47.          $cat_arr[$row['cat_id']]['children'][$row['child_id']]['id'] = $row['child_id']; 
  48.          $cat_arr[$row['cat_id']]['children'][$row['child_id']]['name'] = $row['child_name']; 
  49.          $cat_arr[$row['cat_id']]['children'][$row['child_id']]['url']   = build_uri('category', array('cid' => $row['child_id']), $row['child_name']); 
  50.        } 
  51. } 
  52. return $cat_arr; 
  53. } 
  54. } 

这其实就是一个get_children_tree函数,更具$cat_id来得到当前分类所有的孩子.

第二步,修改/category.php,找到122行,原先的代码是:

$smarty->assign('categories',get_categories_tree($cat_id)); // 分类树

这其实是模板技术,如果你想彻底抛弃原来的分类样式,那么把get_categories_tree($cat_id)换成刚才我们自定义的函数get_children_tree($cat_id)

如果你想保留原先的分类功能,再新增自定义的分类功能,那么在122行下面再新增一行:

$smarty->assign('categories2',get_children_tree($cat_id));

如果想用不同的颜色表示出当前点击的分类和其他分类,那么还要保留当前点击的分类id,再加一行:

$smarty->assign('current_cat_id', $cat_id); //当前的id

最后一步是模板:修改category.dwt.

你要根据第二部定义的模板变量来写:到底是categories还是categories2,更具你实际情况来定,我这里是categories2:

  1. 《!--{foreach from=$categories item=cat}--》 
  2. {$cat.name|escape:html} 《!--这个就是你点击的分类,下面都是他的子类--》 
  3.     《!--{foreach from=$cat.children item=child}--》 
  4.    《a href="{$child.url}"》 
  5. 《!--{if $current_cat_id eq $child.id} 显示当前点击的分类为橙色--》《span style="color:#ff6600"》《!--{/if}--》· {$child.name|escape:html}《!--{if $current_cat_id eq $child.id}--》《/span》《!--{/if}--》《/a》 
  6.  
  7.    《!--{foreachelse}--》 
  8.    · 没有分类了! 
  9.    《!--{/foreach}--》 
  10. 《!--{/foreach}--》 

Tags: ecshop当前分类 子类HACK

分享到: