user3140617
user3140617

Reputation: 117

Create menu from array which can be hooked up from anywhere on the script

I' am trying to make a menu something like WordPress does but in more simple way. In WordPress you can hook up Admin sidebar menu from any php scripts, thats what I' am trying to do.

This are all the array that contains the menu information

$admin_menu_page = array();
    $admin_submenu_page = array();

    $admin_menu_page[] = array("Dashboard", "dashboard");
    $admin_menu_page[] = array("Pages", "pages");
    $admin_menu_page[] = array("Setings", "setings");

    $admin_submenu_page['dashboard'] = array("Home", "home");
    $admin_submenu_page['dashboard'] = array("Update", "update");

    $admin_submenu_page['pages'] = array("Add New Page", "new");
    $admin_submenu_page['pages'] = array("All Pages", 'pages');

    $admin_submenu_page['setings'] = array("SMTP", "smtp");
    $admin_submenu_page['setings'] = array("Theme Options", "theme-options");

This is the html part that renders that menu in the page

echo '<ul>';
foreach($admin_menu_page as $menu){
    echo '<li>';
    echo '<a href="#">';
    echo $menu[0];
    echo '<ul>';
        foreach($admin_submenu_page[$menu[1]] as $submenu){
            echo '<li>';
            echo '<a href="#">';
            echo $submenu;
            echo '</a>';
            echo '</li>';
        }
    echo '</ul>';
    echo '</a>';
    echo '</li>';
}
echo '</ul>';

This is what it looks like at the moment from the above code enter image description here This is how the end result should be like

enter image description here

Upvotes: 0

Views: 62

Answers (2)

user2472312
user2472312

Reputation:

$main_menu = array() ;

$sub_menu = array() ;
$sub_menu['dashboard'][] = array( "title" => "Home" , "action" => "home" ) ;
$sub_menu['dashboard'][] = array( "title" => "Update" , "action" => "update" ) ;

$main_menu['dashboard']['title'] = 'Dashboard' ;
sort( $sub_menu['dashboard'] ) ;
$main_menu['dashboard']['submenu'] = $sub_menu['dashboard'] ;

$sub_menu['pages'][] = array( "title" => "All Pages" , "action" => "pages" ) ;
$sub_menu['pages'][] = array( "title" => "Add New Page" , "action" => "new" ) ;

$main_menu['pages']['title'] = 'Pages' ;
sort( $sub_menu['pages'] ) ;
$main_menu['pages']['submenu'] = $sub_menu['pages'] ;

$sub_menu['setings'][] = array( "title" => "SMTP" , "action" => "smtp" ) ;
$sub_menu['setings'][] = array( "title" => "Theme Options" , "action" => "theme-options" ) ;

$main_menu['setings']['title'] = 'Setings' ;
sort( $sub_menu['setings'] ) ;
$main_menu['setings']['submenu'] = $sub_menu['setings'] ;

foreach ( $main_menu as $key => $menu ) {
    echo '<li>' ;
    echo '<a href="#">' ;
    echo $menu['title'] ;
    echo '<ul>' ;

    foreach ( $menu['submenu'] as $item ) {
        echo '<li>' ;
        echo "<a href=\"#{$item['action']}\">" ;
        echo $item['title'] ;
        echo '</a>' ;
        echo '</li>' ;
    }
    echo '</ul>' ;
    echo '</li>' ;
}

Note that this solution serve only to submenu 1 to 1, for submenu N to N submenu you should optimize this script using recursion:

See -> http://www.php.net/manual/pt_BR/regexp.reference.recursive.php

Upvotes: 1

kafsoksilo
kafsoksilo

Reputation: 259

First you have to properly create submenus, you are missing brackets:

Instead:

$admin_submenu_page['dashboard'] = array("Home", "home");
$admin_submenu_page['dashboard'] = array("Update", "update");

$admin_submenu_page['pages'] = array("Add New Page", "new");
$admin_submenu_page['pages'] = array("All Pages", 'pages');

$admin_submenu_page['setings'] = array("SMTP", "smtp");
$admin_submenu_page['setings'] = array("Theme Options", "theme-options");

Do:

$admin_submenu_page['dashboard'][] = array("Home", "home");
$admin_submenu_page['dashboard'][] = array("Update", "update");

$admin_submenu_page['pages'][] = array("Add New Page", "new");
$admin_submenu_page['pages'][] = array("All Pages", 'pages');

$admin_submenu_page['setings'][] = array("SMTP", "smtp");
$admin_submenu_page['setings'][] = array("Theme Options", "theme-options");

Second on the render, change:

echo $submenu;

to

echo $submenu[0];

Upvotes: 1

Related Questions