DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   PHP (http://www.daniweb.com/forums/forum17.html)
-   -   Category hierarchy problem............ (http://www.daniweb.com/forums/thread160394.html)

nil_gh_80 Dec 2nd, 2008 6:15 am
Category hierarchy problem............
 
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,


//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;
        }       

}

sikka_varun Dec 2nd, 2008 7:38 am
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..

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

chrisranjana Dec 3rd, 2008 9:28 am
Database Category hierarchy problem............
 
This article should help

Storing hierarchical data in database


All times are GMT -4. The time now is 9:12 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC