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.
$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