Jump to content

Recommended Posts

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-cat
Parent Category
---> Sub Category 1
---> Sub Category 2
---> Sub Category 3

-- Active sub-cat
Parent 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-cat
Parent 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 3

And 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

Try something like this

categories 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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.