I am working on the same project, and I have not found found the need to use objects. The database pretty much takes care of the structure, and php functions can do the rest. My solution was have a parent field for pages that points to a section name, as you have, but also to add a parent field for the sections table, so that sections can point to other sections as parents. It is still very manageable because there are only 2 parent fields to keep track of, one for each table, and I can nest my structure as many levels as necessary in the future.
So for dynamic tree creation, I am going to recursively check for parents until I hit a null, which indicates to me that the current element sits on the doc root. This way we don't need to know any details of the current page structure in the function code, and we can focus on just adding and arranging pages in mysql. Since another poster showed some menu html, I thought I would add an example here for a dynamic breadcrumb path because the themes are so similar.
DATA
// Sample 3 x 2 page array (php / html pages)
// D1 key = page name
// D2 element 1 = page name for menu display
// D2 element 2 = parent name (section)
$pages = Array
(
'sample-page-1.php' => Array ( 'M' => 'page 1', 'P' => null ),
'sample-page-2.php' => Array ( 'M' => 'page 2', 'P' => 'hello' ),
'sample-page-3.php' => Array ( 'M' => 'page 3', 'P' => 'world' )
);
// Sample 2 x 1 section array (parent directories)
// D1 key = section name
// D2 element = parent name (if null, assume root)
$sections = Array
(
'hello' => null,
'world' => 'hello'
);
$sep = ' > '; // Path seperator
$site = 'test.com'; // Home string
FUNCTIONS
// Echo paragraph to browser
function html_pp ( $text )
{
echo PHP_EOL . '<p>' . sprintf ( $text ) . '</p>' . PHP_EOL;
}
// Get breadcrumb for given page
function breadcrumb ( $page )
{
// Reference variables in parent scope
global $pages;
global $sections;
global $sep;
global $site;
// Get page data from array
$menu = $pages [ $page ] [ 'M' ];
$parent = $pages [ $page ] [ 'P' ];
if ( $parent == null )
{
$path = $site . $sep . $menu;
}
else
{
$path = $site . $sep . get_path ( $parent ) . $sep . $menu;
}
return $path;
}
// Trace ancestry back to root
function get_path ( $parent )
{
// Reference variables in parent scope
global $sections;
global $sep;
if ( $sections [ $parent ] == null )
{
// No more parents
return $parent;
}
else
{
// Get next parent through recursive call
return get_path ( $sections [ $parent ] ) . $sep . $parent;
}
}
USAGE
// Get breadcrumbs by page name
$p1 = 'sample-page-1.php';
$p2 = 'sample-page-2.php';
$p3 = 'sample-page-3.php';
html_pp ( $p1 . ' || ' . breadcrumb ( $p1 ) );
html_pp ( $p2 . ' || ' . breadcrumb ( $p2 ) );
html_pp ( $p3 . ' || ' . breadcrumb ( $p3 ) );
// or use foreach to list all pages
foreach ( $pages as $page => $data)
{
html_pp ( $page . ' || ' . breadcrumb ( $page ) );
}
OUTPUT
sample-page-1.php || test.com > page 1
sample-page-2.php || test.com > hello > page 2
sample-page-3.php || test.com > hello > world > page 3