获得指定分类以及下级分类的函数

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


  1. //获得指定分类以及他的下一级分类 liangfang add

  2. function get_self_child_tree($cat_id = 0)
  3. {

  4. $sql = 'SELECT cat_name,cat_id FROM ' . $GLOBALS['ecs']->table('category') . " WHERE cat_id = '$cat_id' AND is_show = 1 ";
  5. $rw=$GLOBALS['db']->getRow($sql);

  6. $self_arr[$rw['cat_id']]['id']=$rw['cat_id'];
  7. $self_arr[$rw['cat_id']]['name']=$rw['cat_name'];
  8. $self_arr[$rw['cat_id']]['url']=build_uri('category', array('cid' => $rw['cat_id']), $rw['cat_name']);

  9. $parent_id=$rw['cat_id'];

  10. $three_arr = array();
  11. /*判断是否存在子下级分类*/
  12. $sql = 'SELECT count(*) FROM ' . $GLOBALS['ecs']->table('category') . " WHERE parent_id = '$parent_id' AND is_show = 1 ";
  13. if ($GLOBALS['db']->getOne($sql) || $cat_id == 0)
  14. {
  15. $child_sql = 'SELECT cat_id, cat_name, parent_id, is_show ' .
  16. 'FROM ' . $GLOBALS['ecs']->table('category') .
  17. "WHERE parent_id = '$parent_id' AND is_show = 1 ORDER BY sort_order ASC, cat_id ASC";
  18. $res = $GLOBALS['db']->getAll($child_sql);
  19. foreach ($res AS $row)
  20. {
  21. if ($row['is_show'])
  22. {
  23. $three_arr[$row['cat_id']]['id']= $row['cat_id'];
  24. $three_arr[$row['cat_id']]['name'] = $row['cat_name'];
  25. $three_arr[$row['cat_id']]['url']= build_uri('category', array('cid' => $row['cat_id']), $row['cat_name']);
  26. }
  27. }
  28. }

  29. $self_arr[$rw['cat_id']]['three']=$three_arr;
  30. return $self_arr;
  31. }
复制代码
  1. TEST....................

  2. Array
  3. (
  4. [261] => Array
  5. (
  6. [id] => 261
  7. [name] => 优品稻米
  8. [url] => category.php?id=261
  9. [three] => Array
  10. (
  11. [269] => Array
  12. (
  13. [id] => 269
  14. [name] => 东北大米
  15. [url] => category.php?id=269
  16. )

  17. [270] => Array
  18. (
  19. [id] => 270
  20. [name] => 泰国香米
  21. [url] => category.php?id=270
  22. )

  23. [271] => Array
  24. (
  25. [id] => 271
  26. [name] => 日本大米
  27. [url] => category.php?id=271
  28. )

  29. )

  30. )

  31. )
复制代码