PHP - Notice: Undefined index: submenu

user40308

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

Barmar

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>';
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related