morowind
morowind

Reputation: 302

PHP /MySQL dynamic breadcrumb

----------------------------------------------------------------------------------
|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.

Upvotes: 0

Views: 2787

Answers (2)

Samuel Herzog
Samuel Herzog

Reputation: 3611

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.

Upvotes: 0

RageZ
RageZ

Reputation: 27313

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.

Upvotes: 1

Related Questions