始终只显示当前类别下的所有子类

2016-07-07 15:14 来源:www.chinab4c.com 作者:ecshop专家

http://wenxiong.t.zidc.cn/category.php?id=6这是现在的效果,我想改成只显示当类下的全部子类。百度上的那些解决方案用了之类子类点进去父类就会不见了。以下是代码区,指知道的好心人帮忙
category.php
  1. $smarty->assign('categories', get_categories_tree($cat_id)); // 分类树
  2. $smarty->assign('current_cat_id', $cat_id); //当前的id
  3. $smarty->assign('categories2',get_children_tree($cat_id));
复制代码


lib_goods.php
  1. function get_children_tree($cat_id=0)

  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. $res = $GLOBALS['db']->getAll($sql);

  37. $cat_arr = array();

  38. foreach ($res AS $row)

  39. {

  40. $cat_arr[$row['cat_id']]['id'] = $row['cat_id'];

  41. $cat_arr[$row['cat_id']]['name'] = $row['cat_name'];

  42. $cat_arr[$row['cat_id']]['url']= build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);

  43. if ($row['child_id'] != NULL)

  44. {

  45. $cat_arr[$row['cat_id']]['children'][$row['child_id']]['id'] = $row['child_id'];

  46. $cat_arr[$row['cat_id']]['children'][$row['child_id']]['name'] = $row['child_name'];

  47. $cat_arr[$row['cat_id']]['children'][$row['child_id']]['url']= build_uri('category', array('cid' => $row['child_id']), $row['child_name']);

  48. }

  49. }

  50. return $cat_arr;

  51. }

  52. }


  53. /**
  54. * 获得指定分类同级的所有分类以及该分类下的子分类
  55. *
  56. * @accesspublic
  57. * @paraminteger$cat_id分类编号
  58. * @returnarray
  59. */
  60. function get_categories_tree($cat_id = 0)
  61. {
  62. if ($cat_id > 0)
  63. {
  64. $sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$cat_id'";
  65. $parent_id = $GLOBALS['db']->getOne($sql);
  66. }
  67. else
  68. {
  69. $parent_id = 0;
  70. }

  71. /*
  72. 判断当前分类中全是是否是底级分类,
  73. 如果是取出底级分类上级分类,
  74. 如果不是取当前分类及其下的子分类
  75. */
  76. $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('category') . " WHERE parent_id = '$parent_id' AND is_show = 1 ";
  77. if ($GLOBALS['db']->getOne($sql) || $parent_id == 0)
  78. {
  79. /* 获取当前分类及其子分类 */
  80. $sql = 'SELECT cat_id,cat_name ,parent_id,is_show ' .
  81. 'FROM ' . $GLOBALS['ecs']->table('category') .
  82. "WHERE parent_id = '$parent_id' AND is_show = 1 ORDER BY sort_order ASC, cat_id ASC";

  83. $res = $GLOBALS['db']->getAll($sql);

  84. foreach ($res AS $row)
  85. {
  86. if ($row['is_show'])
  87. {
  88. $cat_arr[$row['cat_id']]['id']= $row['cat_id'];
  89. $cat_arr[$row['cat_id']]['name'] = $row['cat_name'];
  90. $cat_arr[$row['cat_id']]['url']= build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);

  91. if (isset($row['cat_id']) != NULL)
  92. {
  93. $cat_arr[$row['cat_id']]['cat_id'] = get_child_tree($row['cat_id']);
  94. }
  95. }
  96. }
  97. }
  98. if(isset($cat_arr))
  99. {
  100. return $cat_arr;
  101. }
  102. }

  103. function get_child_tree($tree_id = 0)
  104. {
  105. $three_arr = array();
  106. $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('category') . " WHERE parent_id = '$tree_id' AND is_show = 1 ";
  107. if ($GLOBALS['db']->getOne($sql) || $tree_id == 0)
  108. {
  109. $child_sql = 'SELECT cat_id, cat_name, parent_id, is_show ' .
  110. 'FROM ' . $GLOBALS['ecs']->table('category') .
  111. "WHERE parent_id = '$tree_id' AND is_show = 1 ORDER BY sort_order ASC, cat_id ASC";
  112. $res = $GLOBALS['db']->getAll($child_sql);
  113. foreach ($res AS $row)
  114. {
  115. if ($row['is_show'])

  116. $three_arr[$row['cat_id']]['id']= $row['cat_id'];
  117. $three_arr[$row['cat_id']]['name'] = $row['cat_name'];
  118. $three_arr[$row['cat_id']]['url']= build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);

  119. if (isset($row['cat_id']) != NULL)
  120. {
  121. $three_arr[$row['cat_id']]['cat_id'] = get_child_tree($row['cat_id']);

  122. }
  123. }
  124. }
  125. return $three_arr;
  126. }
复制代码



category_tree.lbi
  1. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  2. <div class="border2">
  3. <!--{foreach from=get_categories_tree() item=cat}-->
  4. <div class="bar1"><h4><a href="{$cat.url}">{$cat.name|escape:html}</a></h4></div>
  5. <dl class="product_class">
  6. <!--{foreach from=$cat.cat_id item=child}-->
  7. <dt><a href="{$child.url}"><!--{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></dt><dd>
  8. <!--{foreach from=$child.cat_id item=childer}-->
  9. <a href="{$childer.url}"><!--{if $current_cat_id eq $childer.id} 显示当前点击的分类为橙色--><span style="color:#ff6600"><!--{/if}-->{$childer.name|escape:html}<!--{if $current_cat_id eq $child.id}--></span><!--{/if}--></a><br>
  10. <!--{/foreach}-->

  11. <!--{/foreach}-->

  12. </dd></dt></dl>
  13. <!--{/foreach}-->
  14. </div>
复制代码

回答:
建议你去让别人二次开发帮你弄下,基本上不会有人解答的

不要这么残忍,跪求好心人解答啊

支持楼主,不错不错,来学习一下,呵呵!

ECSHOP自带有这个获取某个分类下的子类功能的
眼镜网|校园网|美瞳隐形眼镜|美瞳隐形眼镜|眼镜框架网上配眼镜|隐形眼镜护理液|眼镜商城

最好粘贴自己不理解的代码。这样看的眼花

我就是把我现在用的功能代码贴出来,我现在不知道要改哪个代码才能实现我只显示单前类全部分类的功能

我做的要嘛说是可以只显示当前子类,要嘛就是可以始终显示分类,但两个要合成始终只显示水前子类就不行了,我哭,不知道契机在哪里

不错啊! 一个字牛啊!












垃圾站厂http://www.fwgj.net

谢谢你哦,你是最棒的












垃圾站厂http://www.fwgj.net

经验之谈,谢谢楼主了,请继续努力












垃圾站厂http://www.fwgj.net

研究一下吧。可以的。

研究一下吧。可以的。
zgfzr 发表于 2012-7-12 15:19


这回复明显只是混个熟脸

取得顶级分类就好办了

努力研究中……虽然好像代码都看不懂