RSS Forums RSS

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

Reply
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............

  #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 7:15 pm. Reason: Please, either use [code] or [code=php] (or other langiage as necessary). DO NOT USE [code language=php]
AddThis Social Bookmark Button
Reply With Quote  
Posts: 79
Reputation: sikka_varun is an unknown quantity at this point 
Solved Threads: 9
sikka_varun's Avatar
sikka_varun sikka_varun is offline Offline
Junior Poster in Training

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

  #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..

//mysql connection


$objcatId = new catId();
$rsId[] = explode(",",$objcatId->prod_view(22));
print_r($rsId);

class catId{

static $idvalues="";

function prod_view($id)
{

	$sql = mysql_query("select id from cart_category where pid='$id'") or die(mysql_error());
	$num = mysql_num_rows($sql);

	if($num > 0)
	{

		while($row = mysql_fetch_array($sql))
		{
			if(self::$idvalues=="")
			{
				self::$idvalues=$row['id'];
			}
			else
			{
				self::$idvalues.=",".$row['id'];
			}
			
			$this->prod_view($row['id']);

		}
	
	}

	return self::$idvalues;
} 

}


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  
Posts: 72
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............

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Similar Threads
Other Threads in the PHP Forum
Views: 339 | Replies: 2 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:44 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC