Need help with creating child categories

Discussion in 'PHP' started by nastynappy, Jan 25, 2008.

  1. #1
    I really need help with creating child categories.

    My tbl_categories (should be parent category) structure:

    mysql> select * from tbl_categories;
    +-------+---------+----------+
    | catid | catname | parentid |
    +-------+---------+----------+
    | 69 | test | 1 |
    | 70 | a | 0 |
    | 71 | s | 0 |
    +-------+---------+----------+


    My tbl_child (should be child category) structure :

    mysql> select * from tbl_child;
    +-------+----------+------------+
    | catid | parentid | catname |
    +-------+----------+------------+
    | 1 | 1 | child of 1 |
    +-------+----------+------------+


    What i need to do is:

    Show all the child of a category which have its parent id.
    i mean if child category " child of 1 " has parent id "1" , then it should be shown under parent category "test", which has parent id 1 .

    I think you understood what i meant..
    if you didnt, here is another example of what i am trying to do:

    [​IMG]

    Thanks in advance for helping me :wink:
     
    nastynappy, Jan 25, 2008 IP
  2. sharqi

    sharqi Guest

    Messages:
    105
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Each table just needs a value that can be linked to the parent.

    SELECT * FROM table WHERE parentvalue IN (SELECT parentvalue FROM childtable WHERE data = '2673');
     
    sharqi, Jan 25, 2008 IP
  3. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #3
    You don't need to do it using two tables. You can manage it using one table.

    PHP Code:
    
    <?
    function build_child($oldID)			
    {
    	global $exclude, $depth;			
    	
    	$child_query = mysql_query("SELECT * FROM `category` WHERE cat_parent=" . $oldID);
    	while ( $child = mysql_fetch_array($child_query) )
    	{
    		if ( $child['cat_id'] != $child['cat_parent'] )
    		{ 
    			$tempTree .= '<option value="' .$child['cat_id']. '">';
    			for ( $c=0;$c<$depth;$c++ )			
    			
    			{ $tempTree .= "&nbsp;"; }
    			$tempTree .= "- " . $child['cat_name'] . "</option>";
    			$depth++;		
    			$tempTree .= build_child($child['cat_id']);		
    			$depth--;		
    			array_push($exclude, $child['cat_id']);			
    		}
    		
    	}
    	
    	return $tempTree;		
    }
    echo '<select>';
    echo $tree;
    echo '</select><br>';
    ?>
    
    PHP:
    This is basic example. I have not tested it. I hope this will work and solve your problem.
     
    greatlogix, Jan 25, 2008 IP
  4. nastynappy

    nastynappy Banned

    Messages:
    499
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #4
    what??
    your functions doesnt work, shows an empty option field.
     
    nastynappy, Jan 25, 2008 IP
  5. sharqi

    sharqi Guest

    Messages:
    105
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5

    Seriously some people...
     
    sharqi, Jan 26, 2008 IP
  6. nastynappy

    nastynappy Banned

    Messages:
    499
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #6
    what??
    you said an incomplete sentence :confused:.
    I already did the db query you wrote above, but still it doesnt work,
     
    nastynappy, Jan 26, 2008 IP
  7. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #7
    I apologize. Code was not complete. I did copy paste from one of my project's source file and did not notice that it's not complete. Here is complete code.I have tested it. This will create a select list of categories and subcategories with little indentation. Use this to create new sub cats(cats=categories).

    If admin/member selects a cat and enter a new name in text field than add selected cat value as new cat's parent. If no cat is selected from list than add cat parent zero. If cat parent is zero, It means that it's a top cat.

    <?php
    
    $nav_query = mysql_query("SELECT * FROM `category` ORDER BY `cat_id`");
    $tree = "";					
    $depth = 3;					
    $top_level_on = 1;			
    $exclude = array();			
    array_push($exclude, 0);	
    
    while ( $nav_row = mysql_fetch_array($nav_query) )
    {
    	$goOn = 1;			
    	for($x = 0; $x < count($exclude); $x++ )		
    	{
    		if ( $exclude[$x] == $nav_row['cat_id'] )
    		{
    			$goOn = 0;
    			break;				
    		}
    	}
    	if ( $goOn == 1 )
    	{
    		$tree .= '<option value="'.$nav_row['cat_id']. '">'. $nav_row['cat_name'] . "</option>";				
    		array_push($exclude, $nav_row['cat_id']);		
    		if ( $nav_row['cat_id'] < 6 )
    		{ $top_level_on = $nav_row['cat_id']; }
    		
    		$tree .= build_child($nav_row['cat_id']);		
    	}
    }
    function build_child($oldID)           
    {
        global $exclude, $depth;           
       
        $child_query = mysql_query("SELECT * FROM `category` WHERE cat_parent=" . $oldID);
        while ( $child = mysql_fetch_array($child_query) )
        {
            if ( $child['cat_id'] != $child['cat_parent'] )
            {
                $tempTree .= '<option value="' .$child['cat_id']. '">';
                for ( $c=0;$c<$depth;$c++ )   
               
                { $tempTree .= "&nbsp;"; }
                $tempTree .= "--" . $child['cat_name'] . "</option>";
                $depth++;      
                $tempTree .= build_child($child['cat_id']);  
                $depth--;      
                array_push($exclude, $child['cat_id']);   
            }
           
        }
       
        return $tempTree;      
    }
    echo '<select>';
    echo $tree;
    echo '</select><br>';
    ?>
    
    PHP:
     
    greatlogix, Jan 26, 2008 IP
  8. nastynappy

    nastynappy Banned

    Messages:
    499
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #8
    WOW DUDE !! it worked :D
    Now, can you please tell me how to add childs to existing parents and childs using input and forms?
    and will it work if i set int(10) instead of mediumint(5) ??
    and BTW, why did you use mediumint ?? whats the difference between both of them?

    and can you please tell me how can i show them in this way ??

    [Top] (just a text)
    |___Parent
    | |___Child1
    | |___Child2
    | |___Child3
    | |___Child4
    |___Parent
    | |___Child1
    | |___Child2
    | |___Child3
    |___Child4


    ???

    would be really appreciated if you could..

    so far, i have done this :

    |___Parent
    | |___Child1
    | |___Child2
    | |___Child3
    | |___Child4
    |___Parent
    | |___Child1
    | |___Child2
    | |___Child3
    | |___Child4


    BUT i want [Top] on the top of the options
    and i need the | to be removed from the last child, only if it is a child, not if its a parent..

    My edited code is :

    
    <?php
    include_once "init.php";
    
    $nav_query = mysql_query("SELECT * FROM `category` ORDER BY `cat_id`");
    $tree = "";     
    $depth = 2;     
    $top_level_on = 1;   
    $exclude = array();   
    array_push($exclude, 0);    
    
    while ( $nav_row = mysql_fetch_array($nav_query) )
    {
        $goOn = 1;      
        for($x = 0; $x < count($exclude); $x++ )        
        {
            if ( $exclude[$x] == $nav_row['cat_id'] )
            {
                $goOn = 0;
                break;        
            }
        }
        if ( $goOn == 1 )
        {
            $tree .= '<option value="'.$nav_row['cat_id']. '">|___'. $nav_row['cat_name'] . '</option>';                
            array_push($exclude, $nav_row['cat_id']);      
            if ( $nav_row['cat_id'] < 6 )
            { $top_level_on = $nav_row['cat_id']; }
            
            $tree .= build_child($nav_row['cat_id']);      
        }
    }
    
    function build_child($oldID)           
    {
        global $exclude, $depth;           
    
        $child_query = mysql_query("SELECT * FROM `category` WHERE cat_parent=" . $oldID);
        while ( $child = mysql_fetch_array($child_query) )
        {
            if ( $child['cat_id'] != $child['cat_parent'] )
            {
                $tempTree .= '<option value="' .$child['cat_id']. '">|';
                for ( $c=0;$c<$depth;$c++ ) {
    			$tempTree .= "&nbsp;";
    			}
                $tempTree .= "|___" . $child['cat_name'] . "</option>";
                $depth++;      
                $tempTree .= build_child($child['cat_id']);  
                $depth--;      
                array_push($exclude, $child['cat_id']);   
            }
           
        }
       
        return $tempTree;      
    }
    echo '<select>';
    echo $tree;
    echo '</select><br>';
    
    ?>
    
    PHP:
     
    nastynappy, Jan 26, 2008 IP
  9. nastynappy

    nastynappy Banned

    Messages:
    499
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #9
    cant anyone please help me??


    Edited : ok , no help needed anymore, i coded it myself, but i am deeply disappointed that no one here could help me... :(
     
    nastynappy, Feb 1, 2008 IP