As one of the steps toward a greater website redesign I am putting the majority of the content of our website into html files to be used as includes. I am intending on passing a variable to the PHP template page through the URL to call the proper include.
Our website has many programs that each need an index page as well as about 5 sub-pages. These program pages will need a menu system to navigate between the different pages.I am naming the pages pagex_1, pagex_2, pagex_3, etc. where "pagex" is descriptive of the page content.
My question is, what would be the best way to handle this menu system? Is there a way to modify the initial variable used to arrive at the index page to create links in the menu to arrive at the other pages?
Thanks for any help!
I'd store the menu structure in an associative PHP array like so:
$menu = array(
'index'=>array(
'title'=>'Homepage',
'include'=>'home.html',
'subitems'=>array()
),
'products' => array(
'title'=>'Products',
'include'=>'products.html',
'subitems'=>array(
'product1'=>array(
...
),
'product2'=>array(
...
),
'product3'=>array(
...
)
)
)
);
Make that array available to the template script as well (e.g. by include/require). Then use delimiters in the variable to identify the menu item (like a file system does with folders and files) and therefore the resulting page to be included:
- 'index' would identify the index page
- 'products' would identifiy the products start page
- 'products/product1' would identify the product 1 subpage inside the products page
... and so on.
// Menu Array multidimensional
$menu = array(
'index' => array(
'title' => 'Homepage',
'include' => 'home.html',
'subitems' => array()
),
'products' => array(
'title' => 'Products',
'include' => 'products.html',
'subitems' => array(
'product1' => array(
'title' => 'product1',
'include' => 'product1.html',
'subitems' => array(
'product1.2' => array(
'title' => 'product1.2',
'include' => 'product1.2.html',
'subitems' => array()
)
)
),
'product2' => array(
'title' => 'product2',
'include' => 'product2.html',
'subitems' => array()
),
'product3' => array(
'title' => 'product3',
'include' => 'product31.html',
'subitems' => array()
),
'product4' => array(
'title' => 'product4',
'include' => 'product4.html',
'subitems' => array()
)
)
)
);
// Make menu
function makeMenu($menu_array,$is_sub = false,$list=['ul','li']){
$attr = (!$is_sub) ? ' class="menu"' : ' class="submenu"';
$child = NULL;
$menu = "<{$list[0]}{$attr}>";
foreach($menu_array as $id => $items){
foreach($items as $key => $val){
if( is_array($val) ) {
if ( !empty($val) ) $child = makeMenu($val,true,$list);
} else {
$$key = $val;
}
}//foreach
$menu .= "<{$list[1]}>";
$menu .= '<a href="'.$include.'">'.$title.'</a>';
$menu .= $child; // Sub Menu
$menu .= "</{$list[1]}>";
unset($url, $text, $sub);
}//foreach
$menu .= "</{$list[0]}>";
return $menu;
}
// retrieve the desired page with the shaft
function makeSubMenu($menu, $key) {
return makeMenu(array($menu[$key]));
}
// chain Article
function chainItem(array $array, $unset = '' ){
$ds = array();
if ( is_array($array) )
foreach((array)$unset as $arr) unset($array[$arr]);
foreach($array as $key=>$value)
if (!is_array($value)) $ds[$key] = $value;
return $ds;
}
// Build walk recursive -> single array insert database MySql
function walkRecursive($array, $ordId = true, $unset=[], $children = 'children', $i = 1, $parent = 0, &$res = [] ) {
if ( !is_array($array) ) return array();
foreach(array_values($array) as $key=>$arr) {
$das = array(
'id' => $id = $ordId ? $arr['id'] : $i++,
'parent' => $parent
);
$res[] = array_merge($das,chainItem($arr,$unset));
if( isset($arr[$children]) ) {
walkRecursive($arr[$children], $ordId, $unset, $children, $i, $id, $res );
}
}
return $res;
}
echo makeMenu($menu);
// Result of the print
<ul class="menu">
<li><a href="home.html">Homepage</a></li>
<li><a href="products.html">Products</a>
<ul class="submenu">
<li><a href="product1.html">product1</a>
<ul class="submenu">
<li><a href="product1.2.html">product1.2</a></li>
</ul>
</li>
<li><a href="product2.html">product2</a>
<ul class="submenu">
<li><a href="product1.2.html">product1.2</a></li>
</ul>
</li>
<li><a href="product31.html">product3</a>
<ul class="submenu">
<li><a href="product1.2.html">product1.2</a></li>
</ul>
</li>
<li><a href="product4.html">product4</a>
<ul class="submenu">
<li><a href="product1.2.html">product1.2</a></li>
</ul>
</li>
</ul>
</li>
</ul>
// Build walk recursive -> single array insert database MySql
header("Content-type: text/plain");
print_r( walkRecursive($menu,false,['parent','id'],'subitems') );
// Print array -> Result:
Array
(
[0] => Array
(
[id] => 1
[parent] => 0
[title] => Homepage
[include] => home.html
)
[1] => Array
(
[id] => 2
[parent] => 0
[title] => Products
[include] => products.html
)
[2] => Array
(
[id] => 3
[parent] => 2
[title] => product1
[include] => product1.html
)
[3] => Array
(
[id] => 4
[parent] => 3
[title] => product1.2
[include] => product1.2.html
)
[4] => Array
(
[id] => 4
[parent] => 2
[title] => product2
[include] => product2.html
)
[5] => Array
(
[id] => 5
[parent] => 2
[title] => product3
[include] => product31.html
)
[6] => Array
(
[id] => 6
[parent] => 2
[title] => product4
[include] => product4.html
)
)
精彩评论