954,585 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

indented subcategory recursive loop

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;
}
carie
Newbie Poster
3 posts since Sep 2009
Reputation Points: 10
Solved Threads: 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"]."";
    showlist($line["component_part_id"], &$catlistids,$depth+1);
  }
return $catlistids;
}
edwinhermann
Junior Poster
141 posts since Sep 2009
Reputation Points: 67
Solved Threads: 29
 

Thank you Edwin! You are a saint :)

carie
Newbie Poster
3 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: