How do I Display Full Hierarchical Tree from MySQL table?

Discussion in 'PHP' started by peppy, Jun 15, 2010.

  1. #1
    Hello,

    I have a MySQL database set up something like this:


    +-------------+----------------------+--------------+-------+
    | lev1        | lev2                 | lev3         | lev4  |
    +-------------+----------------------+--------------+-------+
    | ELECTRONICS | TELEVISIONS          | TUBE         | NULL  |
    | ELECTRONICS | TELEVISIONS          | LCD          | NULL  |
    | ELECTRONICS | TELEVISIONS          | PLASMA       | NULL  |
    | ELECTRONICS | PORTABLE ELECTRONICS | MP3 PLAYERS  | FLASH |
    | ELECTRONICS | PORTABLE ELECTRONICS | CD PLAYERS   | NULL  |
    | ELECTRONICS | PORTABLE ELECTRONICS | 2 WAY RADIOS | NULL  |
    | OUTDOORS    | PATIO                | BRICK        | NULL  |
    | OUTDOORS    | PATIO                | STONE        | NULL  |
    | OUTDOORS    | PATIO                | STONE        | SMALL |
    | OUTDOORS    | LAWN                 | LAWNMOWER    | NULL  |
    +-------------+----------------------+--------------+-------+
    
    PHP:
    How can I make some hierarchical links that display something like this:

    
    +-----------------------+
    | LINKS                 |
    +-----------------------+
    | ELECTRONICS           |
    |  TELEVISIONS          |
    |   TUBE                |
    |   LCD                 |
    |   PLASMA              |
    |  PORTABLE ELECTRONICS |
    |   MP3 PLAYERS         |
    |    FLASH              |
    |   CD PLAYERS          |
    |   2 WAY RADIOS        |
    |    FRS                |
    | OUTDOORS              |
    |  PATIO                |
    |   BRICK               |
    |   STONE               |
    |    SMALL              |
    |   LAWN                |
    |    LAWNMOWER          |
    +-----------------------+
    
    PHP:
    Thanks
    Best
     
    peppy, Jun 15, 2010 IP
  2. Chronomus

    Chronomus Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You may want to look at this: dev.mysql.com/tech-resources/articles/hierarchical-data.html. Funny how you used similar same table names and structure too, as the answer you're looking for is on that page. Honestly, if you typed the exact title of your thread on Google you would find the same link as the first result. Look at the section "Finding the Depth of the Nodes". :eek:
     
    Last edited: Jun 15, 2010
    Chronomus, Jun 15, 2010 IP
  3. peppy

    peppy Active Member

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    95
    #3
    lol yeah, I searched Google and found that page and that's where I got the table but I couldn't understand this page well.

    Also, this page didn't say how to print the links out in PHP either.
     
    peppy, Jun 15, 2010 IP
  4. Chronomus

    Chronomus Peon

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Have you tried looking at the section "Finding the Depth of the Nodes"? It gives you the MySQL query to do it. Then all you need to do is loop through all the results and echo them on the page, indenting each table according to the depth value, using a PHP script, as you would normally do with any query.
     
    Last edited: Jun 15, 2010
    Chronomus, Jun 15, 2010 IP
  5. peppy

    peppy Active Member

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    95
    #5
    I read that. I think the confusing part is the way my table is built. They are using a table where the data is organized differently than mine I think.

    The table in my original post is exactly the same way mine is set up in my Excel and MySQL files.

    I assume that my table is designed using the "Nested Set Model" ?
     
    Last edited: Jun 15, 2010
    peppy, Jun 15, 2010 IP
  6. peppy

    peppy Active Member

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    95
    #6
    Well, I sort of found a solution:

    I used a series of "IF" and "IF ELSE" tags and a variation of them nested in eachother. I am sort of new to MySQL/PHP type sits so I'm not sure if this strategy is going to have adverse effects. Basically my end goal for this problem is to create a nice nested navigation link menu.
     
    peppy, Jun 15, 2010 IP