Display and grouping
Results 1 to 3 of 3

Thread: Display and grouping

  1. #1
    Senior Member
    Join Date
    Apr 2005
    Posts
    1,246

    Display and grouping

    I have a category table with I'd, category and parentID

    the parent ID is the I'd of another row.

    I want to loop though all records in a ul li item and display any rows which had the current ID as its parent ID

    It might I 3 or 4 layers deep


    Food
    Desert
    Ice cream
    Chocolate

  2. #2
    Senior Member
    Join Date
    Apr 2005
    Posts
    1,246
    Any ideas? i'm still looking for an answer

  3. #3
    Senile Member
    Join Date
    Oct 2002
    Location
    Cheshire, UK
    Posts
    1,660
    Quote Originally Posted by NZ_Kiwis View Post
    Any ideas? i'm still looking for an answer
    try something like this

    Code:
    $db = new mysqli(HOST, USERNAME, PASSWORD, DATABASE );
    
    $sql = "SELECT category_id, name, parent FROM category";
    $res = $db->query($sql);
    $cats = array();
    while (list($id, $name, $parent) = $res->fetch_row()) {
        $cats[$parent][] = array($id, $name);
    }
    
    $parentCat = 1; // set start category
    listCats($parentCat, $cats);
    
    //
    // recursive function to list child categories
    //
    function listCats($id, &$cats, $level=0)
    {
        if (isset($cats[$id]))
        foreach ($cats[$id] as $child) {
            $indent = str_repeat('----', $level);
            echo $indent . $child[1] . '<br>';
            listCats($child[0], $cats, $level+1);
        }
    }
    Barand

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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