View Single Post
Join Date: Aug 2007
Posts: 64
Reputation: nil_gh_80 is an unknown quantity at this point 
Solved Threads: 0
nil_gh_80's Avatar
nil_gh_80 nil_gh_80 is offline Offline
Junior Poster in Training

Category hierarchy problem............

 
0
  #1
Dec 2nd, 2008
Hello friends,

I've a category table, the structure is:

category{
id int(11) autoincrement,
name varchar(255),
parentid int(11);
}

Now a node can have multiple child nodes and grand child nodes. now if I want to get all childnodes and grandchild nodes of a node id what should i do ???????


I've written a recursive function but this is not working properly,

  1.  
  2. //mysql connection
  3.  
  4.  
  5. $objcatId = new catId();
  6. $rsId[] = $objcatId->prod_view(22);
  7. print_r($rsId);
  8.  
  9. class catId{
  10.  
  11. var $id;
  12.  
  13. function prod_view($id){
  14.  
  15. $sql = mysql_query("select id from cart_category where pid='$id'") or die(mysql_error());
  16. $num = mysql_num_rows($sql);
  17.  
  18. if($num > 0){
  19.  
  20. while($row = mysql_fetch_array($sql)){
  21.  
  22. $idArray[] = $row['id'];
  23.  
  24. $this->prod_view($row['id']);
  25. }
  26.  
  27. }
  28. //print_r($idArray);
  29. return $idArray;
  30. }
  31.  
  32. }
Last edited by peter_budo; Dec 2nd, 2008 at 8:15 pm. Reason: Please, either use [code] or [code=php] (or other langiage as necessary). DO NOT USE [code language=php]
Reply With Quote