943,829 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 555
  • PHP RSS
Dec 2nd, 2008
0

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

Expand Post »
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,

php Syntax (Toggle Plain Text)
  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]
Similar Threads
Reputation Points: 8
Solved Threads: 2
Junior Poster in Training
nil_gh_80 is offline Offline
64 posts
since Aug 2007
Dec 2nd, 2008
0

Re: Category hierarchy problem............

Hii...
What it seems from your code is, you are storing the value after mysql_fetch_array in an array without an id..
So try this code..

PHP Syntax (Toggle Plain Text)
  1. //mysql connection
  2.  
  3.  
  4. $objcatId = new catId();
  5. $rsId[] = explode(",",$objcatId->prod_view(22));
  6. print_r($rsId);
  7.  
  8. class catId{
  9.  
  10. static $idvalues="";
  11.  
  12. function prod_view($id)
  13. {
  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.  
  21. while($row = mysql_fetch_array($sql))
  22. {
  23. if(self::$idvalues=="")
  24. {
  25. self::$idvalues=$row['id'];
  26. }
  27. else
  28. {
  29. self::$idvalues.=",".$row['id'];
  30. }
  31.  
  32. $this->prod_view($row['id']);
  33.  
  34. }
  35.  
  36. }
  37.  
  38. return self::$idvalues;
  39. }
  40.  
  41. }


Try this code... The system im using right now doesnt have PHP installed.. so cant check it here.. Hope this helps you.. If any problem comes, then tell me the error..
Reputation Points: 11
Solved Threads: 12
Junior Poster in Training
sikka_varun is offline Offline
94 posts
since Dec 2008
Dec 3rd, 2008
0

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

This article should help

Storing hierarchical data in database
Reputation Points: 11
Solved Threads: 3
Junior Poster in Training
chrisranjana is offline Offline
82 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: search from database
Next Thread in PHP Forum Timeline: PHP Gaming basics





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC