我有子类别但想要显示完整的类别路径 [英] I have sub-categories but want to display full category path

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

问题描述

假设我有一个简单的Web应用程序,只运行两个MySQL

表。表结构如下:


表:类别

category_id(PK)

category_name

parent_category(FK)//引用此表中的category_id


表:链接

link_id(PK)

link_name

link_description

category_id(FK)//引用类别表中的category_id


这是类别表中的数据

1,England,0

2,West Yorkshire,1

3,Batley,2

4,Leeds,2

5,布拉德福德,2

正如你所看到的,巴特利,利兹和布拉德福德是西部的子类别。
约克郡本身是英格兰的子类别。


当我浏览链接的子类别时,我想要显示的是

不仅仅是名称子类别,但它是'类别'的父母,盛大

父母等等,如下例所示。


英国>西约克郡> ;巴特利


我在很多目录网站上看到了这一点,但我的PHP书籍都没有涵盖

这是怎么做的。必须非常简单,所以有人请指出我在

正确的方向。希望我已经解释得很好了,不知道是什么

这个过程被称为。


干杯


Phil

解决方案

我认为这不是一个好的桌面设计。你需要首先学习数据库

规范化。
http://dev.mysql.com/tech-resources/...alization.html


-
http://www.mastervb.net
http://www.theukmap.com

2月2日上午7:58 ,Phil Latio < phil.la ... @ f-in-stupid.co.ukwrote:


假设我有一个只运行两个的简单Web应用程序MySQL

表。表结构如下:


表:类别

category_id(PK)

category_name

parent_category(FK)//引用此表中的category_id


表:链接

link_id(PK)

link_name

link_description

category_id(FK)//引用类别表中的category_id


这是类别表中的数据

1,England,0

2,West Yorkshire,1

3,Batley,2

4,Leeds,2

5,布拉德福德,2

正如你所看到的,巴特利,利兹和布拉德福德是西部的子类别。
约克郡本身是英格兰的子类别。


当我浏览链接的子类别时,我想要显示的是

不仅仅是名称子类别,但它是'类别'的父母,盛大

父母等等,如下例所示。


英国>西约克郡> ;巴特利


我在很多目录网站上看到了这一点,但我的PHP书籍都没有涵盖

这是怎么做的。必须非常简单,所以有人请指出我在

正确的方向。希望我已经解释得很好了,不知道是什么

这个过程被称为。


干杯


Phil





" lorento" < la ********** @ yahoo.comwrote in message

news:11 ******************** **@q2g2000cwa.googlegro ups.com ...


>我认为这不是一个好的桌面设计。你需要首先学习数据库

规范化。
http://dev.mysql.com/tech-resources/...alization.html


-
http://www.mastervb.net
http://www.theukmap.com

2月2日上午7:58 ,Phil Latio < phil.la ... @ f-in-stupid.co.ukwrote:


>让我们说我有一个简单的Web应用程序运行只有两个MySQL
表。表格结构如下:

表格:类别
category_id(PK)
category_name
parent_category(FK)//引用此表格中的category_id

表:链接
link_id(PK)
link_name
link_description
category_id(FK)//引用类别表中的category_id

这里是类别表中的数据
1,英格兰,0
2,西约克郡,1
3,Batley,2
4,利兹,2
5,布拉德福德,2

你可以看到巴特利,利兹和布拉德福德是西约翰郡的子类别,约克郡本身就是英格兰的子类别。

我是什么我希望在浏览子类链接时显示

不仅是该子类别的名称,而且是'类别'的父母,盛大的父母等等就像下面的例子一样。

英国>西约克郡> Batley

我在很多目录网站上看到这个,但我的PHP书籍都没有涵盖如何
这已经完成了。必须非常简单,所以有人请指出我正确的方向。希望我已经解释得很好了,我不知道这个过程叫什么。

干杯

Phil



它通常被称为''痕迹导航''。


继续lorento'非常有用的顶级帖子(: p)......我觉得他的是什么?b $ b试图说出父母和孩子之间的关系可能是你的树形结构(这似乎是你的结果)我试图在你的桌子上代表
表示,最好用一张单独的表格表示......所以你会有两张桌子给b $ b:
< br $>
类别

-------------------------

category_id (PK)

category_name


Category_Relations

----------------- --------

parent_category_id(FK)

child_category_id(FK)

parent_category_id + child_category_id在第二表一起将

组成一个复合公关表格的imary键。


当然,结果是每个孩子*可能*成为多个父母的孩子。这可能是一个优点或缺点,取决于你希望孩子们的行为方式,这可能是



我猜你可以使用另一种方法是为了识别

以下实体/表格(...是的,弗吉尼亚州,我知道它们不一样

):


国家

地区




如果你的结构要求你拥有无限的树''节点'但是,这种

方法不起作用,所以你当前的结构效果更好。我猜

这取决于你是否正在尝试构建一个n叶树,或者

强制执行严格的地点规则...规范的时间?


IMO说你当前的结构''不是一个好的桌子

design''是不正确的,因为你可能确实想要构建一棵树''而不是

严格的权利模式。


课程的马匹。


la ********** @ yahoo.com 说...
< blockquote class =post_quotes>
我认为这不是一个好的桌面设计。你需要首先学习数据库

规范化。
http://dev.mysql.com/tech-resources/...alization.html



内部自引用表格实际上是*好*数据库设计,

并且满足规范化规则。


<搁置>


因为很难理解上下文。


为什么顶级帖子不好?


< / aside>


这是类别表中的数据

1,England,0

2 ,西约克郡,1

3,Batley,2

4,利兹,2

5,Bradford,2


当我浏览链接的子类别时,我想要显示的是

不仅是该子类别的名称,而且是类别的父母,隆重
父母等等如下例所示。


UK> West Yorkshire> Batley


我明白了这在许多目录网站上,但我的PHP书籍都没有涵盖

这是怎么做的。必须非常简单,所以有人请指出我在

正确的方向。希望我已经解释得很好,不知道这个过程被调用了什么




它最常被称为breadcrumbing。 (来自Hansel& Gretel童话

故事)。


以下伪代码应该可以完成这项工作,但是*不是*优化,因为我

现在使用支持精彩CONNECT BY构造的Oracle来递归查询自引用表,所以这是一个简单的

的slegdehammer MySQL版本。


我的MySQL(以及我的一些PHP)非常生锈,但是如果有任何

错误,至少它会给你一个想法如何做到这一点:


另请注意,它并不适合不良数据,例如。一个link_id,它不会被
解析回class_id为0的最终父级。


<?php

函数makeBreadcrumbs(


Let''s say I have a simple web application running with just two MySQL
tables. The tables structure is as follows:

Table: category
category_id (PK)
category_name
parent_category (FK) //references category_id in this table

Table: link
link_id (PK)
link_name
link_description
category_id (FK) //references category_id in category table

Here is the data in the category table
1, England, 0
2, West Yorkshire, 1
3, Batley, 2
4, Leeds, 2
5, Bradford, 2

As you can see Batley, Leeds and Bradford are sub-categories of West
Yorkshire which itself is a sub-category of England.

What I want to display when I am browsing through sub-categories of links is
not only the name of that sub-category but it''s category''s parents, grand
parents and so on like the below example.

UK >West Yorkshire >Batley

I see this on a lot of directory sites but none of my PHP books cover how
this is done. Must be quite simple so can someone please point me in the
right direction. Hope I''ve explained it well enough, haven''t a clue what
this process is called.

Cheers

Phil

解决方案

I think it is not a good table design. You need to learn database
normalization first.
http://dev.mysql.com/tech-resources/...alization.html

--
http://www.mastervb.net
http://www.theukmap.com
On Feb 2, 7:58 am, "Phil Latio" <phil.la...@f-in-stupid.co.ukwrote:

Let''s say I have a simple web application running with just two MySQL
tables. The tables structure is as follows:

Table: category
category_id (PK)
category_name
parent_category (FK) //references category_id in this table

Table: link
link_id (PK)
link_name
link_description
category_id (FK) //references category_id in category table

Here is the data in the category table
1, England, 0
2, West Yorkshire, 1
3, Batley, 2
4, Leeds, 2
5, Bradford, 2

As you can see Batley, Leeds and Bradford are sub-categories of West
Yorkshire which itself is a sub-category of England.

What I want to display when I am browsing through sub-categories of links is
not only the name of that sub-category but it''s category''s parents, grand
parents and so on like the below example.

UK >West Yorkshire >Batley

I see this on a lot of directory sites but none of my PHP books cover how
this is done. Must be quite simple so can someone please point me in the
right direction. Hope I''ve explained it well enough, haven''t a clue what
this process is called.

Cheers

Phil




"lorento" <la**********@yahoo.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.com...

>I think it is not a good table design. You need to learn database
normalization first.
http://dev.mysql.com/tech-resources/...alization.html

--
http://www.mastervb.net
http://www.theukmap.com
On Feb 2, 7:58 am, "Phil Latio" <phil.la...@f-in-stupid.co.ukwrote:

>Let''s say I have a simple web application running with just two MySQL
tables. The tables structure is as follows:

Table: category
category_id (PK)
category_name
parent_category (FK) //references category_id in this table

Table: link
link_id (PK)
link_name
link_description
category_id (FK) //references category_id in category table

Here is the data in the category table
1, England, 0
2, West Yorkshire, 1
3, Batley, 2
4, Leeds, 2
5, Bradford, 2

As you can see Batley, Leeds and Bradford are sub-categories of West
Yorkshire which itself is a sub-category of England.

What I want to display when I am browsing through sub-categories of links
is
not only the name of that sub-category but it''s category''s parents, grand
parents and so on like the below example.

UK >West Yorkshire >Batley

I see this on a lot of directory sites but none of my PHP books cover how
this is done. Must be quite simple so can someone please point me in the
right direction. Hope I''ve explained it well enough, haven''t a clue what
this process is called.

Cheers

Phil

It''s often called ''breadcrumb navigation''.

Further to lorento''s incredibly useful top post (:p)... I think what he''s
trying to say that perhaps the relationship between parent and children in
your tree structure (which seems to be what you are trying to represent in
your table), would be best expressed in a separate table... so you''d have
two tables:

Category
-------------------------
category_id (PK)
category_name

Category_Relations
-------------------------
parent_category_id (FK)
child_category_id (FK)

parent_category_id + child_category_id in the second table together would
form a compound primary key for the table.

The result of this, of course, would be that each child *might* become a
child of multiple parents. This could be an advantage or a disadvantage,
depending on how you want the children to behave.

The other approach you could use, I guess, would be to identify the
following entities/tables (... yes, virginia, I know they''re not the same
thing):

Country
Region
Town

If your structure requires that you have infinite tree ''nodes'' however, this
approach will not work, and so your current structure works better. I guess
it depends on whether you are trying to construct an n-leafed tree, or
enforce strict locality rules... time for a specification?

It is incorrect IMO to say that your current structure is ''not a good table
design'', since you may, indeed be trying to construct a ''tree'' rather than a
strict entitiy model.

Horses for courses.


la**********@yahoo.com says...

I think it is not a good table design. You need to learn database
normalization first.
http://dev.mysql.com/tech-resources/...alization.html

Internally self-referencing tables are actually *good* database design,
and satisfy normalization rules.

<aside>

Because it''s difficult to follow context.

Why is top-posting bad?

</aside>

Here is the data in the category table
1, England, 0
2, West Yorkshire, 1
3, Batley, 2
4, Leeds, 2
5, Bradford, 2

What I want to display when I am browsing through sub-categories of links is
not only the name of that sub-category but it''s category''s parents, grand
parents and so on like the below example.

UK >West Yorkshire >Batley

I see this on a lot of directory sites but none of my PHP books cover how
this is done. Must be quite simple so can someone please point me in the
right direction. Hope I''ve explained it well enough, haven''t a clue what
this process is called.

It''s most commonly called "breadcrumbing" (from the Hansel & Gretel fairy
tale).

The following pseudo-code should do the job, but is *not* optimised as I
nowadays use Oracle which supports the wonderful CONNECT BY construct to
recursively query self-referencing tables, so this is a simple
slegdehammer MySQL version.

My MySQL (and some of my PHP) is *very* rusty, but if there are any
errors, at least it will give you an idea of how it might be done:

Note also that it doesn''t cater for bad data, eg. a link_id which won''t
resolve back to an eventual parent with a category_id of 0.

<?php
function makeBreadcrumbs(


这篇关于我有子类别但想要显示完整的类别路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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