1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

php category function

Discussion in 'PHP' started by dracula51, Mar 15, 2012.

  1. #1
    Hi I am using following php code for showing categories in my site (found it online)

    //Categories
    $nav_query = $mysql->query("SELECT * FROM categories WHERE sub = 0 ORDER BY id");
    $categories = "";					// 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 )
    	{
    		$categories .= "<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']; }
    		
    		$categories .= 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:
    so its showing perfectly categories & sub-cat or sub of sub-cat..

    [COLOR="#0000CD"]TV[/COLOR]
    -->LG
    -->Samsung
    -->Sony
    [COLOR="#0000CD"]Computer[/COLOR]
    -->Desktop
    [COLOR="#B22222"]-->-->HP
    -->-->DEELL[/COLOR]
    -->Laptop
    [COLOR="#B22222"]-->-->Apple
    -->-->HP[/COLOR]
    [COLOR="#0000CD"]Car[/COLOR]
    -->Ford
    -->Toyota
    Code (markup):
    its showing all together.
    but i want it to show sub-cat only when its parent-cat is clicked (we may use if($_GET['cat_id'] == parentid) something like tht)
    like if apple selected...it will only show

    [COLOR="#0000CD"]TV[/COLOR]
    [COLOR="#0000CD"]Computer[/COLOR]
    -->Desktop
    -->Laptop
    [COLOR="#B22222"]-->-->Apple
    -->-->HP[/COLOR]
    [COLOR="#0000CD"]Car[/COLOR]
    Code (markup):
    plz help
     
    dracula51, Mar 15, 2012 IP
  2. samyak

    samyak Active Member

    Messages:
    280
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    90
    #2
    add this line
    if($_GET['cat_id']  == $nav_row['id']) </PHP] above
    
    [PHP]
    $categories .= build_child($nav_row['id']);
    PHP:
    BUt mind you this will show the deeper level categories ONLY if current category is the top level. i.e it wont show the 'Computer' if "apple' os choosen.
     
    samyak, Mar 16, 2012 IP
  3. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    already tried that one. but in this case...when u click on the parent cat, it will show sub-cat (perfect till now)
    but then when u click any sub-cat ...it doesnt show any sub-cat at all...just parent cat
     
    dracula51, Mar 16, 2012 IP