2

I have a tables in MySQL one is test table columns like this

  id refid name userdefined
  1  0       A
  2  0       B 
  3  0       C
  4  1       A1   abc
  5  1       A2   cde
  6  2       B1
  7  2       B2
  8  3       C3
  9  3       c4
  10 3       c5
  11 2       C7 
  12 2       C8  lmn
  13 11       c9
  14 11      c10 

Using the above table I am creating the dynamic menu using a PHP function. I have one more table, it has the login fields and data like this:

id username password  field3
1  john      john     1,3,4,5,6,7,8,9

What i want is if John is logged in, how do I only show the menu items in field3. I am new to PHP. I am showing all menu items using a function in PHP please help me, thanks in advance.

<?php

//this is php function for creating menu
$sql2 = mysqli_query($conn, 'select * from login');
$row = mysqli_fetch_array($sql2);
$menu_items = explode(',', $row['field3']);

function submenu($parentid = 0)
{
    global $conn;
    $sql = mysqli_query($conn, "SELECT * FROM test WHERE refid=" . $parentid . " AND id IN " . ($menu_items));
    {
        $rowcount = mysqli_num_rows($sql);
        if ($rowcount > 0) {
            echo '<ul>';
        }

        while ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)) {
            if ($row['refid'] == 0) {
                echo '<li class="limain">' . $row['name'];
                submenu($row['id']);
                echo '</li>';

            } else {
                if ($row['userdefined']) {
                    echo '<li class="lichild"><a href="' . $row['userdefined'] . '">' . $row['name'] . '</a>';
                } else {
                    echo '<li class="lichild">' . $row['name'];
                }
                submenu($row['id']);
                echo '</li>';

            }

        }

        if ($rowcount > 0) {
            echo '</ul>';
        }
    }
}

?>

3 Answers 3

1
$sql2=mysqli_query($conn,'select * from login');
$row = mysqli_fetch_array($sql2);
//storing the field values in menuitems variables
$menu_items =  $row['field3'];


//pass the menuitem variables to query

function submenu($parentid=0){
global $conn;
$sql=mysqli_query($conn,"SELECT * FROM test WHERE refid=".$parentid ." AND 
id in ($menu_items)");
 {
   $rowcount=mysqli_num_rows($sql);
   if($rowcount>0){
    echo '<ul>';
   }
         while($row=mysqli_fetch_array($sql,MYSQLI_ASSOC))
        {
          if($row['refid']==0)
          {
            echo '<li class="limain">'.$row['name'];
             submenu($row['id']);
             echo '</li>';
        }
          else{
            if($row['userdefined']){
           echo '<li class="lichild"><a 
   href="'.$row['userdefined'].'">'.$row['name'].'</a>';
         }else{
           echo '<li class="lichild">'.$row['name'];
         }
           submenu($row['id']);
           echo '</li>';
        }

      }
  if($rowcount>0){
    echo '</ul>';
    }
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Your function is mostly correct in principle, I had a bit of trouble following your function so I changed a few things to improve readability, but this should work.

The only issue I found that would prevent this from working was your if($refid), which is checking for any data not == to null.

0 == null //true 0 === null //false

If I understood your table correctly, entries with a refid of 0 are parent items. In that case they're the only menu items passing into that part of the function, so I changed it from if ($refid) to if ($refid === 0)

With regards to you wanting to you only wanting to show specific menu items, you'll need to run a separate query outside of the function to get the list you made. And then add a check in you submenu() function.

I didn't have a way to test this, but it should work.

//Query to get list of menu items
$menu_items = explode(',', $row['menus']);

function submenu($parentid=0, $menu_items=null) {
    global $conn;
    $sql=mysqli_query($conn,"SELECT * FROM test WHERE refid=".$parentid);

    $rowcount=mysqli_num_rows($sql);
    if($rowcount > 0) {
        echo '<ul>';
    }

    while($row=mysqli_fetch_array($sql,MYSQLI_ASSOC)) {

        if($menu_items !== null || !in_array($id, $menu_items)) {
            continue;
        }

        $id = $row['id'];
        $refid = $row['refid'];
        $userdefined = $row['userdefined'];
        $name = $row['name'];

        if($refid === 0) {
            echo "<li class='limain'>{$name}";
            submenu($id, $menu_items);
            echo '</li>';
        } else if ($refid > 0) {
            if($userdefined) {
               echo "<li class='lichild'><a href='{$userdefined}'>{$name}</a>";
            } else {
               echo "<li class='lichild'>{$name}";
            }
            submenu($id, $menu_items);
            echo '</li>';
        }
    }

    if($rowcount > 0) {
        echo '</ul>';
    }
}

4 Comments

i think you didn't read the second part of question.anyway thanks for help.
@itsme, apologies for that, I've corrected my answer to better respond to your question.
no problem. Would you mind voting my answer, or marking it as correct please :). Could do with the reputation.
it is actually not working as expected thats y i din't accept it.thanks for the help.
0

You must protect the php files too, not only the menu items. If the user know the php filename, can access with a direct link to protected area.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.