0

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

1
  • Not too sure what are you trying to achieve but from what I understood the easiest way to go is create a menu as stand-alone include file and then you can include it in any script Commented Feb 21, 2014 at 10:47

2 Answers 2

1

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

Comments

1
$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

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.