Skip to content

Instantly share code, notes, and snippets.

@nigelheap
Created May 26, 2011 00:25
Show Gist options
  • Save nigelheap/992297 to your computer and use it in GitHub Desktop.
Save nigelheap/992297 to your computer and use it in GitHub Desktop.
<?php
public function get_children_ids($node_id, $tree_table, $left_col, $right_col, $depth = 1){
$result = $this->EE->db->query(
"SELECT node.*, (COUNT(parent.node_id) - (sub_tree.depth + 1)) AS depth
FROM ".$tree_table." AS node,
".$tree_table." AS parent,
".$tree_table." AS sub_parent,
(
SELECT node.node_id, (COUNT(parent.node_id) - 1) AS depth
FROM ".$tree_table." AS node,
".$tree_table." AS parent
WHERE node.".$left_col." BETWEEN parent.".$left_col." AND parent.".$right_col."
AND node.node_id = ".$node_id."
GROUP BY node.node_id
ORDER BY node.".$left_col."
)AS sub_tree
WHERE node.lft BETWEEN parent.".$left_col." AND parent.".$right_col."
AND node.".$left_col." BETWEEN sub_parent.".$left_col." AND sub_parent.".$right_col."
AND sub_parent.node_id = sub_tree.node_id
GROUP BY node.node_id
HAVING depth = ".$depth."
ORDER BY node.".$left_col.";");
return $result->num_rows() ? $result->result_array() : array();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment