dk4210 Posted January 24, 2011 Share Posted January 24, 2011 Hello Guys, Need a little help here. I have a db structure like this cat_id | cat_name | cat_status | parent_id 1 First Test 1 0 2 Just testing 1 0 4 Books 1 2 5 Cars 1 0 6 Ford 1 5 If the parent_id = 0 it is considered to be the parent. If it is a number it is considered a child and referencing the parent I want to be able to query the database and grab the cat and the sub cat and echo it on my page. Its like a classified ads application. Here is my while loop that I currently have which only echo's out the parent category. I would like to echo the child category under the respective parent category. I think I would have to do a loop within a loop, but not sure how to do it.. $query = "SELECT * FROM ad_category WHERE cat_status='1' && Parent_id='0' ORDER BY cat_name"; $result = mysql_query($query); while($row = mysql_fetch_row($result)) { $cat_name = $row[1]; $cupper = UCWords($cat_name); echo $cupper . "<br>" ; } Thanks for your help in advance Quote Link to comment Share on other sites More sharing options...
johnny86 Posted January 24, 2011 Share Posted January 24, 2011 Here are two functions that are ready-made, you'll have to modify them just a little bit for your needs but it's not a biggie. http://articles.sitepoint.com/article/hierarchical-data-database Quote Link to comment Share on other sites More sharing options...
dk4210 Posted January 25, 2011 Author Share Posted January 25, 2011 I found this code but I cant seem to get it to work right. The issue is that if the parent id =0 than its the parent. If its some other number it is considered to be the child and it is referencing the parent. $nav_query = MYSQL_QUERY("SELECT * FROM ad_category WHERE cat_status='1' && Parent_id='0' ORDER BY cat_name"); $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['cat_name'] ) { $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 .= $nav_row['cat_name'] . "<br>"; // Process the main tree node ARRAY_PUSH($exclude, $nav_row['cat_id']); // Add to the exclusion list IF ( $nav_row['cat_id'] < 6 ) { $top_level_on = $nav_row['cat_id']; } $tree .= build_child($nav_row['cat_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; // Refer to the global array defined at the top of this script $child_query = MYSQL_QUERY("SELECT * FROM `ad_categoy` WHERE parent_id=" . $oldID); WHILE ( $child = MYSQL_FETCH_ARRAY($child_query) ) { IF ( $child['cat_id'] != $child['parent_id'] ) { FOR ( $c=0;$c<$depth;$c++ ) // Indent over so that there is distinction between levels { $tempTree .= " "; } $tempTree .= "- " . $child['title'] . "<br>"; $depth++; // Incriment depth b/c we're building this child's child tree (complicated yet???) $tempTree .= build_child($child['cat_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['cat_id']); // Add the item to the exclusion list } } RETURN $tempTree; // Return the entire child tree } ECHO $tree; Quote Link to comment Share on other sites More sharing options...
dk4210 Posted January 25, 2011 Author Share Posted January 25, 2011 Thanks for the help, but I found something that works really well.. http://www.codeassembly.com/How-to-display-infinite-depth-expandable-categories-using-php-and-javascript/ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.