PHP菜单问题

PHP菜单问题,php,html,templates,menu,Php,Html,Templates,Menu,作为一个更大的网站重新设计的步骤之一,我把我们网站的大部分内容放入html文件中,作为包含文件使用。我打算通过URL向PHP模板页面传递一个变量,以调用适当的include 我们的网站有许多程序,每个程序都需要一个索引页以及大约5个子页。这些程序页面需要一个菜单系统在不同页面之间导航。我将页面命名为pagex_1、pagex_2、pagex_3等,其中pagex是页面内容的描述 我的问题是,处理这个菜单系统的最佳方式是什么?是否有方法修改用于到达索引页的初始变量,以便在菜单中创建链接以到达其他页

作为一个更大的网站重新设计的步骤之一,我把我们网站的大部分内容放入html文件中,作为包含文件使用。我打算通过URL向PHP模板页面传递一个变量,以调用适当的include

我们的网站有许多程序,每个程序都需要一个索引页以及大约5个子页。这些程序页面需要一个菜单系统在不同页面之间导航。我将页面命名为pagex_1、pagex_2、pagex_3等,其中pagex是页面内容的描述

我的问题是,处理这个菜单系统的最佳方式是什么?是否有方法修改用于到达索引页的初始变量,以便在菜单中创建链接以到达其他页


谢谢你的帮助

我将菜单结构存储在一个关联的PHP数组中,如下所示:

$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(
           ...
        )
     )
  )
);
使该数组也可用于模板脚本,例如通过include/require。然后在变量中使用分隔符来标识菜单项,就像文件系统对文件夹和文件所做的那样,从而标识要包含的结果页面:

“index”将标识索引页 “产品”将标识产品的起始页 “products/product1”将在products页面中标识product 1子页面
。。。等等。

请在回答中添加一些代码之外的其他详细信息。谢谢删除数组的一个小改动:unset$url、$text、$sub for->unset$child;
// 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
        )

)