我想建立一个小的eCommerce网站,没有什么特别的,没有添加到购物车,评级和类似的东西,但我确实有一个两难境地。我需要有多个级别的类别。
举个例子,如果我可能会有这样的东西:
Samsung
- Tv
- Smart Tv
- Samsuns Smart Tv s45
- Samsung Smart Tv k7x
- 3D Tv
- some product
- Laptops
- Intel Core 7
- Some product
- Inter Core 5
- Some product
LG
- same stuff唯一的问题是类别的级别没有预先定义,所以我不知道会有多少级别。
有没有人能给我举几个例子,mysql数据库是怎么看这种结构的?
发布于 2016-11-22 13:39:15
对于多个类别级别
id | category_name | slug | parent_id考虑这样的设计
发布于 2018-10-03 20:39:00
我已经在coldfusion中写了这段代码,你可以根据你的要求进行修改...
<cfquery name="Stuff" datasource="xyz">
SELECT CategoryId, CategoryName, ParentId
FROM Category
ORDER BY CategoryName
</cfquery>
<cfset RootItems=ArrayNew(1)>
<cfset Depth=ArrayNew(1)>
<cfset RowFromID=StructNew()>
<cfset ChildrenFromID=StructNew()>
<cfloop query="Stuff">
<cfset RowFromID[CategoryId]=CurrentRow>
<cfif NOT StructKeyExists(RowFromID, ParentId)>
<cfset ArrayAppend(RootItems, CategoryId)>
<cfset ArrayAppend(Depth, 0)>
</cfif>
<cfif NOT StructKeyExists(ChildrenFromID, ParentId)>
<cfset ChildrenFromID[ParentId]=ArrayNew(1)>
</cfif>
<cfset ArrayAppend(ChildrenFromID[ParentId], CategoryId)>
</cfloop>
<cfloop condition="ArrayLen(RootItems) GT 0">
<cfset ThisID=RootItems[1]>
<cfset ArrayDeleteAt(RootItems, 1)>
<cfset ThisDepth=Depth[1]>
<cfset ArrayDeleteAt(Depth, 1)>
<cfif StructKeyExists(RowFromID, ThisID)>
<cfset RowID=RowFromID[ThisID]>
<cfoutput>#RepeatString("--",ThisDepth)# #Stuff.CategoryName[RowID]#</cfoutput>
</cfif>
<cfif StructKeyExists(ChildrenFromID, ThisID)>
<cfset ChildrenIDs=ChildrenFromID[ThisID]>
<cfloop from="#ArrayLen(ChildrenIDs)#" to="1" step="-1" index="i">
<cfset ArrayPrepend(RootItems, ChildrenIDs[i])>
<cfset ArrayPrepend(Depth, ThisDepth + 1)>
</cfloop>
</cfif>
</cfloop>

https://stackoverflow.com/questions/24110229
复制相似问题