Q:

Get MySQL Path Columns of Subpages Into Hierarchical Array And Output To Tiered 

I have a MySQL table with content for a custom CMS I've built.
There are a number of columns, but the ones dealing with the paths are:

path (full path to the page, e.g. /category/page1/ )
parentpath (path up to that particular page, e.g. /category/ )
childpath (path following the parentpath, e.g. page1/ )

I've written a function that gets all subpages of a particular page into an array, and another that outputs them into li elements.
function get_subpages($path) {

    $GetSubpages=mysql_query("SELECT * FROM content WHERE parentpath='$path'");
    while ( $SubpageLoop = mysql_fetch_assoc($GetSubpages) ) {
        $Subpages[] = $SubpageLoop;
    }
    unset($GetSubpages);
    return $Subpages;
}

and
function list_subpages($path) {

    $Subpages=get_subpages($path);

    if ($Subpages) {

        $Output="";

        foreach($Subpages as $Subpage) {

            $Output .= '<li><a href="'.$Subpage[path].'">'.$Subpage[title].'</a></li>';

        }

    }

    return $Output;
}

However, I would like to have the functions operate so that the array produced is hierarchical, corresponding to the page structure, along with the HTML list that is produced.
I've been experimenting with the DISTINCT operator in the SQL query, and then creating a secondary loop to then get the subpages of each result, but without much luck
mysql_query("SELECT DISTINCT parentpath FROM content WHERE parentpath LIKE '$path%' ORDER BY path ASC");

I also started playing with a preg_match to catch any items with a slash in the middle of them and then run another query using one of the array results, but haven't been able to figure out exactly how to get that to work the way I want.
preg_match ( "/^(.*)\/(.*)\//", $ParentLevel, $Matches );

I'm just having a really hard time piecing it together.  Can anyone help me out?

A:

Figured it out:
 function has_immediate_children($path) {

    $path=sanitize($path);

    $GetImmediateChildren=mysql_query("SELECT path FROM content WHERE parentpath='$path' ORDER BY path ASC");
    if ( mysql_num_rows($GetImmediateChildren)>0) { return true; }
    else { return false; }

}

function get_immediate_children($path) {

    $path=sanitize($path);

    $GetImmediateChildren=mysql_query("SELECT * FROM content WHERE parentpath='$path' ORDER BY path ASC");
    while ( $ImmediateChildrenLoop = mysql_fetch_assoc($GetImmediateChildren) ) {

        $ImmediateChildren[]=$ImmediateChildrenLoop;

    }
    return $ImmediateChildren;

}

function get_all_subpages($path) {

    $path=sanitize($path);

    $ImmediateChildren=get_immediate_children($path);

    echo '<ul>';

    foreach ($ImmediateChildren as $ImmediateChild) {

        echo '<li><a href="'.$ImmediateChild[path].'">'.$ImmediateChild[title].'</a></li>';

        if ( has_immediate_children($ImmediateChild[path]) ) {

            get_all_subpages($ImmediateChild[path]);

        }

    }

    echo '</ul>';

}

