AdamPGT Posted March 30, 2006 Share Posted March 30, 2006 So I'm building a content/document management syste and it's turned a bit more complicated than I had anticipated. The problem I've run into is how I should setup my DB tables. Basically, I would like some outside input on DB structure so I can get my navigation to work like it should.The navigation has a Parent Category, with sub-categories and pages. But each sub category should have the ability to have subs and pages, etc. Now some of these sub-categories will be uploaded PDF docs.The tricky part of the navigation, or at least the part I'm having a bit of trouble with is that I only want the "active" category to be expanded. Kind of like this:-- No Active sub-catParent Category---> Sub Category 1---> Sub Category 2---> Sub Category 3-- Active sub-catParent Category---> Sub Category 1---> Sub Category 2-------> Sub-Sub Category 1-------> Sub-Sub Category 2-------> Sub-Sub Page 1---> Sub Category 3-- Active sub-sub-catParent Category---> Sub Category 1---> Sub Category 2-------> Sub-Sub Category 1----------> Sub-Sub-Sub Category 1----------> Sub-Sub-Sub Category 2-------> Sub-Sub Category 2-------> Sub-Sub Page 1---> Sub Category 3And so on and so on. Any DB setup suggestions? I don't need any code help or anything, just figured someone would have a suggestion that would give me a push in the right direction.--Adam Link to comment https://forums.phpfreaks.com/topic/6187-need-some-advice/ Share on other sites More sharing options...
Barand Posted March 30, 2006 Share Posted March 30, 2006 Try something like thiscategories table[code]ID | parentID | catname | [/code]Top-level categories have parentID = 0.[code]session_start();include 'db.php'; // set up the array if it isn't there if (!isset($_SESSION['cats'])) { $res = mysql_query( "SELECT ID, catname, parentID FROM categories"); while (list($i, $n, $p) = mysql_fetch_row($res)) { $_SESSION['cats'][$i] = array('parent'=>$p, 'name'=>$n, 'active'=>0); } } if (isset($_GET['selected'])) { clearActive(); setActive($_GET['selected']); } displayCategory(0);//-------- supporting functions ---------------------------------function clearActive() { foreach($_SESSION['cats'] as $kid => $karray) { $_SESSION['cats'][$kid]['active'] = 0; }}function setActive ($i) { foreach($_SESSION['cats'] as $kid => $karray) { if ($kid == $i) { $_SESSION['cats'][$kid]['active'] = 1; // set parents active also if ($karray['parent'] != 0) setActive($karray['parent']); } }}function displayCategory($id, $level=0) { foreach($_SESSION['cats'] as $kid => $karray) { if ($karray['parent'] == $id) { $p = 10*$level; echo "<span style='padding-left: {$p}px' onClick='location.href=\"?selected=$kid\"'>" . $karray['name'] . '</span><br>'; if ($karray['active']) displayCategory($kid, $level+1); } }}[/code] Link to comment https://forums.phpfreaks.com/topic/6187-need-some-advice/#findComment-22393 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.