Page 1 of 1

Category and subcategory menu

Posted: Mon Apr 16, 2007 1:58 pm
by Bizzy
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi there, 

I'm PHP beginner and I'm stucked due to one problem.

I want to create menu with categories and their subcategories.


HTML:
[syntax="html"]<li>
  <a href="/cat/tshirts">T-shirts</a>
  <ol>
    <li><a href="/cat/tshirts/with-long-sleeve">with long sleeve</a></li>
    <li><a href="/cat/tshirts/poloshirts">poloshirts</a></li>
  </ol>
</li>
SQL:

Code: Select all

`category` (
  `id` int(10) unsigned NOT NULL auto_increment,
  `name` varchar(30) default NULL,
  `subid` int(11) NOT NULL default '0', // If not 0, it's subcategory of 'id'
  PRIMARY KEY  (`kid`)
)

Could anybody help me with some clean, basic solution? Thanks.


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Apr 16, 2007 4:44 pm
by feyd
Unfortunately, with the table design you've chosen there is only one solution: recursion. It's not very simple, but it's your only option without redesigning the table structure.

Posted: Tue Apr 17, 2007 2:53 am
by CoderGoblin
Have a look at Storing Hierarchical Data in a Database. You may find it helpful as it explains the two most common methods.

Posted: Tue Apr 17, 2007 9:15 am
by Bizzy
Thanks a lot.