Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>DATA</p> <pre><code>// 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' =&gt; Array ( 'M' =&gt; 'page 1', 'P' =&gt; null ), 'sample-page-2.php' =&gt; Array ( 'M' =&gt; 'page 2', 'P' =&gt; 'hello' ), 'sample-page-3.php' =&gt; Array ( 'M' =&gt; 'page 3', 'P' =&gt; 'world' ) ); // Sample 2 x 1 section array (parent directories) // D1 key = section name // D2 element = parent name (if null, assume root) $sections = Array ( 'hello' =&gt; null, 'world' =&gt; 'hello' ); $sep = ' &gt; '; // Path seperator $site = 'test.com'; // Home string </code></pre> <p>FUNCTIONS</p> <pre><code>// Echo paragraph to browser function html_pp ( $text ) { echo PHP_EOL . '&lt;p&gt;' . sprintf ( $text ) . '&lt;/p&gt;' . 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; } } </code></pre> <p>USAGE</p> <pre><code>// 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 =&gt; $data) { html_pp ( $page . ' || ' . breadcrumb ( $page ) ); } </code></pre> <p>OUTPUT</p> <p>sample-page-1.php || test.com > page 1</p> <p>sample-page-2.php || test.com > hello > page 2</p> <p>sample-page-3.php || test.com > hello > world > page 3</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload