0

Hello, I'm working on a recursive loop subcategory system. The code below is ALMOST where I need it. The last thing I'd like to do is indent each subcategory based on levels deep, rather than just separate by commas. I can't seem to wrap my head around it. Any suggestions?

function showlist($parent, &$catlistids="") {
  $result = mysql_query("SELECT component_part_id, component_part_quantity_used FROM builds WHERE build_part_id='$parent'");
  while ($line = mysql_fetch_array($result)) {
    if($catlistids!=""){ $catlistids .= ", "; }
    $catlistids .= get_part($line["component_part_id"]) . ' ' .  $line["component_part_quantity_used"];
    showlist($line["component_part_id"], &$catlistids);
  }
return $catlistids;
}
2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by carie
0

Hello, I'm working on a recursive loop subcategory system. The code below is ALMOST where I need it. The last thing I'd like to do is indent each subcategory based on levels deep, rather than just separate by commas. I can't seem to wrap my head around it. Any suggestions?

Not sure if you're wanting to return HTML code or plaintext.

Try this for plaintext:

function showlist($parent, &$catlistids="",$depth = 0) {
  $result = mysql_query("SELECT component_part_id, component_part_quantity_used FROM builds WHERE build_part_id='$parent'");
  while ($line = mysql_fetch_array($result)) {
    for ($i=0;$i<$depth;$i++) {
      $catlistids .= "  ";
    }
    $catlistids .= get_part($line["component_part_id"]) . ' ' .  $line["component_part_quantity_used"]."\n";
    showlist($line["component_part_id"], &$catlistids,$depth+1);
  }
return $catlistids;
}

Or for HTML:

function showlist($parent, &$catlistids="",$depth = 0) {
  $result = mysql_query("SELECT component_part_id, component_part_quantity_used FROM builds WHERE build_part_id='$parent'");
  while ($line = mysql_fetch_array($result)) {
    for ($i=0;$i<$depth;$i++) {
      $catlistids .= "&nbsp;&nbsp;";
    }
    $catlistids .= get_part($line["component_part_id"]) . ' ' .  $line["component_part_quantity_used"]."<br>";
    showlist($line["component_part_id"], &$catlistids,$depth+1);
  }
return $catlistids;
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.