尝试使用PHP和MySQL获取节点的路径
I'm trying to build a breadcrumb here and I'm having some trouble doing it. The problem arises from the fact that I have to save category name (in two languages), slug and id.
我正在尝试在这里建立一个面包屑,我在做这件事时遇到了一些麻烦。问题源于我必须保存类别名称(两种语言),slug和id。
My categories table looks like this:
我的类别表如下所示:
CREATE TABLE `categories` (
`category_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`category_name` varchar(256) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`category_slug` varchar(256) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`category_parent` smallint(5) unsigned NOT NULL DEFAULT '0',
`category_description_ro` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
`category_description_en` text CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`category_id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=latin1
Bellow is an example of data in the table:
贝娄是表中数据的一个例子:
category id | category name | category_parent
1 Categoria 1 0
2 Categoria 2 0
3 Categoria 3 0
4 Categoria 1.1 1
5 Categoria 1.2 1
6 Categoria 1.3 1
7 Categoria 1.1.2 4
I have to mention that I can not make any change to the MySQL table.
我必须提到我无法对MySQL表做任何更改。
What I would like to accomplish is to build a breadcrumb like:
我想要完成的是建立一个像以下一样的面包屑:
Home > Categoria 1 > Categoria 1.1 > Categoria 1.1.2
首页>分类1>分类1.1>分类1.1.2
Can anyone provide a small piece of code on how to do this? I already tried that "the path to a node" code from here, but, as I said, I have to include in that array category_name, category_slug, category_description_ro and category_description_en. Thanks in advance guys.
任何人都可以提供一小段代码来解决这个问题吗?我已经从这里尝试了“节点的路径”代码,但是,正如我所说,我必须在该数组中包含category_name,category_slug,category_description_ro和category_description_en。先谢谢你们。
2 个解决方案
#1
1
In pseudo-code:
$currentID = 7;
do{
$category = getCategoryByID($currentID);
$currentID = $category['category_parent'];
$crumb .= $category['category_name'];
}while($category['category_parent']);
This will just loop back up the category tree, building the crumb until there are no more parents.
这将只是循环备份类别树,构建面包屑,直到没有更多的父母。
更多相关文章
- 字体图标的引入和通过媒体查询改变导航样式
- HTML样式和常用选择器
- 字体图标的引用和自定义样式/媒体查询的使用
- 数据库的CURD操作、PDO本质与原理的学习
- CSS之伪类选择器和简单盒子简单案例
- 伪类选择器与盒模型常用属性
- 伪类选择器-结构伪类、根据位置选择匹配
- 7.4——常用标签与应用场景之表格与单元格
- css伪类选择器和盒模型