vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Unlimited Category Tree - Need Help (https://vborg.vbsupport.ru/showthread.php?t=116220)

roosevelt 05-21-2006 12:36 AM

Unlimited Category Tree - Need Help
 
For a while I've been trying to make this system and so far it goes up to one level. I am pretty sure there's a way to display unlimited category levels but that's what I just don't know. Anyways I will not post the actual source code but the concept should be enough for a more advanced PHP programmer.

Table
- CatId
- CatName
- CatParent

Example
CatId CatName CatParent (or subof)
1 Man 0
2 Woman 0
3 Boy 1
4 Girl 2
5 Baby Girl 4
6 Baby Boy 3

PHP Code:

$sql mysql_query("SELECT * FROM categorytable WHERE CatParent = '0'");
while (
$data mysql_fetch_array($sql))
{
 echo 
"{$data['CatName']}<br />";
 
$sql2 mysql_query("SELECT * FROM categorytable WHERE CatParent = '".$data['CatId']."'");
 echo 
"- {$data2['CatName']}<br />";


My little while loop produces this:
Man
- Boy
Woman
- Girl

So if I wish to display Baby Girl category under Girl I will need another loop just under $sql2. Now the problem is I don't really know how deep the user will create his categories. So what is the trick to make unlimited categories?

Thanks a bunch in advance!!

hambil 05-21-2006 04:08 PM

You need to add a CatRoot, then you can get it all in one query.

Table
- CatId
- CatName
- CatParent
- CatRoot

Example
CatId CatName CatParent (or subof)
1 Man 0 1
2 Woman 0 2
3 Boy 1 1
4 Girl 2 2
5 Baby Girl 4 2
6 Baby Boy 3 1

PHP Code:

$sql mysql_query("SELECT * FROM " TABLE_PREFIX "categorytable ORDER BY CatRoot, CatParent");
while (
$data mysql_fetch_array($sql))
{
 echo 
$data['CatName'] . "<br />";




All times are GMT. The time now is 10:27 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01039 seconds
  • Memory Usage 1,718KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (2)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete