Category and subcategory menu

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
Bizzy
Forum Newbie
Posts: 10
Joined: Mon Apr 16, 2007 1:15 pm

Category and subcategory menu

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
Bizzy
Forum Newbie
Posts: 10
Joined: Mon Apr 16, 2007 1:15 pm

Post by Bizzy »

Thanks a lot.
Post Reply