Php动态菜单栏代码错误

发布于 2024-12-03 10:11:09 字数 1947 浏览 1 评论 0原文

我在使酒吧中的链接正常工作时遇到问题,数据库的设置如下 我的数据库http://bloodkittens.com/resources/upload/db.png

<?php 
error_reporting(E_ALL);
ini_set('display_errors', '1');

mysql_connect("localhost", "dbuser", "******");
mysql_select_db("guild");

// prepare special array with parent-child relations
$menuData = array(
'items' => array(),
'parents' => array()
);
$result = mysql_query("SELECT  id_menu id, parentID_menu parentId, label_menu name FROM main_menu` ORDER BY parentID_menu"); 

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

// menu builder function, parentId 0 is the root
function buildMenu($parentId, $menuData)
{
$html = '';
$parent='';

if (isset($menuData['parents'][$parentId]))
{
    $menuClass= ($parentId==0) ? ' class="navbar" id="navbar"' : '';
    $parent= ($parentId==0) ? 0 : 1;    
    $html = "<ul{$menuClass}>\n";

    foreach ($menuData['parents'][$parentId] as $itemId)
    {
        //subment
        $result=mysql_query("select * from main_menu where parentID_menu='$itemId'");
        if (mysql_num_rows($result)>(int)0 && $parentId!=0) {
            $subm  =' class="navbar"';
        }else{
           $subm  ='';
        }
        //end

        $menu = $parentId == 0 ? ' class="menulink"' : '';      //class of main menu
        $html .= '<li>' . "<a{$subm}{$menu} href=\"#\" >{$menuData['items'][$itemId]['name']}</a>";

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

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

return $html;
}

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

我该如何使值 link_menu 成为数据库中每个单独条目的代码中的 href ?而不是 \'#\' 因为代码完全可以工作,我很高兴我将 css 应用到它之后它的外观,但链接不起作用

I am having a problem with making the links in my bar work properly the database is setup like so my db http://bloodkittens.com/resources/upload/db.png

<?php 
error_reporting(E_ALL);
ini_set('display_errors', '1');

mysql_connect("localhost", "dbuser", "******");
mysql_select_db("guild");

// prepare special array with parent-child relations
$menuData = array(
'items' => array(),
'parents' => array()
);
$result = mysql_query("SELECT  id_menu id, parentID_menu parentId, label_menu name FROM main_menu` ORDER BY parentID_menu"); 

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

// menu builder function, parentId 0 is the root
function buildMenu($parentId, $menuData)
{
$html = '';
$parent='';

if (isset($menuData['parents'][$parentId]))
{
    $menuClass= ($parentId==0) ? ' class="navbar" id="navbar"' : '';
    $parent= ($parentId==0) ? 0 : 1;    
    $html = "<ul{$menuClass}>\n";

    foreach ($menuData['parents'][$parentId] as $itemId)
    {
        //subment
        $result=mysql_query("select * from main_menu where parentID_menu='$itemId'");
        if (mysql_num_rows($result)>(int)0 && $parentId!=0) {
            $subm  =' class="navbar"';
        }else{
           $subm  ='';
        }
        //end

        $menu = $parentId == 0 ? ' class="menulink"' : '';      //class of main menu
        $html .= '<li>' . "<a{$subm}{$menu} href=\"#\" >{$menuData['items'][$itemId]['name']}</a>";

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

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

return $html;
}

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

How would i make it so that the value link_menu would be the href in the code for each separate entry in the db? rather then \'#\' because the code works completely and i'm very happy how it looks after i apply my css to it but the links aren't working

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幸福不弃 2024-12-10 10:11:09

对于工作菜单排序
必须在表中添加一个新字段,
并将此查询从更改

$result = mysql_query("SELECT id_menu id, parentID_menu parentId, label_menu name,  link_menu link FROM main_menu ORDER BY parentID_menu");

为:

$result = mysql_query("SELECT id_menu id, parentID_menu parentId, label_menu name,  link_menu link  FROM main_menu  ORDER BY menu_sort");

For work the menu sorting
must be add a new field in the table,
and change this query from

$result = mysql_query("SELECT id_menu id, parentID_menu parentId, label_menu name,  link_menu link FROM main_menu ORDER BY parentID_menu");

to :

$result = mysql_query("SELECT id_menu id, parentID_menu parentId, label_menu name,  link_menu link  FROM main_menu  ORDER BY menu_sort");
策马西风 2024-12-10 10:11:09

你似乎从来没有选择它

$result = mysql_query("SELECT id_menu id, parentID_menu parentId, label_menu, link_menu, name FROM main_menu ORDER BY parentID_menu"); 

然后..

$html .= '<li>' . "<a{$subm}{$menu} href=\"{$menuData['items'][$itemId]['link_menu']}\" >{$menuData['items'][$itemId]['name']}</a>";

you never seem to select it

$result = mysql_query("SELECT id_menu id, parentID_menu parentId, label_menu, link_menu, name FROM main_menu ORDER BY parentID_menu"); 

and then..

$html .= '<li>' . "<a{$subm}{$menu} href=\"{$menuData['items'][$itemId]['link_menu']}\" >{$menuData['items'][$itemId]['name']}</a>";
抚笙 2024-12-10 10:11:09

使用以下命令代替 href=\"#\" >

href=\"{$menuData['items'][$itemId]['link_menu']}\" >

Instead of href=\"#\" >, use this:

href=\"{$menuData['items'][$itemId]['link_menu']}\" >
落花随流水 2024-12-10 10:11:09

我使用的代码最终是这样的,我想我纯粹是靠运气才让它工作的,但不管怎样我赢了:)

<?php 
error_reporting(E_ALL);
ini_set('display_errors', '1');

mysql_connect("localhost", "BloodKittens", "Zangoshi1");
mysql_select_db("bloodkittens");

// prepare special array with parent-child relations
$menuData = array(
'items' => array(),
'parents' => array(),
'links' => array()
);
 $result = mysql_query("SELECT id_menu id, parentID_menu parentId, label_menu name,  link_menu link FROM main_menu ORDER BY parentID_menu");

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

// menu builder function, parentId 0 is the root
function buildMenu($parentId, $menuData)
 {
$html = '';
$parent='';

if (isset($menuData['parents'][$parentId]))
{
    $menuClass= ($parentId==0) ? ' class="navbar" id="navbar"' : '';
    $parent= ($parentId==0) ? 0 : 1;    
    $html = "<ul{$menuClass}>\n";

    foreach ($menuData['parents'][$parentId] as $itemId)
    {
        //subment
        $result=mysql_query("select * from main_menu where parentID_menu='$itemId'");
        if (mysql_num_rows($result)>(int)0 && $parentId!=0) {
            $subm  =' class="navbar"';
        }else{
           $subm  ='';
        }
        //end

        $menu = $parentId == 0 ? ' class="menulink"' : '';      //class of main menu
        $html .= '<li>' . "<a{$subm}{$menu} href=\"{$menuData['items'][$itemId]['link']}\" >{$menuData['items'][$itemId]['name']}</a>";

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

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

   return $html;
 }

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

?>

the code that i used was in the end like this i think i got it to work by pure luck but regardless i win : )

<?php 
error_reporting(E_ALL);
ini_set('display_errors', '1');

mysql_connect("localhost", "BloodKittens", "Zangoshi1");
mysql_select_db("bloodkittens");

// prepare special array with parent-child relations
$menuData = array(
'items' => array(),
'parents' => array(),
'links' => array()
);
 $result = mysql_query("SELECT id_menu id, parentID_menu parentId, label_menu name,  link_menu link FROM main_menu ORDER BY parentID_menu");

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

// menu builder function, parentId 0 is the root
function buildMenu($parentId, $menuData)
 {
$html = '';
$parent='';

if (isset($menuData['parents'][$parentId]))
{
    $menuClass= ($parentId==0) ? ' class="navbar" id="navbar"' : '';
    $parent= ($parentId==0) ? 0 : 1;    
    $html = "<ul{$menuClass}>\n";

    foreach ($menuData['parents'][$parentId] as $itemId)
    {
        //subment
        $result=mysql_query("select * from main_menu where parentID_menu='$itemId'");
        if (mysql_num_rows($result)>(int)0 && $parentId!=0) {
            $subm  =' class="navbar"';
        }else{
           $subm  ='';
        }
        //end

        $menu = $parentId == 0 ? ' class="menulink"' : '';      //class of main menu
        $html .= '<li>' . "<a{$subm}{$menu} href=\"{$menuData['items'][$itemId]['link']}\" >{$menuData['items'][$itemId]['name']}</a>";

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

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

   return $html;
 }

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

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文