Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
207 views
in Technique[技术] by (71.8m points)

PHP - Notice: Undefined index: submenu

I'm trying to create an menu with submenus.

I created two tables on my database, here the data:

Menu table -

menu_id
front
back
url 
menu_friendlyname   
name
modules active

Submenu table -

submenu_id
menu_id url
submenu_name    
modules

I'm doing a specie of MVC, but with my own rules, here the model:

I'm calling two tables here

    $query = ("SELECT DISTINCT name, menu_id
      FROM menu
      INNER JOIN submenu USING(menu_id)");

    $array = db_array($query, 'a+');
    if (!empty($array)) {
        printMenu($array);
        return true;
    } else {
        return false;
    }
}

And I'm calling the view here, as you can see, I'm trying to show the submenus

function  printMenu($array){


echo '<pre>';
var_dump($array);
echo '</pre>';
foreach($array as $key => $values){

echo '<ul>';

echo '<li>';
echo $values['name'];
echo '</li>';


echo '<li>';

foreach($values['submenu'] as $k => $v){
echo '<span>';
echo $v['submenu'];
echo '</span>';

}

echo '</li></ul>';

}
}

And is giving me this error:

Notice: Undefined index: submenu
Warning: Invalid argument supplied for foreach()

I did a var_dump too

array(5) {
  [0]=>
  array(2) {
    ["name"]=>
    string(5) "admin"
    ["menu_id"]=>
    string(1) "1"
  }
  [1]=>
  array(2) {
    ["name"]=>
    string(4) "user"
    ["menu_id"]=>
    string(1) "2"
  }
  [2]=>
  array(2) {
    ["

name"]=>
        string(22) "permissions_management"
        ["menu_id"]=>
        string(1) "3"
      }
      [3]=>
      array

(2) {
    ["name"]=>
    string(10) "job_offers"
    ["menu_id"]=>
    string(1) "5"
  }
  [4]=>
  array(2) {
    ["name"]=>
    string(12) "applications"
    ["menu_id"]=>
    string(1) "6"
  }
}

Maybe the error is from the array

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your query should be:

SELECT menu_id, name, submenu_name
FROM menu
INNER JOIN submenu USING (menu_id)
ORDER BY menu_id, submenu_id

Then the function to print the menus should be:

function  printMenu($array){
    echo '<pre>';
    var_dump($array);
    echo '</pre>';

    $last_menu = null;
    foreach($array as $values){
        if ($values['menu_id'] !== $last_menu) {
            if ($last_menu !== null) {
                echo '</ul>';
            }
            echo '<ul>';
            echo '<li>';
            echo $values['name'];
            echo '</li>';
            $last_menu = $values['menu_id'];
        }
        echo '<li>';
        echo '<span>';
        echo $v['submenu_name'];
        echo '</span>';
    }
    if ($last_menu !== null) {
        echo '</ul>';
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[1] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

57.0k users

...