[RESOLVED] Indenting Nested categories
Results 1 to 3 of 3

Thread: [RESOLVED] Indenting Nested categories

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Location
    Nebraska
    Posts
    16

    resolved [RESOLVED] Indenting Nested categories

    i am using a nested categories table, that is setup with id, name, lft, rgt. The children categories have lft and rgt numbers that fall within the paren'ts lft and rgt values. I am wanting to indent the sub-categories, and I am having a hard time with it.

    I have a category called root, that is the parent to all categories. I do not want the Root to show up, and i do not want the direct children categories under root indented.
    It currently looks like this

    root
    +accessories
    +Infant
    ++Girl's
    ++boys
    +Toddler
    ...

    I want it to look like this.

    accessories
    Infant
    +Girl's
    +boys
    Toddler
    ...

    this is the code I have. I did get the Root to go away, but have not been able to fix the indents the way i want.

    PHP Code:
    $sql "SELECT lft, rgt FROM product_categories WHERE id=1"//root category
    $stmt $db->query($sql);
    $row $stmt->fetch(PDO::FETCH_ASSOC);
    $rgt $row['rgt'];
    $lft $row['lft'];
    $right=array();
    $sql "SELECT * FROM product_categories WHERE lft BETWEEN $lft AND $rgt ORDER BY lft ASC";
    $stmt $db->query($sql);
    while (
    $row $stmt->fetch(PDO::FETCH_ASSOC)) {
      if(
    count($right)>0) {
        while (
    $right[count($right)-1]<$row['rgt']) {
          
    array_pop($right);
        }
      }
      if(
    $row['name'] != 'Root') {
        
    $category_list .= '<li>'.str_repeat(' + ',count($right)).$row['name'].'</li>';
      }
      
    $right[] = $row['rgt'];

    Hope the format is okay. I am blind, so the indenting for the code does me no good, so i do not do it for myself. I know people like to have it done that way.

    If there is a different/better way of doing what I want, your suggestions are much appreciated.

    If this has been discussed previously, and I missed it, please feel free to point me in that direction.

    thanks,
    Michael

  2. #2
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    1,740
    I don't know if this would produce the results you're looking for or not, but you could try changing the str_repeat call to use count($right)-1 instead of just count.
    Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail
    I'd rather be a comma, then a full stop.
    User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning

  3. #3
    Junior Member
    Join Date
    Jan 2010
    Location
    Nebraska
    Posts
    16
    thanks, that seem to do the trick, along with my if statement to prevent Root from showing up. Without the if statement, I was getting an error "Warning: str_repeat(): Second argument has to be greater than or equal to 0 in...".

    Thanks,
    Michael

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
  •