Displaying hierarchical categories in PHP/MySQL

Discussion in 'PHP' started by gingerbreadweb, Oct 29, 2009.

  1. #1
    Hey all

    I have a "category" table in a MySQL database for an online shop, in which one of the fields is "parent". So the structure of categories is hierarchical.

    CATEGORY TABLE:
    categoryID
    parentID
    category_name

    Using PHP, I want to display the category hierarchy as shown below - is there a simple way of doing this? Some kind of MySQL join, or just a simple loop of sorts?

    • Clothes
      • Jackets
      • Shirts
        • Casual
        • Formal
      • Jeans
      • Underwear
    • Accessories
      • Watches
      • Jewellery
        • Rings
        • Necklaces
      • Belts

    Thanks for your time
     
    gingerbreadweb, Oct 29, 2009 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    well, tree manipulation is always tricky in MySQL.

    Do share table structure and a sample data with us to figure out best way out of it.

    Ideally storing a complete lineage in one column saves multiple querying else repeated queries can kill server at times.

    If categories are not added / modified daily, use a script to generate html once and use it for whole day :)
     
    mastermunj, Oct 29, 2009 IP