|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Quick Example: How to show something hierarchically (tree structure)
Here's another quick example.. hope you'll enjoy it.. here's a way to show categories hierachically.. I'm using a MySQL database in the exampe, so I guess I'll just start by giving you the table structure:
Code:
CREATE TABLE categories ( id mediumint(11) unsigned NOT NULL auto_increment, name varchar(255) NOT NULL default '0', parent tinyint(11) unsigned NOT NULL default '0', PRIMARY KEY (id) ) TYPE=MyISAM; And here's some table data: Code:
INSERT INTO categories (name, parent) VALUES("Category 1", "0");
INSERT INTO categories (name, parent) VALUES("Category 2", "0");
INSERT INTO categories (name, parent) VALUES("Category 3", "0");
INSERT INTO categories (name, parent) VALUES("Category 4", "0");
INSERT INTO categories (name, parent) VALUES("Subcategory 1", "2");
INSERT INTO categories (name, parent) VALUES("Subcategory 2", "2");
INSERT INTO categories (name, parent) VALUES("Subcategory 3", "5");
INSERT INTO categories (name, parent) VALUES("Subcategory 4", "9");
INSERT INTO categories (name, parent) VALUES("Category 5", "0");
As you can see, the table layout is quite simple.. here's an example of how it works (simplified a bit): Code:
|----|---------------|--------| | id | name | parent | |----|---------------|--------| | 1 | Category 1 | 0 | | 2 | Subcategory 1 | 1 | | 3 | Subcategory 2 | 1 | | 4 | Subcategory 3 | 2 | |----|---------------|--------| According to the simplified example above, the output would be: Code:
Category 1 -- Subcategory 1 ---- Subcategory 3 -- Subcategory 2 All right.. let's go on to the nasty part.. the actual code.. if you've got any better way to this, or have any suggestions for the already existing code, feel free to e-mail it to me at lindset@webpixels.net, or just post it in this thread.. ![]() PHP Code:
Ok.. to help you visualize the array in your head, here's an example of the structure after processing: Code:
Array
(
[0] => Array
(
[id] => 1
[name] => Category 1
[parent] => 0
[level] => 0
)
[1] => Array
(
[id] => 2
[name] => Category 2
[parent] => 0
[level] => 0
)
[2] => Array
(
[id] => 5
[name] => Subcategory 1
[parent] => 2
[level] => 1
)
[3] => Array
(
[id] => 7
[name] => Subcategory 3
[parent] => 5
[level] => 2
)
[4] => Array
(
[id] => 6
[name] => Subcategory 2
[parent] => 2
[level] => 1
)
[5] => Array
(
[id] => 3
[name] => Category 3
[parent] => 0
[level] => 0
)
[6] => Array
(
[id] => 4
[name] => Category 4
[parent] => 0
[level] => 0
)
[7] => Array
(
[id] => 9
[name] => Category 5
[parent] => 0
[level] => 0
)
[8] => Array
(
[id] => 8
[name] => Subcategory 4
[parent] => 9
[level] => 1
)
)
To display it correctly, all you have to do is iterate through the array.. like you see at the end of the example.. here's the final output of the php code above: Code:
Category 1 Category 2 --Subcategory 1 ----Subcategory 3 --Subcategory 2 Category 3 Category 4 Category 5 --Subcategory 4 What do you think? I haven't commented the code heavily, so, if you're having difficulties understanding a part of the code, feel free to ask here, and I'll try to answer.. ![]()
__________________
Best Regards, Håvard Lindset Last edited by Lindset : November 5th, 2002 at 04:26 PM. |
|
#2
|
|||
|
|||
|
you forgot something :/
Code:
CREATE TABLE categories (
C_ID mediumint(8) unsigned NOT NULL auto_increment,
C_TITLE varchar(100) NOT NULL default '',
C_ORDER mediumint(8) unsigned NOT NULL default '0',
C_ACTIVE enum('Y','N') NOT NULL default 'Y',
C_PARENT tinyint(11) unsigned NOT NULL default '0',
PRIMARY KEY (C_ID)
) TYPE=MyISAM;
Key parts are Order, and Status ![]() |
|
#3
|
|||
|
|||
|
Quote:
I'm not sure what you mean.. |
|
#4
|
|||
|
|||
|
well,...how are you going order your categories? by Name? that can get confusing for users if you keep adding categories. So I have an Order Field, which I store my order in.
The Status is for the admin f he wishes to turn off a category without deleting anything asociated to it ![]() Name Order Status Cat 1 1 Y Cat 2 2 Y Cat 5 3 N Cat 123 4 Y In this case,....this would print,...Cat 1, 2 and 123 but not 5 Make sense? ![]() -- Jason |
|
#5
|
|||
|
|||
|
Quote:
Ok, but I didn't want to make the example more complicated than it already is.. |
|
#6
|
|||
|
|||
|
not complicated at all
![]() PHP Code:
blammo,...there ya go ![]() |
|
#7
|
|||
|
|||
|
thanks
but the example is simply supposed to show how to display something in a tree structure (not necessarily categories), that's why I didn't want to make it too specific for example categories.. when showing examples, you want to simplify it so it's easier for the audience to understand.. ![]() |
|
#8
|
|||
|
|||
|
easy is not always best
![]() |
|
#9
|
|||
|
|||
|
I'm working on a site to display a tree view of registered users. So far i can only work out how to display the first level.. to see what i mean, its on this page:
URL The tables include these fields currently being used: ------------------------------------------------ | id | Firstname | Surname | Sponsor | ------------------------------------------------ | 1 | Don | H | 1 | | 2 | Number 2 | 2 | 1 | etc ------------------------------------------------ Where you have id and parent, i have id and sponsor. I'd like to use the script here, but when i implement it and change the names i get this warning message: ---------------- Warning: Invalid argument supplied for foreach() ---------------- The echo command is not writing anything to the page, and the warning message identifies the final 'foreach' at the end of the code. Any idea what could be causing this? Thanks, Don |
|
#10
|
|||
|
|||
|
Don,
Would it be possible to post the code you're using? If might give us a better idea of what could be going wrong.... How are you structuring your foreach loop?
__________________
____________________________________________ Developer Shed Weekly Writer | DevArticles Forum Moderator Build Your Own KlipFolio Klip With PHP FrankManno.com - Under Construction Design Interactive Group - Under Construction |
|
#11
|
|||
|
|||
|
How would you link a Subcategory, to it's right article that's in another database, Name Articles ?
Here's what I'm trying to do. Add Topics Titles to the database. Then add topics that would be listed as subcategories, example: Topic 1 PHP --Articles about php --> URL is: topics.php?sub=ID# --Articles about functions --> URL is: topics.php?sub=ID# Topic 2 ASP --Articles about php --> URL is: topics.php?sub=ID# --Scripts --> URL is: topics.php?sub=ID# The URL [?Id=ID#] would then list all articles of that subcategory. I guess I'm trying to relate subTopics ID to the related Article ID. I wanted to post a TopicTitle, Then a TopicSub, So, The TopicSub URL would pull all Articles that where related to that TopicSub. I'm kind-0f , If you can tell. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > Quick Example: How to show something hierarchically (tree structure) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|