类别和子类别 [英] Categories and SubCategories

查看:191
本文介绍了类别和子类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个小时前我问了一个问题-之后决定更改我的sql结构.

I've asked a question few hours ago - afterwards decided to change my sql structure.

我有2张桌子:

categories (id,title,parent_id)
articles (id,title,content,parent_id)

每篇文章只能有1个类别,
每个类别可以有1个父类别

each article can have only 1 category,
each category can have 1 parent category


一个例子:


an example:

article (id = 1) has : parent_id = 3
category (id = 3,'root') has: parent_id = 2
category (id = 2,'sub') has: parent_id = 1
category (id = 1,'subsub') has: parent_id = 0

我想为-article (id=1)生成面包屑
其显示为:Root->sub->subsub

I want to generate breadcrumbs for - article (id=1)
That would be shown as: Root->sub->subsub

有效的方法是什么?

谢谢!

推荐答案

  1. 加载文章时,递归阅读所有类别并显示面包屑.

  1. When loading article, read all categories recursively and display breadcrumbs.

如果每个脚本需要让所有父项都超过一次(例如,当您加载50篇文章并且每篇文章都需要父项时),请使用嵌套集模型,如<一个href ="http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/" rel ="nofollow">在MySQL中管理分层数据.

If you need get all parents more than one time per script (e.g. when you are loading 50 articles and need parents for each), use the nested set model, as explained in Managing Hierarchical Data in MySQL.

这篇关于类别和子类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆