菜单显示在php页面中

时间:2011-01-05 13:01:31

标签: php menu

我正在从管理员端创建CMC页面。我的前端有一个3级菜单。我的CMS页面数据库表结构包含nameparent_iddescriptionstatus字段。

现在我必须在菜单中显示子页面的所有页面,子页面和子页面。如何撰写此查询?

<!-- BEGIN Menu -->
     <ul id="nav">
 <li><a href="consumers.php">Consumers</a>
   <ul><li><a href="personal_finance.php">Personal Finance </a>
 <ul><li><a href="personal_financial_makeover.php">Personal Financial Makeover</a></li>
       <li><a href="personal_finance_consultancy.php">Personal Finance Consultancy</a></li>
      </ul>
     </li>
     <li><a href="investings.php">Investings</a>
      <ul>
  <li><a href="securities_analysis.php">Securities Analysis</a></li>
      <li><a href="portfolio_investment.php">Bernie Madoff Shield (Portfolio and Investment Analysis)</a></li>
     </ul>
     </li>
    </ul>
          </li>
         <li><a href="entrepreneurs.php">Entrepreneurs</a>
            <ul>
     <li><a href="market_analysis.php">Market Analysis and Competitive Report </a></li>
              <li><a href="business_plans_feasibility.php">Business Plans &amp; Feasibility Studies </a>
             <ul>
             <li><a href="business_plans.php">Business Plans</a>
                    <ul>
                       <li><a href="level1.php">Level 1</a></li>
                       <li><a href="level2.php">Level 2</a></li>
                       <li><a href="level3.php">Level 3</a></li>
                     </ul>

                   </li>
       </ul>

      </li>
                   </ul>

  <!-- END Menu -->

正如你所说,我试过了。我有主页。它没有孩子。仍然是空的下拉菜单。然后正确显示第一级菜单。并显示第二级菜单。实际上鼠标悬停在第一级菜单下拉列表上。我正在下降。到现在好了。但公开表示不同的菜单名称。请看我的附件。我附加了2images。在第一个图像直接显示这样的。鼠标悬停在firstlevel显示下拉列表中的第二个图像。 alt text alt text

<? 

$query = "SELECT
    t1.auto_id AS l1_id, t1.name AS l1_name
    ,t2.auto_id AS l2_id, t2.name AS l2_name
    ,t3.auto_id AS l3_id, t3.name AS l3_name
FROM static_pages AS t1
LEFT JOIN static_pages AS t2 ON t2.parent_id = t1.auto_id
LEFT JOIN static_pages AS t3 ON t3.parent_id = t2.auto_id";

$result = mysql_query( $query );

$results = array() ;
while($row=mysql_fetch_assoc($result)){
    $results[] = $row;
}

// Building the menu. Quick and dirty.
$menu = array() ;
foreach( $results as $result ) {
    $menu_level_3 = array( 'auto_id' => $result['l3_id'], 'name' => $result['l3_name'], 'children' => array() ) ;
    $menu_level_2 = array( 'auto_id' => $result['l2_id'], 'name' => $result['l2_name'], 'children' => array( $menu_level_3['auto_id'] => $menu_level_3 ) ) ;
    $menu_level_1 = array( 'auto_id' => $result['l1_id'], 'name' => $result['l1_name'], 'children' => array( $menu_level_2['auto_id'] => $menu_level_2 ) ) ;

    if( !isset( $menu[$menu_level_1['auto_id']] ) ) {
        $menu[$menu_level_1['auto_id']] = $menu_level_1 ;
    } elseif( !isset( $menu[$menu_level_1['auto_id']]['children'][$menu_level_2['auto_id']] ) ) {
        $menu[$menu_level_1['auto_id']]['children'][$menu_level_2['auto_id']] = $menu_level_2 ;
    } else {
        $menu[$menu_level_1['auto_id']]['children'][$menu_level_2['auto_id']]['children'][$menu_level_3['auto_id']] = $menu_level_3;
    }
}
?>
<ul id="nav">
<? foreach($menu as $item0) : ?>
    <li>
    <a href="?page=<?=$item0['auto_id']?>"><?=$item0['name']?></a>
    <? if(!empty($item0['children'])){?>
    <ul>
    <? foreach($item0['children'] as $item1) : ?>
        <li>
        <a href="?page=<?=$item1['auto_id']?>"><?=$item1['name']?></a>
        <? if(!empty($item1['children'])){?>
        <ul>
        <? foreach($item1['children'] as $item2) : ?>
            <li><a href="?page=<?=$item2['auto_id']?>"><?=$item2['name']?></a></li>
        <? endforeach; ?>
        </ul>
        <? }?>
        </li>
    <? endforeach; ?>
    </ul>
    <? }?>
    </li>
<? endforeach;exit;?>
</ul>

1 个答案:

答案 0 :(得分:2)

我建议您阅读Managing Hierarchical Data in MySQL

查询将是:

SELECT t1.name AS lev1, t2.name as lev2, t3.name as lev3
FROM pages AS t1
LEFT JOIN pages AS t2 ON t2.parent = t1.pages_id
LEFT JOIN pages AS t3 ON t3.parent = t2.pages_id;

<强>更新

$query = '
SELECT
    t1.pages_id AS l1_id, t1.name AS l1_name
    ,t2.pages_id AS l2_id, t2.name AS l2_name
    ,t3.pages_id AS l3_id, t3.name AS l3_name
FROM pages AS t1
LEFT JOIN pages AS t2 ON t2.parent = t1.pages_id
LEFT JOIN pages AS t3 ON t3.parent = t2.pages_id
';

$result = mysql_query( $query );

$results = array() ;
while($row=mysql_fetch_assoc($result)){
    $results[] = $row;
}

// Building the menu. Quick and dirty.
$menu = array() ;
foreach( $results as $result ) {
    $menu_level_3 = array( 'id' => $result['l3_id'], 'name' => $result['l3_name'], 'children' => array() ) ;
    $menu_level_2 = array( 'id' => $result['l2_id'], 'name' => $result['l2_name'], 'children' => array( $menu_level_3['id'] => $menu_level_3 ) ) ;
    $menu_level_1 = array( 'id' => $result['l1_id'], 'name' => $result['l1_name'], 'children' => array( $menu_level_2['id'] => $menu_level_2 ) ) ;

    if( !isset( $menu[$menu_level_1['id']] ) ) {
        $menu[$menu_level_1['id']] = $menu_level_1 ;
    } elseif( !isset( $menu[$menu_level_1['id']]['children'][$menu_level_2['id']] ) ) {
        $menu[$menu_level_1['id']]['children'][$menu_level_2['id']] = $menu_level_2 ;
    } else {
        $menu[$menu_level_1['id']]['children'][$menu_level_2['id']]['children'][$menu_level_3['id']] = $menu_level_3;
    }
}
?>
<ul>
<? foreach($menu as $item0) : ?>
    <li>
    <a href="?page=<?=$item0['id']?>"><?=$item0['name']?></a>
    <ul>
    <? foreach($item0['children'] as $item1) : ?>
        <li>
        <a href="?page=<?=$item1['id']?>"><?=$item1['name']?></a>
        <ul>
        <? foreach($item1['children'] as $item2) : ?>
            <li><a href="?page=<?=$item2['id']?>"><?=$item2['name']?></a></li>
        <? endforeach; ?>
        </ul>
        </li>
    <? endforeach; ?>
    </ul>
    </li>
<? endforeach; ?>