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

Reply

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 Quick reply to this message  
Join Date: Dec 2008
Posts: 94
Reputation: sikka_varun is an unknown quantity at this point 
Solved Threads: 11
sikka_varun's Avatar
sikka_varun sikka_varun is offline Offline
Junior Poster in Training

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

 
0
  #2
Dec 2nd, 2008
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..

  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..
VâRûN
---Happy to Help---
sikka_varun@yahoo.com
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 78
Reputation: chrisranjana is an unknown quantity at this point 
Solved Threads: 3
chrisranjana chrisranjana is offline Offline
Junior Poster in Training

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

 
0
  #3
Dec 3rd, 2008
This article should help

Storing hierarchical data in database
Chris, Director, Chrisranjana.com
Web developers and software programmers.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC