SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict aguroyz's Avatar
    Join Date
    Jan 2009
    Location
    Konoha Fire Country
    Posts
    311
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help: Implementing a Category that have unlimited sub-categories deep..

    Hello,

    Please help,

    How do i accomplish something like this:

    Main Category: Apparel


    • Designers
    • Apparel Gifts
    • Categories
      • Dresses
        • Shop By Occasion
        • Shop By Silhouette
        • Fine Apparel

      • Tops
      • Jackets
      • Pants
      • Leggings
      • Skirts
      • Suiting
      • Outerwear Shop
      • Cashmere Shop
      • Evening Shop
      • Denim Shop
      • Swim Shop
      • Intimate
      • Special Sizes

    • Lifestyles





    Each sub-categories have sub-categories, but only the selected will display its childrens...



    Heres my category table
    id | parent(id for parent) | level( 0 if no parent) | catname
    ---------------------------------------------------------------
    1 | 0 | 0 | Apparel
    ----------------------------------------------------------------
    2 | 1 | 1 | Designers

    ----------------------------------------------------------------
    3 | 1 | 1 | Apparel Gifts

    ----------------------------------------------------------------
    4 | 1 | 1 | Categories

    ----------------------------------------------------------------
    5 | 4 | 2 | Dressess
    ----------------------------------------------------------------
    6 | 4 | 2 | Tops
    ----------------------------------------------------------------
    7 | 4 | 2 | Jackets
    ----------------------------------------------------------------
    8 | 5 | 3 | Shop By Occasions
    ----------------------------------------------------------------
    9 | 5 | 3 | Shop By Silhouette
    ----------------------------------------------------------------
    10 | 5 | 3 | Fine Apparel
    Please please help me...


    Thanks
    Uniquely FILIPINO... See how talented and creative Filipinos are.
    http://www.smalltym.com
    Custom Web Designs:
    http://proweaver.com

  2. #2
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Search for recursive function & static keywords in php, you can definitely get hints and thus the solution

  3. #3
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is apparently SQL question, not PHP
    For MySQL you can start from http://dev.mysql.com/tech-resources/...ical-data.html
    Also, you can google for "Nested Sets" and "Materialized Path" technique

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,350
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    sorry, shrapnel, it is very much a php question

    the database design here is the classic adjacency model (using parent_id)

    he's asking how do you write the php to display several levels with nested ULs

    don't tell him to use nested sets because that's a completely different data model

    that's as unhelpful as telling him to switch to oracle, because hey, they have recursion based on parent_id built right into the oracle sql language...
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Guru rageh's Avatar
    Join Date
    Apr 2006
    Location
    London, Formerly Somalia
    Posts
    612
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have a look at this very similar thread:

    http://www.sitepoint.com/forums/showthread.php?t=635970
    ------------------

  6. #6
    SitePoint Addict aguroyz's Avatar
    Join Date
    Jan 2009
    Location
    Konoha Fire Country
    Posts
    311
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks,

    but still I haven't got any idea on how to implement this..

    Please help me out here.. hehehe
    Uniquely FILIPINO... See how talented and creative Filipinos are.
    http://www.smalltym.com
    Custom Web Designs:
    http://proweaver.com

  7. #7
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can get hint from following recursive function:

    PHP Code:
    function displayAll($node_id$level){        
        
    $sql     "SELECT * FROM `categories` WHERE id = '".$node_id."'";
        
    $result mysql_query($sql);        
        
    $data     = array();        
        while(
    $row     mysql_fetch_array($result)){               
           
    $data[]     = $row;                  
           
    $data     array_merge(displayAll($row['parent'], $level 1), $data);
        }            
        return 
    $data;



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •