请问这种分类树是怎么回事?要如何解决?附图

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

看这三张图,第一张是一级分类,第二张是二级分类,第三张是底级分类,因为下面没分类了,所以显示成那样,但我想显示成像第四张这样的效果,类似http://demo.wd5u.com/360buy/category.php?id=72这样的效果。

找了很多有关修改get_categories_tree的帖,但都没有效果。希望哪位高人赐教。。








回答:
买个他的模板不就可以了吗,祝楼主早日得到高手的帮助!



第四张图跟第三张图是同一级的分类,但是第四张图的底级分类跟第三张图的底级分类样式不同。

我不知道你的代码是啥我想应该是 :{if } 就是写个判断分类存在的情况下显示。搞不定 找我 发我代码看

分类不同导致效果不一样!模板本身应该不会有问题吧~

/**
* 获得指定分类同级的所有分类以及该分类下的子分类
*
* @accesspublic
* @paraminteger$cat_id分类编号
* @returnarray
*/
function get_categories_tree($cat_id = 0)
{
if ($cat_id > 0)
{
$sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$cat_id'";
$parent_id = $GLOBALS['db']->getOne($sql);
}
else
{
$parent_id = 0;
}

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

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

foreach ($res AS $row)
{
if ($row['is_show'])
{
$cat_arr[$row['cat_id']]['id']= $row['cat_id'];
$cat_arr[$row['cat_id']]['name'] = $row['cat_name'];
$cat_arr[$row['cat_id']]['url']= build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);

if (isset($row['cat_id']) != NULL)
{
$cat_arr[$row['cat_id']]['cat_id'] = get_child_tree($row['cat_id']);
}
}
}
}
if(isset($cat_arr))
{
return $cat_arr;
}
}




这是lib.goods.php里的代码,实在找不到解决的办法,我也想写个{if}的判断,但是好像底级分类都识别不出似的。

  1. /**
  2. * 获得指定分类同级的所有分类以及该分类下的子分类
  3. *
  4. * @accesspublic
  5. * @paraminteger$cat_id分类编号
  6. * @returnarray
  7. */
  8. function get_categories_tree($cat_id = 0)
  9. {
  10. if ($cat_id > 0)
  11. {
  12. $sql = 'SELECT parent_id FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$cat_id'";
  13. $parent_id = $GLOBALS['db']->getOne($sql);
  14. }
  15. else
  16. {
  17. $parent_id = 0;
  18. }

  19. /*
  20. 判断当前分类中全是是否是底级分类,
  21. 如果是取出底级分类上级分类,
  22. 如果不是取当前分类及其下的子分类
  23. */
  24. $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('category') . " WHERE parent_id = '$cat_id' AND is_show = 1 ";
  25. if ($GLOBALS['db']->getOne($sql) || $parent_id == 0)
  26. {
  27. /* 获取当前分类及其子分类 */
  28. $sql = 'SELECT a.cat_id, a.cat_name, a.sort_order AS parent_order, a.cat_id, a.is_show,' .
  29. 'b.cat_id AS child_id, b.cat_name AS child_name, b.sort_order AS child_order ' .
  30. 'FROM ' . $GLOBALS['ecs']->table('category') . ' AS a ' .
  31. 'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS b ON b.parent_id = a.cat_id AND b.is_show = 1 ' .
  32. "WHERE a.parent_id = '$parent_id' ORDER BY parent_order ASC, a.cat_id ASC, child_order ASC";
  33. }
  34. else
  35. {
  36. /* 获取当前分类及其父分类 */
  37. $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 ' .
  38. 'FROM ' . $GLOBALS['ecs']->table('category') . ' AS a ' .
  39. 'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS b ON b.parent_id = a.cat_id AND b.is_show = 1 ' .
  40. "WHERE b.parent_id = '$parent_id' ORDER BY sort_order ASC";
  41. }
  42. $res = $GLOBALS['db']->getAll($sql);

  43. $cat_arr = array();
  44. foreach ($res AS $row)
  45. {
  46. if ($row['is_show'])
  47. {
  48. $cat_arr[$row['cat_id']]['id']= $row['cat_id'];
  49. $cat_arr[$row['cat_id']]['name'] = $row['cat_name'];
  50. $cat_arr[$row['cat_id']]['url']= build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);

  51. if ($row['child_id'] != NULL)
  52. {
  53. $cat_arr[$row['cat_id']]['children'][$row['child_id']]['id']= $row['child_id'];
  54. $cat_arr[$row['cat_id']]['children'][$row['child_id']]['name'] = $row['child_name'];
  55. $cat_arr[$row['cat_id']]['children'][$row['child_id']]['url']= build_uri('category', array('cid' => $row['child_id']), $row['child_name']);
  56. }
  57. }
  58. }

  59. return $cat_arr;
  60. }
  61. function get_child_tree($tree_id = 0)
  62. {
  63. $three_arr = array();
  64. $sql = 'SELECT cat_name FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$tree_id' AND is_show = 1 ";
  65. $cat_name = $GLOBALS['db']->getOne($sql);

  66. $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('category') . " WHERE parent_id = '$tree_id' AND is_show = 1 ";

  67. if ($GLOBALS['db']->getOne($sql) || $tree_id == 0)
  68. {
  69. $child_sql = 'SELECT cat_id, cat_name, parent_id, is_show ' .
  70. 'FROM ' . $GLOBALS['ecs']->table('category') .
  71. "WHERE parent_id = '$tree_id' AND is_show = 1 ORDER BY sort_order ASC, cat_id ASC";
  72. $res = $GLOBALS['db']->getAll($child_sql);
  73. foreach ($res AS $row)
  74. {
  75. if ($row['is_show'])

  76. $three_arr[$row['cat_id']]['id']= $row['cat_id'];
  77. $three_arr[$row['cat_id']]['name'] = $row['cat_name'];
  78. $three_arr[$row['cat_id']]['url']= build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);

  79. }
  80. }
  81. else
  82. {
  83. // echo "sdf";

  84. $sql = "SELECT parent_id,cat_name FROM " . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$tree_id' AND is_show = 1 ";

  85. $rows = $GLOBALS['db']->getRow($sql);
  86. // print_r($rows);exit;
  87. $parent_id = $rows['parent_id'];

  88. $sql1 = "SELECT cat_name FROM " . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$parent_id' AND is_show = 1 ";

  89. $cat_name = $GLOBALS['db']->getOne($sql1);
  90. //echo $cat_name;exit;
  91. $child_sql = 'SELECT cat_id, cat_name, parent_id, is_show ' .
  92. 'FROM ' . $GLOBALS['ecs']->table('category') .
  93. "WHERE parent_id = '$parent_id' AND is_show = 1 ORDER BY sort_order ASC, cat_id ASC";
  94. $res = $GLOBALS['db']->getAll($child_sql);
  95. foreach ($res AS $row)
  96. {
  97. if ($row['is_show'])

  98. $three_arr[$row['cat_id']]['id']= $row['cat_id'];
  99. $three_arr[$row['cat_id']]['name'] = $row['cat_name'];
  100. $three_arr[$row['cat_id']]['url']= build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);
  101. }
  102. }
  103. //$arr['cat_list'] = $three_arr;
  104. //$arr['cat_name'] = $cat_name;
  105. //print_r($three_arr);exit;
  106. return $three_arr;
  107. }
复制代码

能不能用判断来做?判断是第几级分类,然后修改样式?