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: Thanks in advance for helping me :wink:
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');
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 .= " "; } $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.
what?? you said an incomplete sentence . I already did the db query you wrote above, but still it doesnt work,
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 .= " "; } $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:
WOW DUDE !! it worked 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 .= " "; } $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:
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...