0

My Code and tables are given below

TABLES

    menu
    ====
    m_id    menu_name   parent_menu_id  menu_order  menu_url    status 
    1       M1          0               1           #           1
    2       M2          0               2           #           1
    3       S1          1               1           #           1
    4       S2          1               2           #           1

    user_rights
    ===========     
    user_id         rights
    1               1,2,3,4
    2               1,2,3

CODE

    <?php
    $get_user_id = "1"; //  user id will be 1 or 2 or any id from user table
    mysql_connect("localhost", "root", "password") or die(mysql_error());
    mysql_select_db("booksdb") or die(mysql_error());

    function getMenuRights($get_user_id) {
    $get_user_id = intval($get_user_id);
    $get_access_query = mysql_query("SELECT access FROM user_rights where user_id='".$get_user_id."'");
    $fetch_access_list = mysql_fetch_row($get_access_query);
    $get_access_list = $fetch_access_list[0];
    return $get_access_list;
    }

    function getUserMainMenus($get_user_id) {
    $get_user_id = intval($get_user_id);
    $rights = getMenuRights($get_user_id);
    $get_main_menu_query = mysql_query("SELECT * FROM menu where m_id IN ($rights) and parent_menu_id='0' order by menu_order");
    while ($row = mysql_fetch_assoc($get_main_menu_query)) {
    $results[] = $row;
    }
    return $results;
    }

    function getUserChildMenu($parent_menu_id, $get_user_id) {
    $parent_menu_id = intval($parent_menu_id);
    $get_user_id = intval($get_user_id);
    $rights = getMenuRights($get_user_id);
    $get_sub_menu = mysql_query("SELECT * FROM menu where parent_menu_id='".$parent_menu_id."' AND m_id IN ($rights)");
    while($row = mysql_fetch_assoc( $get_sub_menu )) {
    $results[] = $row;
    }
    return $results;
    }

    ?>
    <div id='cssmenu'>
    <ul>
    <?php foreach (getUserMainMenus($get_user_id) as $get_main_menu): ?>
    <li class='has-sub'><a href='#'><span><?=$get_main_menu['menu_name']; ?></span></a>
    <ul>
    <?php foreach (getUserChildMenu($get_main_menu['m_id'], $get_user_id) as $sub_menu): ?>
    <li class='has-sub'><a href='<?=$sub_menu['menu_url']; ?>'><span><?=$sub_menu['menu_name']; ?></span></a>
    </li>
    <?php endforeach; ?>   
    </ul>
    </li>
    <?php endforeach; ?>   
    </ul>
    </div>

The above code works fine, but it shows some error.

Undefined variable: results in phpfilename.php on line 21(and 32)   

Also, I have to check before each menu and sub-menu start whether there is value or not and then show the sub-menus.

How can I do that?

In other words, before the start of my menu and sub-menu I need to check for values and then show. If values, show the sub-menu. If not show the main menu alone.

Any help.

Thanks, Kimz

2
  • You should declare $results variable as $results = array(); above the while loop before using it. Commented Apr 30, 2014 at 12:50
  • how to check a if condition before the foreach statement? i mena if values is there show the sub menu else leave it Commented May 2, 2014 at 6:54

1 Answer 1

1

You are trying to append to the array in variable $results which isn't initiated yet.

Add the following:

$results = array();

Above each:

while() { ... }

For example:

function getUserMainMenus($get_user_id) {
    $get_user_id = intval($get_user_id);
    $rights = getMenuRights($get_user_id);
    $get_main_menu_query = mysql_query("SELECT * FROM menu where m_id IN ($rights) and parent_menu_id='0' order by menu_order");
    $results = array(); // add this here
    while ($row = mysql_fetch_assoc($get_main_menu_query)) {
    $results[] = $row;
    }
    return $results;
    }
Sign up to request clarification or add additional context in comments.

3 Comments

how to check a if condition before the foreach statement? i mena if values is there show the sub menu else leave it ..
@user3350885 one question per question. Post that as a separate SO question.
bro, i have done that also.. ;) stackoverflow.com/questions/23422565/… check it here please.

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.