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;
}

Recommended Answers

All 2 Replies

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;
}

Thank you Edwin! You are a saint :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.