Managing thread-based data structures

Status
Not open for further replies.

Tekime

Smack-Fu Master, in training
85
I'm currently working on a content manager project in PHP/MySQL. I have implemented the ability to categorize articles by category and type of article, but I want to expand on this and give more flexibility in organizing the site structure.

Ultimately I want a thread based content system, which gives the ability to define categories to whatever depth you want, and also keep track of which articles belong in which category and sub-category.

I've had a hard time finding much info on thread-based web programming, and I'm struggling to come up with a good model for both my articles db and my categories db.

Should I define my categories to have a parent-category field, and then use PHP to sort everything out during process time? Would it be smarter to define all the children as well, to avoid searching every category for its parent, or would this make it tougher by having to update every category entry when adding new ones?

Any links, ideas, etc. would be very very helpful now. I'm relatively new to PHP and an amateur programmer as it is, so I want to have a good concept of how this system should function before I start laying out code.

Thanks in advance :)
 
It's simple, how do you define a graph in database......

Field 1 [parentID], Field 2 [childID]

From there, it's recursive traversals. The only thing you'll need to do is make sure you don't define an infinite loop (may ways to do this). If I can do this in the hella dreaded CoBOL(trembles and pukes), you should have no problem.



Code:
//
// pseudo garbage
//


Traverse(nodeID) {
  recordset = Find("select * from table where parentID = "+ nodeID);

  For each X in recordset {
     Traverse(X.childID);
  }
}

One way to test for an infinite loop is to do a traversal on the "new child" and see if ParentID is ever listed as a childID in that traversal.
 

Tekime

Smack-Fu Master, in training
85
Thanks JoeJoe, you made it sound much easier :)

After some help over at SF as well, I've made a temp db to test things out and I've got a Traverse() function as well as a TraverseUp() function to go back up the tree.

I've been playing with the idea of using two tables, one for category and one for category/sub-category pairs with foreign keys into the category table. This can let a category be a category/sub-category or both. Thing is foreign keys are giving me a hard time in MySQL (loking at InnoDB), and also I'm not sure that much flexibility would really contribute to the consistency of the model.

Thanks again, any other suggestions on where to take this or especially some tips on foreign keys would be helpful.
 
Primary Key is easy, use both [parentID] and [childID]. Both columns should be indexed.

For a foreign key, the "cheap" way to do it is to have an auto-number field as the foreign key. If the DB won't do auto numbers, you can always force the field the have unique values and use "select max(foreignkey) + 1" for new records. This will kill over if two people try to insert the the exact same value, so some graceful fails are a must if you have to do it outside of the DB. Actually, if it fails to insert, you can get try to read "max(foreingkey) + 1" again and insert again. You can repeat this process until it succeeds or until it happens too many times. This way, the user will most likely never even get an error.

If you're going to use any replication, database-side auto-numbers should not be used at all (found this out the hardway).
 
Status
Not open for further replies.