| | |
Category hierarchy problem............
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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,
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)
//mysql connection $objcatId = new catId(); $rsId[] = $objcatId->prod_view(22); print_r($rsId); class catId{ var $id; 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)){ $idArray[] = $row['id']; $this->prod_view($row['id']); } } //print_r($idArray); return $idArray; } }
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]
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..
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..
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)
//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..
•
•
Join Date: Jul 2005
Posts: 78
Reputation:
Solved Threads: 3
Chris, Director, Chrisranjana.com
Web developers and software programmers.
Web developers and software programmers.
![]() |
Similar Threads
- 0x8004010F error in all Offline Address Books from Exchange 2000 server (Windows NT / 2000 / XP)
Other Threads in the PHP Forum
- Previous Thread: search from database
- Next Thread: PHP Gaming basics
| Thread Tools | Search this Thread |
301 apache api array autosuggest beginner beneath binary broadband broken button cakephp checkbox class cms code compression countingeverycharactersfromastring crack cron curl database date decode display dynamic echo email error file files folder form forms function functions google href htaccess html httppost image include insert integration ip javascript joomla limit link links login mail match md5 menu mlm multiple mysql mysql_real_escape_string oop paypal pdf php problem protocol query radio random recursion remote script search searchbox server session sessions sms smtp source space sql strip_tags survey syntax system table tutorial update upload url validator variable video virus votedown web website window.onbeforeunload=closeme; youtube





