How I can create trees via DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bagi
Forum Newbie
Posts: 24
Joined: Thu Oct 31, 2013 10:50 am

How I can create trees via DB

Post by bagi »

I wanna create shop. It will have some categories (for example: Electronics -> Tablets -> iPad or Antique->Coins or Electronics -> PCs). As you see, I have dunamic quantity of "levels". And here is a question: how I can store the trees and display it in a browser? All categories must be clickable. I had an idea to store the categories in other table (as text) and compare it with numeric id, but I think that is bad idea. Is there a better way?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How I can create trees via DB

Post by requinix »

bagi wrote:I had an idea to store the categories in other table (as text) and compare it with numeric id, but I think that is bad idea.
It might not be, depending on what you were thinking (hard to tell with just that description).

Two tables.

First table is just for a list of categories. It has an ID for each category, the name of course, and an optional ID for the parent category's ID (if the category has a parent category). Maybe other fields if you want.

Code: Select all

id | name        | parent
---+-------------+-------
1  | Electronics | NULL
2  | Tablets     | 1
3  | iPad        | 2
4  | Antique     | NULL
5  | Coins       | 4
6  | PCs         | 1
Second table links your products with their categories. There could be more than one category for a product so you need a separate table.

Code: Select all

product | category
--------+---------
    123 | 3         (if product ID=123 is an iPad)
    234 | 2         (if product ID=234 is a non-iPad tablet)
    345 | 2         (product ID=345 is that one Windows 8 laptop that's also a tablet)
    345 | 6
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: How I can create trees via DB

Post by Eric! »

While this example is cakephp specific, they do a good job at showing how to manage and use trees in a database using a parent then left and right. This follows the idea of Modified Preorder Tree Traversal (MPTT) logic.
bagi
Forum Newbie
Posts: 24
Joined: Thu Oct 31, 2013 10:50 am

Re: How I can create trees via DB

Post by bagi »

Oh, yeah! That's I need. But how I can create "union" with two variables? For example, in c++ i can create struct with two variables and via push_back method (in object of vector class) add it to tail. After I have assess to all data by id: 0..size()-1 and I have ids and names don't touched. How I can realize it in php? I wanna get id,name (of category and all parents) and add it into array. After I would like to have access do like this: categories[0].name categories[0].id (this is general parent), categories[1].name categories[1].id (this is child of general parent) and ect
Post Reply