Tree structured categories problem :(

Discussion in 'PHP' started by dracula51, Sep 22, 2011.

  1. #1
    Hello

    mysql table stracture

    id----sub----name

    where id is the category's id & sub is parent id of tht category (root category's sub is 0)

    i used this codes

    //Categories
    $nav_query = $mysql->query("SELECT * FROM categories WHERE sub = 0 ORDER BY id");
    $tree = "";					// Clear the directory tree
    $depth = 1;					// Child level depth.
    $top_level_on = 1;			// What top-level category are we on?
    $exclude = array();			// Define the exclusion array
    array_push($exclude, 0);	// Put a starting value in it
     
    while ( $nav_row = $mysql->fetch_array($nav_query) )
    {
    	$goOn = 1;			// Resets variable to allow us to continue building out the tree.
    	for($x = 0; $x < count($exclude); $x++ )		// Check to see if the new item has been used
    	{
    		if ( $exclude[$x] == $nav_row['id'] )
    		{
    			$goOn = 0;
    			break;				// Stop looking b/c we already found that it's in the exclusion list and we can't continue to process this node
    		}
    	}
    	if ( $goOn == 1 )
    	{
    		$tree .= "<a href='category.php?cid=".$nav_row['id']."'>" . $nav_row['name'] . "</a><br>";				// Process the main tree node
    		array_push($exclude, $nav_row['id']);		// Add to the exclusion list
    		if ( $nav_row['id'] < 6 )
    		{ $top_level_on = $nav_row['id']; }
    		
    		$tree .= build_child($nav_row['id']);		// Start the recursive function of building the child tree
    	}
    }
    
    
    
    function build_child($oldID)			// Recursive function to get all of the children...unlimited depth
    {
    	global $exclude, $depth, $mysql, $getcid;			// Refer to the global array defined at the top of this script
    	$tempTree = '';	
    	$child_query = $mysql->query("SELECT * FROM categories WHERE sub=" . $oldID);
    	while ( $child = mysql_fetch_array($child_query))
    	{
    		if ( $child['id'] != $child['sub'] )
    		{
    			
    			for ( $c=0;$c<$depth;$c++ )			// Indent over so that there is distinction between levels
    			{ $tempTree .= "&nbsp;"; }
    			$tempTree .= "- <a href='category.php?cid=".$child['id']."'>" . $child['name'] . "</a><br>";
    			$depth++;		// Incriment depth b/c we're building this child's child tree  (complicated yet???)
    			$tempTree .= build_child($child['id']);		// Add to the temporary local tree
    			$depth--;		// Decrement depth b/c we're done building the child's child tree.
    			array_push($exclude, $child['id']);			// Add the item to the exclusion list
    		}
    	}
    	return $tempTree;		// Return the entire child tree
    }
    PHP:
    just to let u knw, if u noticed...i used $mysql class which i added before this code...u may use normal mysql functions
    Code (markup):
    Now its showing categories fine...

    Category 1
    --Category 1.1
    ------Category 1.1.1
    ------Category 1.1.2
    --Category 1.2
    --Category 1.3
    ------Category 1.3.1
    Category 2
    Category 3
    --Category 3.1

    but all together

    i want a sub category showed when its parents category is clicked
    like when someone clicked category 1 page it will show those 3 sub...


    anyone? help??
     
    dracula51, Sep 22, 2011 IP