0

trying to get some thing like that dynamically

<ul>
<li><a href="index.php">Home</a></li>
<li><a href="about.php">About Us</a></li>
<li><a href="#">Academics</a>
    <ul style="overflow: hidden; display: block; height: 0px; z-index: 51; opacity: 0.00980392;">
        <li><a href="bscs.php">Bs Computer Science</a></li>
        <li><a href="dit.php">Diplomas (DIT &amp; DCHE)</a></li>
        <li><a href="mbait.php">MBAIT</a></li>
    </ul>
</li>
<li><a class=" " href="#">College</a>
</ul>

the code is

<?php 
//========================================================
$result = mysql_query("    SELECT id, parentId, name
    FROM
        menu
    ORDER BY
        parentId, name");

$menuData = array(
    'items' => array(),
    'parents' => array()
);

while ($menuItem = mysql_fetch_assoc($result))
{
    $menuData['items'][$menuItem['id']] = $menuItem;
    $menuData['parents'][$menuItem['parentId']][] = $menuItem['id'];
}

function buildMenu($parentId, $menuData)
{
    $html = '';

    if (isset($menuData['parents'][$parentId]))
    {
        $html = '<ul id="main_menu">';
        foreach ($menuData['parents'][$parentId] as $itemId)
        {
            $html .= '<li>' . $menuData['items'][$itemId]['name'];

                // find childitems recursively
                $html .= buildMenu($itemId, $menuData);

            $html .= '</li>';
        }
        $html .= '</ul>';
    }

    return $html;
}

// output the menu
echo buildMenu(0, $menuData); 
//=======================================================
 ?>

above code is showing only first parents elements in the menu and the remaing elements are not showing.. the menu is not working correctly becoz of the class id is not given in the ul tag.. and by writing this

echo '<ul id="main_menu">';
// output the menu
echo buildMenu(0, $menuData); 
echo "</ul>";

it shows nothing in the menu

4

1 回答 1

1

好的,试试这个:

<?php

//========================================================
$result = mysql_query("    SELECT id, parentId, name, link
    FROM
        menu
    ORDER BY
        parentId, name");

$menuData = array(
    'items' => array(),
    'parents' => array()
);

while ($menuItem = mysql_fetch_assoc($result)) {
    $menuData['items'][$menuItem['id']] = $menuItem;
    $menuData['parents'][$menuItem['parentId']][] = $menuItem['id'];
}

function buildMenu($parentId, $menuData)
{
    $html = '';

    if (isset($menuData['parents'][$parentId]) && count( $menuData['parents'][$parentId] ) > 0 ) {
       if( $parentId == "0" ){
          $html = '<ul id="main_menu">';
        }else{
          $html = '<ul id="sub_menu">';
        }
        foreach ($menuData['parents'][$parentId] as $itemId) {
            $html .= '<li>';
            $html .= strlen($menuData['items'][$itemId]['link']) > 2?
                     '<a href="'.$menuData['items'][$itemId]['link'].'">'.$menuData['items'][$itemId]['name'].'</a>':
                     $menuData['items'][$itemId]['name'];
            $html .= buildMenu($itemId, $menuData);
            $html .= "</li>";
        }
        $html .= '</ul>';
    } else {

        $html .= '<li>' . $menuData['items'][$parentId]['name'].'</li>';
    }

    return $html;
}

// output the menu
echo buildMenu(0, $menuData);
//=======================================================

?>
于 2012-12-01T20:29:49.987 回答