Edit

Drupal PHP Template Coding Drupal DB Query to find Children (Recursively)

<?php
function loadchildrens($context){
	$sql_children = "SELECT node.nid AS nid,
   node.vid AS node_vid,
   node.title AS node_title
 FROM node node 
 LEFT JOIN content_field_connection node_data_field_connection ON node.vid = node_data_field_connection.vid
 WHERE (node.type in ('category')) AND (node_data_field_connection.field_connection_nid = ".$context.")
   ORDER BY node_title ASC";

	$db_query_children = db_query( $sql_children );

	while( $row = db_fetch_array($db_query_children) ){
		$context_tree[ $row["nid"] ] = array();
		if( $row["nid"] != "517" )
			$context_tree[ $row["nid"] ] = loadchildrens( $row["nid"] );
	}
	return $context_tree;
}
?>
<?php

$node = node_load(arg(1));

$spheres_via_path = array();

$sql_spheres_via_project = "SELECT node.title AS node_title, node.nid AS nid, node.type AS node_type
FROM {node} node
LEFT JOIN {field_data_field_project} field_data_field_project ON node.nid = field_data_field_project.entity_id
WHERE (node.type in ('note')) AND (field_data_field_project.field_project_nid = '".arg(1)."')";

$db_query_spheres_via_project = db_query( $sql_spheres_via_project );

if( $db_query_spheres_via_project->rowCount() ){
	$return = "<h3>Notes</h3>\n";
	$return .= "<ul>\n";
	foreach( $db_query_spheres_via_project as $sphere ){
		if( isset($sphere->nid) ) $spheres_via_project[ $sphere->nid ] = $sphere;
		$return .= "<li><a href=\"/node/".$sphere->nid."\">".$sphere->node_title."</a></li>\n";
	}
	$return .= "</ul>\n";
    return $return;
}

return '<div><strong>Associated Notes:</strong></div>';
?>
    // Obtain sphere nodes
    $query=new EntityFieldQuery();
    $query->entityCondition('entity_type','node')
          ->entityCondition('bundle','sphere');
    $sephiroths=$query->execute();
    if(array_key_exists('node',$sephiroths))
        $sephiroths=entity_load('node',array_keys($sephiroths['node']));


    // Obtain path nodes
    $query=new EntityFieldQuery();
    $query->entityCondition('entity_type','node')
          ->entityCondition('bundle','path');
    $paths=$query->execute();
    if(array_key_exists('node',$paths))
        $paths=entity_load('node',array_keys($paths['node']));