1

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

4 Answers 4

1

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");
Sign up to request clarification or add additional context in comments.

Comments

0

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>";

2 Comments

this doesnt work because link_menu is not appart of $menuData. $menuData is an array of parrents and items. and those arrays are filled by $menuItems
Ok neither of your input fixed my problem but i fixed my code now so it all works
0

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

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

1 Comment

this doesnt work because link_menu is not appart of $menuData. $menuData is an array of parrents and items. and those arrays are filled by $menuItems
0

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);

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.