Skip to content

Instantly share code, notes, and snippets.

@abidibo
Created November 10, 2011 16:03
Show Gist options
  • Save abidibo/1355210 to your computer and use it in GitHub Desktop.
Save abidibo/1355210 to your computer and use it in GitHub Desktop.
Recursion to get a tree from a id-parent sql table
<?php
// I use here Jeff's db methods
public function getTree($registry, $parent=0) {
$child_rows = $registry->db->autoSelect("id", TABLE, "parent='$parent'", "name");
$children = array();
foreach($child_rows as $row) {
$children[$row['id']] = self::getTree($registry, $row['id']);
}
return $children;
}
?>
@nabidefacto
Copy link

so if there are 100 child rows, you recursively make 100 SQL queries?

@abidibo
Copy link
Author

abidibo commented May 9, 2012

Since this snippet is used to retrieve a menu tree, this is not the case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment