开发者

PHP /MySQL dynamic breadcrumb

开发者 https://www.devze.com 2023-02-04 00:15 出处:网络
---------------------------------------------------------开发者_开发问答-------------------------
---------------------------------------------------------开发者_开发问答-------------------------
|id_category|parent_id|category_level|category_sort|category_name|category_rw_tag|
|--------------------------------------------------------------------------------|
|1          |0        |1             |NULL         |Shopping     |NULL           |
|2          |1        |2             |NULL         |Laptops      |NULL           |
|3          |2        |3             |NULL         |Accessories  |NULL           |
|4          |3        |4             |NULL         |HDD          |NULL           |
----------------------------------------------------------------------------------

I have a table of categories. to the configuration shown above. Fist of my problem : I would like to build a breadcrumb like this :

Shopping > Laptops > Accesories > HDD

Second : the breadcrumb. must be dynamic! so when I query the db table with parameter id_category= 5 to display :

Shopping > Laptops > Accesories > HDD

when I query the db table with parameter id_category= 4

Shopping > Laptops > Accesories

...and so on.

I have read all question regarding my question but simply I had no idea how to d this.

Php Jedy's you are my only Hope.


you would have to do some queries to retrieve each category until you hit a root category (i.e parent_id = 0). so in your example you would query category with id = 4, check the row parent_id is not equal to zero so query the next category with the parent_id and so on.

If your ecommerce website doesn't have too much categories that would be ok, if you have more than 5 levels of categories I would advice to make a breadscrumb field in your table with an already formated text.


Your question is a logical duplicate of What is the best practice for fetching a tree of nodes from a database for further rendering.

There is a long list of articles and resources you can use.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号